social-router/src/modules/sequelize.ts
2025-06-21 11:09:58 +08:00

35 lines
878 B
TypeScript

import { Sequelize } from 'sequelize';
import { config } from './config.ts';
import { useContextKey } from '@kevisual/context';
export type PostgresConfig = {
postgres: {
username: string;
password: string;
host: string;
port: number;
database: string;
};
};
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
console.error('postgres config is required password and user');
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',
};
export const init = async () => {
return new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});
};
export const sequelize = useContextKey('sequelize', () => init());