Kysely dialect for PostgreSQL using the postgresjs client.
deno add @byzanteam/kysely-deno-postgres-dialect
Optional: Import using imports
in deno.json
, if you dont want to use npm
registry.
{
"imports": {
"kysely-deno-postgres-dialect": "jsr:@byzanteam/kysely-deno-postgres-dialect",
"postgresjs": "https://deno.land/x/postgresjs@v3.4.4/mod.js",
"kysely": "https://esm.sh/kysely@0.27.4"
}
}
import postgres from "postgresjs/mod.js";
import { type Generated, Kysely } from "kysely";
import {
PostgresJSDialect,
setup,
wrapTransaction as wrapTransactionFn,
} from "kysely-deno-postgres-dialect";
interface UserTable {
id: Generated<number>;
name: string;
}
interface Database {
users: UserTable;
}
setup(() => {
const dialect = new PostgresJSDialect({
postgres: postgres(
{
database: "postgres",
hostname: "localhost",
password: "postgres",
port: 5432,
user: "postgres",
max: 10,
},
),
});
return new Kysely<Database>({
dialect,
});
});
wrapTransaction
function that incorporates a database contextasync function wrapTransaction<T>(
callback: Parameters<typeof wrapTransactionFn<Database, T>>[0],
): Promise<T> {
return await wrapTransactionFn<Database, T>(callback);
}
wrapTransaction
function to execute queriesconst users = await wrapTransaction(async (trx) => {
return await trx.selectFrom("users").selectAll().execute();
});
[!TIP]\ See examples at
./tests/testing/utils_test.ts
.
setupTesting
function is provided to set up the testing environment. It stubs
transaction functions, and each test runs in a transaction that is rolled back
after the test. Theoretically, tests are isolated from each other, and can be
run in parallel.
// test_helper.ts
import { PostgresJSDialect, setup } from "kysely-deno-postgres-dialect";
import { Kysely } from "kysely";
import postgres from "postgresjs";
export function setupTestingDB() {
setup(() => {
const dialect = new PostgresJSDialect({
postgres: postgres(
{
database: "postgres",
hostname: "localhost",
password: "postgres",
port: 5432,
user: "postgres",
max: 10,
},
),
});
return new Kysely<Database>({
dialect,
});
});
}
// test files
import { setupTesting } from "kysely-deno-postgres-dialect/testing";
import { stub } from "jsr:@std/testing/mock";
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
it,
} from "jsr:@std/testing/bdd";
setupTesting({ stub, beforeEach, afterEach, beforeAll, afterAll });
describe("this is a description", () => {
it("works", async () => {
// snip
});
});