Files
zipline/src/lib/db/index.ts
2023-07-20 12:35:08 -07:00

45 lines
916 B
TypeScript

import { log } from '@/lib/logger';
import { Prisma, PrismaClient } from '@prisma/client';
import { userViewSchema } from './models/user';
let prisma: ExtendedPrismaClient;
declare global {
var __db__: ExtendedPrismaClient;
}
if (process.env.NODE_ENV === 'production') {
prisma = getClient();
} else {
if (!global.__db__) {
global.__db__ = getClient();
}
prisma = global.__db__;
}
type ExtendedPrismaClient = ReturnType<typeof getClient>;
function getClient() {
const logger = log('db');
logger.info('connecting to database ' + process.env.DATABASE_URL);
const client = new PrismaClient().$extends({
result: {
user: {
view: {
needs: { view: true },
compute({ view }: { view: Prisma.JsonValue }) {
return userViewSchema.parse(view);
},
},
},
},
});
client.$connect();
return client;
}
export { prisma };