31 lines
699 B
TypeScript
31 lines
699 B
TypeScript
import { useConfig } from '@kevisual/use-config';
|
|
import { Sequelize } from 'sequelize';
|
|
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
|
|
|
type PostgresConfig = {
|
|
postgres: {
|
|
username: string;
|
|
password: string;
|
|
host: string;
|
|
port: number;
|
|
database: string;
|
|
};
|
|
};
|
|
const config = useConfig<PostgresConfig>();
|
|
|
|
const postgresConfig = config.postgres;
|
|
|
|
if (!postgresConfig) {
|
|
console.error('postgres config is required');
|
|
process.exit(1);
|
|
}
|
|
// connect to db
|
|
export const init = () => {
|
|
return new Sequelize({
|
|
dialect: 'postgres',
|
|
...postgresConfig,
|
|
// logging: false,
|
|
});
|
|
};
|
|
export const sequelize = useContextKey('sequelize', init);
|