54 lines
1.9 KiB
TypeScript
Executable File
54 lines
1.9 KiB
TypeScript
Executable File
import { MikroOrmModuleAsyncOptions } from "@mikro-orm/nestjs";
|
|
import { PostgreSqlDriver } from "@mikro-orm/postgresql";
|
|
import { Logger } from "@nestjs/common";
|
|
import { ConfigService } from "@nestjs/config";
|
|
|
|
import { MIKRO_ORM_QUERY_LOGGER, MikroOrmQueryLogger } from "../common/providers/mikro-orm-logger";
|
|
|
|
export const databaseConfig: MikroOrmModuleAsyncOptions = {
|
|
inject: [ConfigService, MIKRO_ORM_QUERY_LOGGER],
|
|
providers: [MikroOrmQueryLogger],
|
|
useFactory: (configService: ConfigService, logger: Logger) => {
|
|
const DB_PASS = configService.getOrThrow<string>("DB_PASS");
|
|
const DB_USER = configService.getOrThrow<string>("DB_USER");
|
|
const DB_HOST = configService.getOrThrow<string>("DB_HOST");
|
|
const DB_PORT = configService.getOrThrow<number>("DB_PORT");
|
|
const encodedPassword = encodeURIComponent(DB_PASS);
|
|
|
|
return {
|
|
driver: PostgreSqlDriver,
|
|
autoLoadEntities: true,
|
|
dbName: configService.getOrThrow<string>("DB_NAME"),
|
|
debug: configService.get<string>("NODE_ENV") !== "production",
|
|
clientUrl: `postgres://${DB_USER}:${encodedPassword}@${DB_HOST}:${DB_PORT}`,
|
|
ensureDatabase: { forceCheck: true, create: true, schema: "update" },
|
|
forceUtcTimezone: true,
|
|
pool: {
|
|
min: 2,
|
|
max: 15,
|
|
idleTimeoutMillis: 10000,
|
|
acquireTimeoutMillis: 10000,
|
|
reapIntervalMillis: 1000,
|
|
createTimeoutMillis: 3000,
|
|
destroyTimeoutMillis: 5000,
|
|
},
|
|
logger: (message) => logger.debug(message),
|
|
schemaGenerator: {
|
|
createForeignKey: true,
|
|
disableForeignKeys: false,
|
|
createIndex: true,
|
|
},
|
|
migrations: {
|
|
path: "./database/migrations",
|
|
pathTs: "./database/migrations",
|
|
tableName: "migrations",
|
|
transactional: true,
|
|
allOrNothing: true,
|
|
dropTables: false,
|
|
safe: true,
|
|
emit: "ts",
|
|
},
|
|
};
|
|
},
|
|
};
|