https://github.com/byzanteam/kysely_postgres_js_dialect
Kysely dialect for PostgreSQL using the Postgres.js client.
https://github.com/byzanteam/kysely_postgres_js_dialect
jet
Last synced: 3 months ago
JSON representation
Kysely dialect for PostgreSQL using the Postgres.js client.
- Host: GitHub
- URL: https://github.com/byzanteam/kysely_postgres_js_dialect
- Owner: Byzanteam
- License: mit
- Created: 2023-12-22T10:13:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-30T05:07:34.000Z (over 1 year ago)
- Last Synced: 2025-11-27T12:28:08.533Z (6 months ago)
- Topics: jet
- Language: TypeScript
- Homepage: https://jsr.io/@byzanteam/kysely-deno-postgres-dialect
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kysely-deno-postgres-dialect
[](https://github.com/Byzanteam/kysely_deno_postgres_dialect/actions/workflows/ci.yml)
[](https://jsr.io/@byzanteam/kysely-deno-postgres-dialect)
[Kysely](https://github.com/kysely-org/kysely) dialect for PostgreSQL using the
[postgresjs](https://github.com/porsager/postgres) client.
## 🚀 Getting started
### Install dependencies
```
deno add @byzanteam/kysely-deno-postgres-dialect
```
Optional: Import using `imports` in `deno.json`, if you dont want to use `npm`
registry.
```json
{
"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"
}
}
```
### Use kysely-deno-postgres-dialect
1. Define your database schema
```typescript
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;
name: string;
}
interface Database {
users: UserTable;
}
```
2. Setup the kysely instance before using it
```typescript
setup(() => {
const dialect = new PostgresJSDialect({
postgres: postgres(
{
database: "postgres",
hostname: "localhost",
password: "postgres",
port: 5432,
user: "postgres",
max: 10,
},
),
});
return new Kysely({
dialect,
});
});
```
3. Make your own `wrapTransaction` function that incorporates a database context
```typescript
async function wrapTransaction(
callback: Parameters>[0],
): Promise {
return await wrapTransactionFn(callback);
}
```
4. Use the `wrapTransaction` function to execute queries
```typescript
const users = await wrapTransaction(async (trx) => {
return await trx.selectFrom("users").selectAll().execute();
});
```
## 🩺 Testing
> [!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.
1. Add a helper function to setup the testing database
```typescript
// 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({
dialect,
});
});
}
```
2. Setup the testing environment in the test files, and write tests
```typescript
// 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
});
});
```