mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Code cleanup, add color and pretty dates to LogManagers
This commit is contained in:
parent
327a716b12
commit
bc051ed043
3 changed files with 103 additions and 33 deletions
|
|
@ -2,6 +2,12 @@ import { config } from "config-manager";
|
|||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { Client } from "pg";
|
||||
import * as schema from "./schema";
|
||||
import {
|
||||
LogLevel,
|
||||
type LogManager,
|
||||
type MultiLogManager,
|
||||
} from "~packages/log-manager";
|
||||
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
||||
|
||||
export const client = new Client({
|
||||
host: config.database.host,
|
||||
|
|
@ -11,4 +17,38 @@ export const client = new Client({
|
|||
database: config.database.database,
|
||||
});
|
||||
|
||||
export const setupDatabase = async (logger: LogManager | MultiLogManager) => {
|
||||
try {
|
||||
await client.connect();
|
||||
} catch (e) {
|
||||
await logger.logError(LogLevel.CRITICAL, "Database", e as Error);
|
||||
|
||||
await logger.log(
|
||||
LogLevel.CRITICAL,
|
||||
"Database",
|
||||
"Failed to connect to database. Please check your configuration.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Migrate the database
|
||||
await logger.log(LogLevel.INFO, "Database", "Migrating database...");
|
||||
|
||||
try {
|
||||
await migrate(db, {
|
||||
migrationsFolder: "./drizzle",
|
||||
});
|
||||
} catch (e) {
|
||||
await logger.logError(LogLevel.CRITICAL, "Database", e as Error);
|
||||
await logger.log(
|
||||
LogLevel.CRITICAL,
|
||||
"Database",
|
||||
"Failed to migrate database. Please check your configuration.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await logger.log(LogLevel.INFO, "Database", "Database migrated");
|
||||
};
|
||||
|
||||
export const db = drizzle(client, { schema });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue