11# kysely-deno-postgres-dialect
22
33[ ![ ci] ( https://github.com/Byzanteam/kysely_deno_postgres_dialect/actions/workflows/ci.yml/badge.svg )] ( https://github.com/Byzanteam/kysely_deno_postgres_dialect/actions/workflows/ci.yml )
4- [ ![ Latest version ] ( https://deno.land/badge/kysely_deno_postgres_dialect/version )] ( https://deno.land/x/kysely_deno_postgres_dialect )
4+ [ ![ JSR ] ( https://jsr.io/badges/@byzanteam/kysely-deno-postgres-dialect )] ( https://jsr.io/@byzanteam/kysely-deno-postgres-dialect )
55
66[ Kysely] ( https://github.com/kysely-org/kysely ) dialect for PostgreSQL using the
77[ postgresjs] ( https://github.com/porsager/postgres ) client.
88
99## 🚀 Getting started
1010
11- Import using ` imports ` in ` deno.json `
11+ ### Install dependencies
12+
13+ ```
14+ deno add @byzanteam/kysely-deno-postgres-dialect
15+ ```
16+
17+ Optional: Import using ` imports ` in ` deno.json ` , if you dont want to use ` npm `
18+ registry.
1219
1320``` json
1421{
1522 "imports" : {
16- "kysely-deno-postgres-dialect/ " : " https:// deno.land/x/kysely_postgrs_js_dialect/ " ,
23+ "kysely-deno-postgres-dialect" : " jsr:@byzanteam/kysely- deno-postgres-dialect " ,
1724 "postgresjs" : " https://deno.land/x/postgresjs@v3.4.4/mod.js" ,
1825 "kysely" : " https://esm.sh/kysely@0.27.3"
1926 }
2027}
2128```
2229
23- use kysely-deno-postgres-dialect
30+ ### Use kysely-deno-postgres-dialect
31+
32+ 1 . Define your database schema
2433
2534``` typescript
35+ import postgres from " postgresjs/mod.js" ;
36+ import { type Generated , Kysely } from " kysely" ;
2637import {
2738 PostgresJSDialect ,
2839 setup ,
2940 wrapTransaction as wrapTransactionFn ,
30- } from " kysely-deno-postgres-dialect/mod.ts" ;
31- import postgres from " postgresjs/mod.js" ;
41+ } from " kysely-deno-postgres-dialect" ;
42+
43+ interface UserTable {
44+ id: Generated <number >;
45+ name: string ;
46+ }
47+
48+ interface Database {
49+ users: UserTable ;
50+ }
51+ ```
52+
53+ 2 . Setup the kysely instance before using it
3254
55+ ``` typescript
3356setup (() => {
3457 const dialect = new PostgresJSDialect ({
3558 postgres: postgres (
@@ -44,81 +67,90 @@ setup(() => {
4467 ),
4568 });
4669
47- return new Kysely <Database >({ // Database is defined by Kysely orm
70+ return new Kysely <Database >({
4871 dialect ,
4972 });
5073});
74+ ```
5175
76+ 3 . Make your own ` wrapTransaction ` function that incorporates a database context
77+
78+ ``` typescript
5279async function wrapTransaction<T >(
5380 callback : Parameters <typeof wrapTransactionFn <Database , T >>[0 ],
5481): Promise <T > {
5582 return await wrapTransactionFn <Database , T >(callback );
5683}
84+ ```
5785
58- const data = await wrapTransaction (async (trx ) => {
59- return trx .selectFrom (" hello" ).selectAll ().execute ();
86+ 4 . Use the ` wrapTransaction ` function to execute queries
87+
88+ ``` typescript
89+ const users = await wrapTransaction (async (trx ) => {
90+ return await trx .selectFrom (" users" ).selectAll ().execute ();
6091});
6192```
6293
6394## 🩺 Testing
6495
65- See detail at ` ./tests/testing/utils_test.ts ` .
96+ > [ !TIP] \
97+ > See examples at ` ./tests/testing/utils_test.ts ` .
6698
67- > [ !IMPORTANT] \
68- > To fix ` leaking resources ` error, you should end all connections between
69- > cases.
99+ ` setupTesting ` function is provided to set up the testing environment. It stubs
100+ transaction functions, and each test runs in a transaction that is rolled back
101+ after the test. Theoretically, tests are isolated from each other, and can be
102+ run in parallel.
70103
71- ``` typescript
72- import {
73- PostgresJSDialect ,
74- setup ,
75- wrapTransaction ,
76- } from " https://deno.land/x/kysely_deno_postgres_dialect/mod.ts" ;
77- import { setupTesting } from " https://deno.land/x/kysely_deno_postgres_dialect/testing.ts" ;
104+ 1 . Add a helper function to setup the testing database
78105
79- setup (() => {
80- const dialect = new PostgresJSDialect ({
81- postgres: postgres (
82- {
83- database: " postgres" ,
84- hostname: " localhost" ,
85- password: " postgres" ,
86- port: 5432 ,
87- user: " postgres" ,
88- max: 10 ,
89- },
90- ),
106+ ``` typescript
107+ // test_helper.ts
108+
109+ import { PostgresJSDialect , setup } from " kysely-deno-postgres-dialect" ;
110+ import { Kysely } from " kysely" ;
111+ import postgres from " postgresjs" ;
112+
113+ export function setupTestingDB() {
114+ setup (() => {
115+ const dialect = new PostgresJSDialect ({
116+ postgres: postgres (
117+ {
118+ database: " postgres" ,
119+ hostname: " localhost" ,
120+ password: " postgres" ,
121+ port: 5432 ,
122+ user: " postgres" ,
123+ max: 10 ,
124+ },
125+ ),
126+ });
127+
128+ return new Kysely <Database >({
129+ dialect ,
130+ });
91131 });
132+ }
133+ ```
92134
93- return new Kysely <Database >({
94- dialect ,
95- });
96- });
135+ 2 . Setup the testing environment in the test files, and write tests
97136
137+ ``` typescript
98138// test files
99139
100- const {
101- beforeAllFn,
102- beforeEachFn,
103- afterEachFn,
104- afterAllFn,
105- } = setupTesting (stub );
140+ import { setupTesting } from " kysely-deno-postgres-dialect/testing" ;
141+ import { stub } from " jsr:@std/testing/mock" ;
142+ import {
143+ afterAll ,
144+ afterEach ,
145+ beforeAll ,
146+ beforeEach ,
147+ describe ,
148+ it ,
149+ } from " jsr:@std/testing/bdd" ;
106150
107- describe (" tests" , () => {
108- beforeAll (async () => {
109- await beforeAllFn ();
110- });
111- afterAll (async () => {
112- await afterAllFn ();
113- });
114- beforeEach (async () => {
115- await beforeEachFn ();
116- });
117- afterEach (async () => {
118- // note: fix Leaking resources error
119- await afterEachFn ();
120- });
151+ setupTesting ({ stub , beforeEach , afterEach , beforeAll , afterAll });
121152
153+ describe (" this is a description" , () => {
122154 it (" works" , async () => {
123155 // snip
124156 });
0 commit comments