generated from alphane/template
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { Sequelize, Options } from "@sequelize/core"
|
|
import { PostgresDialect } from "@sequelize/postgres"
|
|
import { isEmpty, isNil } from "lodash"
|
|
|
|
import {
|
|
DB_DATABASE,
|
|
DB_HOST,
|
|
DB_PASSWORD,
|
|
DB_PORT,
|
|
DB_SSL,
|
|
DB_USERNAME,
|
|
SEQUELIZE_LOGGING,
|
|
} from "@/config"
|
|
import compactSql from "@/utils/compact-sql"
|
|
|
|
if (isEmpty(DB_DATABASE)) throw new Error("database name is unset.")
|
|
if (isEmpty(DB_USERNAME)) throw new Error("database username is unset.")
|
|
if (isEmpty(DB_PASSWORD)) throw new Error("database password is unset.")
|
|
if (isEmpty(DB_HOST)) throw new Error("database host is unset.")
|
|
if (isNil(DB_PORT) || isNaN(DB_PORT)) throw new Error("database port is unset.")
|
|
|
|
function sqlLogger(query: string) {
|
|
console.log(compactSql(query))
|
|
}
|
|
|
|
// See https://sequelize.org/docs/v7/databases/postgres/
|
|
export const SEQUELIZE_CONFIG: Options<PostgresDialect> = {
|
|
dialect: PostgresDialect,
|
|
database: DB_DATABASE,
|
|
user: DB_USERNAME,
|
|
password: DB_PASSWORD,
|
|
host: DB_HOST,
|
|
port: DB_PORT,
|
|
ssl: DB_SSL ? { rejectUnauthorized: false } : false,
|
|
schema: "public", // default - explicit for clarity
|
|
logging: SEQUELIZE_LOGGING ? sqlLogger : false,
|
|
pool: {
|
|
max: 20,
|
|
min: 2,
|
|
acquire: 60_000,
|
|
idle: 10_000,
|
|
evict: 10_000,
|
|
},
|
|
define: {
|
|
underscored: true,
|
|
timestamps: true, // default - explicit for clarity.
|
|
paranoid: true, // adds deleted_at column
|
|
},
|
|
}
|
|
|
|
const db = new Sequelize(SEQUELIZE_CONFIG)
|
|
|
|
export default db
|