diff --git a/README.md b/README.md index fcda1a1..b118b6e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Powered by TypeScript](https://img.shields.io/badge/powered%20by-typescript-blue.svg) -[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. +[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). 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. @@ -41,8 +41,8 @@ To fix that, add an [`import_map.json`](https://deno.land/manual@v1.26.1/linking ```json { "imports": { - "kysely": "https://cdn.jsdelivr.net/npm/kysely@0.23.5/dist/esm/index.js", - "postgres": "https://deno.land/x/postgresjs@v3.3.4/mod.js" + "kysely": "https://cdn.jsdelivr.net/npm/kysely@0.26.3/dist/esm/index.js", + "postgres": "https://deno.land/x/postgresjs@v3.4.0/mod.js" } } ``` @@ -65,26 +65,13 @@ interface Database { const db = new Kysely({ dialect: new PostgresJSDialect({ - connectionString: 'postgres://admin@localhost:5434/test', - options: { - max: 10, - }, - postgres, - }), -}) - -// or... - -const db = new Kysely({ - dialect: new PostgresJSDialect({ - options: { + postgres: postgres({ database: 'test', host: 'localhost', max: 10, port: 5434, user: 'admin', - }, - postgres, + }), }), }) ``` diff --git a/package.json b/package.json index 385c299..9189295 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "eslint-config-prettier": "^8.8.0", "eslint-plugin-import": "^2.27.5", "eslint-plugin-prettier": "^4.2.1", - "kysely": "^0.24.2", + "kysely": "^0.26.3", "mocha": "^10.2.0", "mocha-each": "^2.0.1", "postgres": "^3.4.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0c6582..ae7ee35 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,8 +57,8 @@ devDependencies: specifier: ^4.2.1 version: 4.2.1(eslint-config-prettier@8.8.0)(eslint@8.38.0)(prettier@2.8.7) kysely: - specifier: ^0.24.2 - version: 0.24.2 + specifier: ^0.26.3 + version: 0.26.3 mocha: specifier: ^10.2.0 version: 10.2.0 @@ -1689,8 +1689,8 @@ packages: minimist: 1.2.8 dev: true - /kysely@0.24.2: - resolution: {integrity: sha512-+7eaTJNUYm2yRq1x+lEOZc+78TO35dTZ9b0dh49+Z9CTt2byMSbMiOKpwPlOyCAaHD4kILkAYWYZNywFlmBwRA==} + /kysely@0.26.3: + resolution: {integrity: sha512-yWSgGi9bY13b/W06DD2OCDDHQmq1kwTGYlQ4wpZkMOJqMGCstVCFIvxCCVG4KfY1/3G0MhDAcZsip/Lw8/vJWw==} engines: {node: '>=14.0.0'} dev: true diff --git a/src/driver.ts b/src/driver.ts index ff393d9..f96f789 100644 --- a/src/driver.ts +++ b/src/driver.ts @@ -1,18 +1,14 @@ import type {Driver, TransactionSettings} from 'kysely' -import type {Sql} from 'postgres' import {PostgresJSConnection} from './connection.js' import type {PostgresJSDialectConfig} from './types.js' -import {createPostgres, freeze} from './utils.js' +import {freeze} from './utils.js' export class PostgresJSDriver implements Driver { readonly #config: PostgresJSDialectConfig - readonly #sql: Sql constructor(config: PostgresJSDialectConfig) { this.#config = freeze({...config}) - - this.#sql = createPostgres(this.#config) } async init(): Promise { @@ -20,7 +16,7 @@ export class PostgresJSDriver implements Driver { } async acquireConnection(): Promise { - const reservedConnection = await (this.#sql as any).reserve() + const reservedConnection = await this.#config.postgres.reserve() return new PostgresJSConnection(reservedConnection) } @@ -42,6 +38,6 @@ export class PostgresJSDriver implements Driver { } async destroy(): Promise { - await this.#sql.end() + await this.#config.postgres.end() } } diff --git a/src/types.ts b/src/types.ts index 25681cd..e2f093a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,13 +1,5 @@ -import type postgres from 'postgres' -import type {Options} from 'postgres' +import type {Sql} from 'postgres' -export type PostgresJSDialectConfig = - | { - connectionString: string - options?: Options - postgres: typeof postgres - } - | { - options: Options - postgres: typeof postgres - } +export interface PostgresJSDialectConfig { + readonly postgres: Sql +} diff --git a/src/utils.ts b/src/utils.ts index 6c493aa..91c58b3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,13 +1,3 @@ -import type {Sql} from 'postgres' - -import type {PostgresJSDialectConfig} from './types' - export function freeze(obj: T): Readonly { return Object.freeze(obj) } - -export function createPostgres(config: PostgresJSDialectConfig): Sql { - return 'connectionString' in config - ? config.postgres(config.connectionString, config.options) - : config.postgres(config.options) -} diff --git a/tests/nodejs/index.test.ts b/tests/nodejs/index.test.ts index 9ce3def..e315b76 100644 --- a/tests/nodejs/index.test.ts +++ b/tests/nodejs/index.test.ts @@ -1,14 +1,11 @@ import {CompiledQuery, DeleteResult, InsertResult, UpdateResult, sql, type Kysely, type Transaction} from 'kysely' import { - CONFIGS, DEFAULT_DATA_SET, POOL_SIZE, - TestConfig, clearDatabase, destroyTest, expect, - forEach, initTest, insertDefaultDataSet, testSql, @@ -17,12 +14,12 @@ import { type TestContext, } from './test-setup' -forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => { +describe('PostgresJSDialect: %s', () => { let ctx: TestContext const executedQueries: CompiledQuery[] = [] before(async function () { - ctx = await initTest(this, config.config, (event) => { + ctx = await initTest(this, (event) => { if (event.level === 'query') { executedQueries.push(event.query) } @@ -380,7 +377,9 @@ forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => { }) it('should delete two rows', async () => { - const query = ctx.db.deleteFrom('person').where('first_name', '=', 'Jennifer').orWhere('first_name', '=', 'Arnold') + const query = ctx.db + .deleteFrom('person') + .where((eb) => eb('first_name', '=', 'Jennifer').or('first_name', '=', 'Arnold')) const result = await query.executeTakeFirst() diff --git a/tests/nodejs/test-setup.ts b/tests/nodejs/test-setup.ts index d4f0067..5a75023 100644 --- a/tests/nodejs/test-setup.ts +++ b/tests/nodejs/test-setup.ts @@ -58,7 +58,6 @@ interface PetInsertParams extends Omit { } export interface TestContext { - config: KyselyConfig db: Kysely } @@ -68,56 +67,30 @@ export const PLUGINS: KyselyPlugin[] = [] export const POOL_SIZE = 20 -const DATABASE = 'test' -const HOST = 'localhost' -const PORT = 5434 -const USER = 'admin' - -export const CONFIGS: {config: KyselyConfig; toString: () => string}[] = [ - { - config: { - dialect: new PostgresJSDialect({ - connectionString: `postgres://${USER}@${HOST}:${PORT}/${DATABASE}`, - options: { - max: POOL_SIZE, - onnotice() {}, - }, - postgres, - }), - }, - toString: () => 'connection-string', - }, - { - config: { - dialect: new PostgresJSDialect({ - options: { - database: DATABASE, - host: HOST, - max: POOL_SIZE, - onnotice() {}, - port: PORT, - user: USER, - }, - postgres, - }), - }, - toString: () => 'options', - }, -] - -export type TestConfig = (typeof CONFIGS)[number] +export const CONFIG: KyselyConfig = { + dialect: new PostgresJSDialect({ + postgres: postgres({ + database: 'test', + host: 'localhost', + max: 20, + onnotice() {}, + port: 5434, + user: 'admin', + }), + }), +} -export async function initTest(ctx: Mocha.Context, config: KyselyConfig, log?: Logger): Promise { +export async function initTest(ctx: Mocha.Context, log?: Logger): Promise { ctx.timeout(TEST_INIT_TIMEOUT) const db = await connect({ - ...config, + ...CONFIG, log, }) await createDatabase(db) - return {config, db} + return {db} } export async function destroyTest(ctx: TestContext): Promise {