customer/test/helpers/db.ts
Peppa 4acc7f5ea6
All checks were successful
peppa/lint lint: success за 0с
peppa/typecheck typecheck: success за 1с
peppa/test test: success за 14с
peppa/build build: success за 1с
ci / checks (pull_request) Successful in 25s
deploy / deploy (pull_request) Successful in 3s
CLT-2: Postgres, миграции, учётные записи, сессии, CSRF, rate-limit, удаление аккаунта
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:24:05 +05:00

29 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PGlite } from "@electric-sql/pglite";
import type { Db, TxDb } from "../../src/db/db.ts";
import { runMigrations } from "../../src/db/migrate.ts";
/** Настоящий Postgres (WASM) в памяти: те же миграции, что на проде. */
export async function createTestDb(migrate = true): Promise<TxDb & { pg: PGlite }> {
const pg = new PGlite();
const wrap = (c: { query: PGlite["query"]; exec: PGlite["exec"] }): Db => ({
query: async (sql, params) => {
// Без параметров допустим многооператорный SQL (миграции), как у настоящего pg.
if (!params || params.length === 0) {
const rs = await c.exec(sql);
const last = rs[rs.length - 1];
return { rows: (last?.rows ?? []) as never[], rowCount: last?.affectedRows ?? last?.rows.length ?? 0 };
}
const r = await c.query(sql, params as unknown[]);
return { rows: r.rows as never[], rowCount: r.affectedRows ?? r.rows.length };
},
});
const db: TxDb & { pg: PGlite } = {
pg,
...wrap(pg),
tx: (fn) => pg.transaction((t) => fn(wrap(t as never))),
close: () => pg.close(),
};
if (migrate) await runMigrations(db);
return db;
}