🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { drizzle as nodeDrizzle } from 'drizzle-orm/node-postgres';
import { migrate as nodeMigrate } from 'drizzle-orm/node-postgres/migrator';
import { drizzle as pgliteDrizzle } from 'drizzle-orm/pglite';
import { migrate as pgliteMigrate } from 'drizzle-orm/pglite/migrator';
import { Pool as NodePool } from 'pg';
import { serverDBEnv } from '@/config/db';
import * as schema from '../schemas';
import type { LobeChatDatabase } from '../type';
import { createPGlite, migrationsFolder, readPGliteSnapshot } from './pgliteSnapshot';
const isServerDBMode = process.env.TEST_SERVER_DB === '1';
let testClientDB: ReturnType<typeof pgliteDrizzle<typeof schema>> | null = null;
let testServerDB: ReturnType<typeof nodeDrizzle<typeof schema>> | null = null;
export const getTestDB = async (): Promise<LobeChatDatabase> => {
// Server DB mode (node-postgres)
if (isServerDBMode) {
if (testServerDB) return testServerDB as unknown as LobeChatDatabase;
const connectionString = serverDBEnv.DATABASE_TEST_URL;
if (!connectionString) {
throw new Error('DATABASE_TEST_URL is not set');
}
const client = new NodePool({ connectionString });
testServerDB = nodeDrizzle(client, { schema });
await nodeMigrate(testServerDB, { migrationsFolder });
return testServerDB as unknown as LobeChatDatabase;
}
// Client DB mode (PGlite)
if (testClientDB) return testClientDB as unknown as LobeChatDatabase;
const snapshot = readPGliteSnapshot();
const pglite = createPGlite(snapshot);
testClientDB = pgliteDrizzle({ client: pglite, schema });
// The global setup's snapshot is already migrated; without it, migrate here.
// pg_search migrations skip themselves when the extension is unavailable, as in PGlite.
if (!snapshot) await pgliteMigrate(testClientDB, { migrationsFolder });
return testClientDB as unknown as LobeChatDatabase;
};