import { ColumnType, Kysely } from "kysely"; //#region src/vitest/pglite-kysely.d.ts /** Result of a {@link PGliteClient.query} call. */ export interface PGliteQueryResult { /** Rows returned by the statement. */ rows: unknown[]; /** Number of rows an INSERT/UPDATE/DELETE touched. */ affectedRows?: number; /** Postgres command tag of the statement (`"SELECT"`, `"INSERT"`, ...). */ command?: string; /** Row count reported alongside the command tag. */ rowCount?: number; } /** * The subset of a `@electric-sql/pglite` `PGlite` instance used by * {@link createKyselyPGlite}. Any client with a compatible `query`/`close` * pair works. */ export interface PGliteClient { /** Run a single SQL statement with positional (`$1`, `$2`, ...) parameters. */ query(query: string, params?: unknown[]): Promise; /** Release the underlying database. Called by `db.destroy()`. */ close(): Promise; } type WritableAs = [S] extends [W] ? W : [W] extends [S] ? S : W | Exclude; type UnmigratedColumn = C extends ColumnType ? ColumnType, WritableAs> : C; /** * `DB` as its rows stand before the migration script has run: every column * accepts on insert and update whatever it can still hold on read. * * The generated `db.ts` types a column the migration makes required as * `ColumnType`, and an enum whose values it narrows as * `ColumnType`, so `migrate.ts` cannot write a null * or a removed value into them — and neither can a test that has to stage * the rows the script converts. Type the PGlite instance with * `Unmigrated` to stage them; `main` still receives a * `Transaction`. * @example * ```typescript * const db = createKyselyPGlite>(new PGlite()); * await db.insertInto("User").values({ name: "a", email: null }).execute(); * await db.transaction().execute((trx) => main(trx)); * ``` */ export type Unmigrated = { [T in keyof DB]: { [C in keyof DB[T]]: UnmigratedColumn; }; }; /** * Create a Kysely instance backed by a PGlite in-memory Postgres, for * executing a migration script's queries against real data in tests. * Pass the migration's schema as the type argument — wrapped in * {@link Unmigrated} so the test can stage the rows the script has not yet * backfilled: `createKyselyPGlite>(new PGlite())`. * * PGlite runs full PostgreSQL while TailorDB supports a subset of it, so a * statement passing here can still be rejected by the platform; keep a * statement-level test (see `createKyselyMock`) alongside. * @param client - A `PGlite` instance from `@electric-sql/pglite` * @returns A Kysely instance that executes queries on the client and closes it on `destroy()` * @example * ```typescript * // migrations/0005/migrate.pglite.test.ts * import { PGlite } from "@electric-sql/pglite"; * import { createKyselyPGlite, type Unmigrated } from "@tailor-platform/sdk/vitest"; * import type { Database } from "./db"; * import { main } from "./migrate"; * * const db = createKyselyPGlite>(new PGlite()); * // create tables matching db.ts, insert rows, then: * await db.transaction().execute((trx) => main(trx)); * ``` */ export declare function createKyselyPGlite>(client: PGliteClient): Kysely; //#endregion