34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { vi, beforeAll , afterAll, beforeEach, afterEach } from "vitest";
|
|
import { drizzle, type PgliteDatabase } from 'drizzle-orm/pglite';
|
|
import * as schema from "~/server/dbschema/schema"
|
|
import type * as DrizzleKit from 'drizzle-kit/api';
|
|
import { createRequire } from 'node:module';
|
|
import { PGlite } from "@electric-sql/pglite"
|
|
|
|
let clients: Record<string,PGlite> = {};
|
|
export let db: PgliteDatabase<typeof schema>
|
|
// beforeEach(async (test) => {
|
|
const client = new PGlite()
|
|
// workaround for https://github.com/drizzle-team/drizzle-orm/issues/2853
|
|
const require = createRequire(import.meta.url);
|
|
const { generateDrizzleJson, generateMigration } = require('drizzle-kit/api') as typeof DrizzleKit;
|
|
// end of workaround
|
|
|
|
// workaround for https://github.com/drizzle-team/drizzle-orm/issues/3913
|
|
async function pushSchema(db: PgliteDatabase<typeof schema>) {
|
|
const prevJson = generateDrizzleJson({});
|
|
const curJson = generateDrizzleJson(schema, prevJson.id, undefined, 'snake_case');
|
|
const statements = await generateMigration(prevJson, curJson);
|
|
for (const statement of statements) {
|
|
let res = await db.execute(statement);
|
|
}
|
|
}
|
|
db = drizzle({client:client,schema:schema,casing:'snake_case'});
|
|
await pushSchema(db);
|
|
// })
|
|
|
|
// afterEach(async (test) => {
|
|
// const client = clients[test.task.id]
|
|
// await client!.close()
|
|
// })
|