First commit
This commit is contained in:
Executable
+33
@@ -0,0 +1,33 @@
|
||||
import type { FastifyPluginAsync } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import { MikroORM } from "@mikro-orm/postgresql";
|
||||
import config from "@/mikro-orm.config";
|
||||
import logger from "@/lib/logger";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyInstance {
|
||||
orm: MikroORM;
|
||||
}
|
||||
}
|
||||
|
||||
const Database: FastifyPluginAsync = async (fastify) => {
|
||||
const orm = await MikroORM.init(config);
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
try {
|
||||
await orm.schema.updateSchema();
|
||||
logger.info("Database migration completed.");
|
||||
} catch (err) {
|
||||
logger.error("Migration failed:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fastify.decorate("orm", orm);
|
||||
|
||||
fastify.addHook("onClose", async () => {
|
||||
await orm.close(true);
|
||||
});
|
||||
};
|
||||
|
||||
export default fp(Database);
|
||||
Reference in New Issue
Block a user