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:
@@ -1,6 +1,6 @@
|
||||
import { helloRouter } from '@/routes/hello';
|
||||
import { manageAuthRouter } from '@/routes/auth/manage';
|
||||
import { FastifyInstance } from 'fastify';
|
||||
|
||||
export async function setupRoutes(app: FastifyInstance) {
|
||||
app.register(helloRouter);
|
||||
app.register(manageAuthRouter.register);
|
||||
}
|
||||
|
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' });
|
||||
}
|
17
src/modules/mikro/orm.ts
Normal file
17
src/modules/mikro/orm.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { MikroORM, PostgreSqlDriver } from '@mikro-orm/postgresql';
|
||||
import path from 'path';
|
||||
|
||||
export async function createORM(url: string, log: (msg: string) => void) {
|
||||
return await MikroORM.init<PostgreSqlDriver>({
|
||||
type: 'postgresql',
|
||||
clientUrl: url,
|
||||
entities: ['./models/**/*.js'],
|
||||
entitiesTs: ['./models/**/*.ts'],
|
||||
baseDir: path.join(__dirname, '../../db'),
|
||||
migrations: {
|
||||
pathTs: './migrations/**/*.ts',
|
||||
path: './migrations/**/*.ts',
|
||||
},
|
||||
logger: log,
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user