@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
44 lines (33 loc) • 1.43 kB
text/typescript
import { Pool as NeonPool, neonConfig } from '@neondatabase/serverless';
import { drizzle as neonDrizzle } from 'drizzle-orm/neon-serverless';
import { drizzle as nodeDrizzle } from 'drizzle-orm/node-postgres';
import { Pool as NodePool } from 'pg';
import ws from 'ws';
import { serverDBEnv } from '@/config/db';
import { isServerMode } from '@/const/version';
import * as schema from '@/database/schemas';
import { LobeChatDatabase } from '../type';
export const getDBInstance = (): LobeChatDatabase => {
if (!isServerMode) return {} as any;
if (!serverDBEnv.KEY_VAULTS_SECRET) {
throw new Error(
` \`KEY_VAULTS_SECRET\` is not set, please set it in your environment variables.
If you don't have it, please run \`openssl rand -base64 32\` to create one.
`,
);
}
let connectionString = serverDBEnv.DATABASE_URL;
if (!connectionString) {
throw new Error(`You are try to use database, but "DATABASE_URL" is not set correctly`);
}
if (serverDBEnv.DATABASE_DRIVER === 'node') {
const client = new NodePool({ connectionString });
return nodeDrizzle(client, { schema });
}
if (process.env.MIGRATION_DB === '1') {
// https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined
neonConfig.webSocketConstructor = ws;
}
const client = new NeonPool({ connectionString });
return neonDrizzle(client, { schema });
};