Skip to content

Commit 529778b

Browse files
authored
refactor: accept postgres instance. (#7)
* accept postgres instance. * bump kysely. * fix test. * update readme.
1 parent 824a2e6 commit 529778b

File tree

8 files changed

+37
-100
lines changed

8 files changed

+37
-100
lines changed

README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![Powered by TypeScript](https://img.shields.io/badge/powered%20by-typescript-blue.svg)
44

5-
[Kysely](https://github.com/koskimas/kysely) dialect for [PostgreSQL](https://www.postgresql.org/) using the [Postgres.js](https://github.com/porsager/postgres) client library under the hood.
5+
[Kysely](https://github.com/koskimas/kysely) dialect for [PostgreSQL](https://www.postgresql.org/) using the [Postgres.js](https://github.com/porsager/postgres) client library under the hood (version >= 3.4).
66

77
This dialect should not be confused with Kysely's built-in PostgreSQL dialect, which uses the [pg](https://github.com/brianc/node-postgres) client library instead.
88

@@ -41,8 +41,8 @@ To fix that, add an [`import_map.json`](https://deno.land/[email protected]/linking
4141
```json
4242
{
4343
"imports": {
44-
"kysely": "https://cdn.jsdelivr.net/npm/kysely@0.23.5/dist/esm/index.js",
45-
"postgres": "https://deno.land/x/postgresjs@v3.3.4/mod.js"
44+
"kysely": "https://cdn.jsdelivr.net/npm/kysely@0.26.3/dist/esm/index.js",
45+
"postgres": "https://deno.land/x/postgresjs@v3.4.0/mod.js"
4646
}
4747
}
4848
```
@@ -65,26 +65,13 @@ interface Database {
6565

6666
const db = new Kysely<Database>({
6767
dialect: new PostgresJSDialect({
68-
connectionString: 'postgres://admin@localhost:5434/test',
69-
options: {
70-
max: 10,
71-
},
72-
postgres,
73-
}),
74-
})
75-
76-
// or...
77-
78-
const db = new Kysely<Database>({
79-
dialect: new PostgresJSDialect({
80-
options: {
68+
postgres: postgres({
8169
database: 'test',
8270
host: 'localhost',
8371
max: 10,
8472
port: 5434,
8573
user: 'admin',
86-
},
87-
postgres,
74+
}),
8875
}),
8976
})
9077
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"eslint-config-prettier": "^8.8.0",
5757
"eslint-plugin-import": "^2.27.5",
5858
"eslint-plugin-prettier": "^4.2.1",
59-
"kysely": "^0.24.2",
59+
"kysely": "^0.26.3",
6060
"mocha": "^10.2.0",
6161
"mocha-each": "^2.0.1",
6262
"postgres": "^3.4.1",

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/driver.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import type {Driver, TransactionSettings} from 'kysely'
2-
import type {Sql} from 'postgres'
32

43
import {PostgresJSConnection} from './connection.js'
54
import type {PostgresJSDialectConfig} from './types.js'
6-
import {createPostgres, freeze} from './utils.js'
5+
import {freeze} from './utils.js'
76

87
export class PostgresJSDriver implements Driver {
98
readonly #config: PostgresJSDialectConfig
10-
readonly #sql: Sql
119

1210
constructor(config: PostgresJSDialectConfig) {
1311
this.#config = freeze({...config})
14-
15-
this.#sql = createPostgres(this.#config)
1612
}
1713

1814
async init(): Promise<void> {
1915
// noop
2016
}
2117

2218
async acquireConnection(): Promise<PostgresJSConnection> {
23-
const reservedConnection = await (this.#sql as any).reserve()
19+
const reservedConnection = await this.#config.postgres.reserve()
2420

2521
return new PostgresJSConnection(reservedConnection)
2622
}
@@ -42,6 +38,6 @@ export class PostgresJSDriver implements Driver {
4238
}
4339

4440
async destroy(): Promise<void> {
45-
await this.#sql.end()
41+
await this.#config.postgres.end()
4642
}
4743
}

src/types.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import type postgres from 'postgres'
2-
import type {Options} from 'postgres'
1+
import type {Sql} from 'postgres'
32

4-
export type PostgresJSDialectConfig =
5-
| {
6-
connectionString: string
7-
options?: Options<any>
8-
postgres: typeof postgres
9-
}
10-
| {
11-
options: Options<any>
12-
postgres: typeof postgres
13-
}
3+
export interface PostgresJSDialectConfig {
4+
readonly postgres: Sql
5+
}

src/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
import type {Sql} from 'postgres'
2-
3-
import type {PostgresJSDialectConfig} from './types'
4-
51
export function freeze<T>(obj: T): Readonly<T> {
62
return Object.freeze(obj)
73
}
8-
9-
export function createPostgres(config: PostgresJSDialectConfig): Sql {
10-
return 'connectionString' in config
11-
? config.postgres(config.connectionString, config.options)
12-
: config.postgres(config.options)
13-
}

tests/nodejs/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import {CompiledQuery, DeleteResult, InsertResult, UpdateResult, sql, type Kysely, type Transaction} from 'kysely'
22

33
import {
4-
CONFIGS,
54
DEFAULT_DATA_SET,
65
POOL_SIZE,
7-
TestConfig,
86
clearDatabase,
97
destroyTest,
108
expect,
11-
forEach,
129
initTest,
1310
insertDefaultDataSet,
1411
testSql,
@@ -17,12 +14,12 @@ import {
1714
type TestContext,
1815
} from './test-setup'
1916

20-
forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => {
17+
describe('PostgresJSDialect: %s', () => {
2118
let ctx: TestContext
2219
const executedQueries: CompiledQuery[] = []
2320

2421
before(async function () {
25-
ctx = await initTest(this, config.config, (event) => {
22+
ctx = await initTest(this, (event) => {
2623
if (event.level === 'query') {
2724
executedQueries.push(event.query)
2825
}
@@ -380,7 +377,9 @@ forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => {
380377
})
381378

382379
it('should delete two rows', async () => {
383-
const query = ctx.db.deleteFrom('person').where('first_name', '=', 'Jennifer').orWhere('first_name', '=', 'Arnold')
380+
const query = ctx.db
381+
.deleteFrom('person')
382+
.where((eb) => eb('first_name', '=', 'Jennifer').or('first_name', '=', 'Arnold'))
384383

385384
const result = await query.executeTakeFirst()
386385

tests/nodejs/test-setup.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ interface PetInsertParams extends Omit<Pet, 'id' | 'owner_id'> {
5858
}
5959

6060
export interface TestContext {
61-
config: KyselyConfig
6261
db: Kysely<Database>
6362
}
6463

@@ -68,56 +67,30 @@ export const PLUGINS: KyselyPlugin[] = []
6867

6968
export const POOL_SIZE = 20
7069

71-
const DATABASE = 'test'
72-
const HOST = 'localhost'
73-
const PORT = 5434
74-
const USER = 'admin'
75-
76-
export const CONFIGS: {config: KyselyConfig; toString: () => string}[] = [
77-
{
78-
config: {
79-
dialect: new PostgresJSDialect({
80-
connectionString: `postgres://${USER}@${HOST}:${PORT}/${DATABASE}`,
81-
options: {
82-
max: POOL_SIZE,
83-
onnotice() {},
84-
},
85-
postgres,
86-
}),
87-
},
88-
toString: () => 'connection-string',
89-
},
90-
{
91-
config: {
92-
dialect: new PostgresJSDialect({
93-
options: {
94-
database: DATABASE,
95-
host: HOST,
96-
max: POOL_SIZE,
97-
onnotice() {},
98-
port: PORT,
99-
user: USER,
100-
},
101-
postgres,
102-
}),
103-
},
104-
toString: () => 'options',
105-
},
106-
]
107-
108-
export type TestConfig = (typeof CONFIGS)[number]
70+
export const CONFIG: KyselyConfig = {
71+
dialect: new PostgresJSDialect({
72+
postgres: postgres({
73+
database: 'test',
74+
host: 'localhost',
75+
max: 20,
76+
onnotice() {},
77+
port: 5434,
78+
user: 'admin',
79+
}),
80+
}),
81+
}
10982

110-
export async function initTest(ctx: Mocha.Context, config: KyselyConfig, log?: Logger): Promise<TestContext> {
83+
export async function initTest(ctx: Mocha.Context, log?: Logger): Promise<TestContext> {
11184
ctx.timeout(TEST_INIT_TIMEOUT)
11285

11386
const db = await connect({
114-
...config,
87+
...CONFIG,
11588
log,
11689
})
11790

11891
await createDatabase(db)
11992

120-
return {config, db}
93+
return {db}
12194
}
12295

12396
export async function destroyTest(ctx: TestContext): Promise<void> {

0 commit comments

Comments
 (0)