mirror of
https://github.com/barthuijgen/factorio-sites.git
synced 2024-11-21 18:16:33 +02:00
fix: start database from env directly
This commit is contained in:
parent
27a9d633b2
commit
3036bf2b68
@ -1,50 +1,13 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { getEnvOrThrow, getSecretOrThrow } from "../env.util";
|
||||
import { getEnvOrThrow } from "../env.util";
|
||||
|
||||
const globalWithPrisma = globalThis as typeof globalThis & { prisma: PrismaClient };
|
||||
const url = getEnvOrThrow("DATABASE_URL");
|
||||
|
||||
export let prisma: PrismaClient;
|
||||
|
||||
const _init = async () => {
|
||||
if (globalWithPrisma.prisma) return globalWithPrisma.prisma;
|
||||
|
||||
const host = getEnvOrThrow("POSTGRES_HOST");
|
||||
const port = host.includes(":") ? "" : ":5432";
|
||||
const user = getEnvOrThrow("POSTGRES_USER");
|
||||
const database = getEnvOrThrow("POSTGRES_DB");
|
||||
const passwordKey = getEnvOrThrow("POSTGRES_PASSWORD");
|
||||
const password = passwordKey.startsWith("projects/")
|
||||
? await getSecretOrThrow(passwordKey)
|
||||
: passwordKey;
|
||||
|
||||
const prismaClient = new PrismaClient({
|
||||
log: ["warn", "error"],
|
||||
datasources: {
|
||||
db: {
|
||||
url: `postgresql://${user}:${password}@${host}${port}/${database}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await prismaClient.$connect();
|
||||
|
||||
return prismaClient;
|
||||
};
|
||||
|
||||
const promise = _init()
|
||||
.then((result) => {
|
||||
prisma = result;
|
||||
if (!globalWithPrisma.prisma) {
|
||||
globalWithPrisma.prisma = prisma;
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.catch((reason) => {
|
||||
if (process.env.JEST_WORKER_ID) return;
|
||||
console.log("Database failed to init!", reason);
|
||||
});
|
||||
export const prisma = new PrismaClient({
|
||||
log: ["warn", "error"],
|
||||
datasources: { db: { url } },
|
||||
});
|
||||
|
||||
export const init = async () => {
|
||||
await promise;
|
||||
await prisma.$connect();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user