Files
code-center/src/modules/sequelize.ts
2025-07-04 00:02:18 +08:00

41 lines
1.0 KiB
TypeScript

import { Sequelize } from 'sequelize';
import { config } from './config.ts';
import { log } from './logger.ts';
export type PostgresConfig = {
postgres: {
username: string;
password: string;
host: string;
port: number;
database: string;
};
};
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
log.error('postgres config is required password and user');
log.error('config', config);
process.exit(1);
}
const postgresConfig = {
username: config.POSTGRES_USER,
password: config.POSTGRES_PASSWORD,
host: config.POSTGRES_HOST || 'localhost',
port: parseInt(config.POSTGRES_PORT || '5432'),
database: config.POSTGRES_DB || 'postgres',
};
// connect to db
export const sequelize = new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});
sequelize
.authenticate({ logging: false })
.then(() => {
log.info('Database connected');
})
.catch((err) => {
log.error('Database connection failed', { err, config: postgresConfig });
process.exit(1);
});