mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 18:13:26 +00:00
add basic repo setup, with user creation
This commit is contained in:
44
src/modules/mikro/index.ts
Normal file
44
src/modules/mikro/index.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { conf } from '@/config';
|
||||
import { scopedLogger } from '@/services/logger';
|
||||
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import { createORM } from './orm';
|
||||
|
||||
const log = scopedLogger('orm');
|
||||
let orm: MikroORM<PostgreSqlDriver> | null = null;
|
||||
|
||||
export function getORM() {
|
||||
if (!orm) throw new Error('ORM not set');
|
||||
return orm;
|
||||
}
|
||||
|
||||
export async function setupMikroORM() {
|
||||
log.info(`Connecting to postgres`, { evt: 'connecting' });
|
||||
const mikro = await createORM(conf.postgres.connection, (msg) =>
|
||||
log.info(msg),
|
||||
);
|
||||
|
||||
if (conf.postgres.syncSchema) {
|
||||
const generator = mikro.getSchemaGenerator();
|
||||
try {
|
||||
await generator.updateSchema();
|
||||
} catch {
|
||||
try {
|
||||
await generator.clearDatabase();
|
||||
await generator.updateSchema();
|
||||
} catch {
|
||||
await generator.clearDatabase();
|
||||
await generator.dropSchema();
|
||||
await generator.updateSchema();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (conf.postgres.migrateOnBoot) {
|
||||
const migrator = mikro.getMigrator();
|
||||
await migrator.up();
|
||||
}
|
||||
|
||||
orm = mikro;
|
||||
log.info(`Connected to postgres - ORM is setup!`, { evt: 'success' });
|
||||
}
|
Reference in New Issue
Block a user