Skip to content

Commit

Permalink
refactor: accept postgres instance. (#7)
Browse files Browse the repository at this point in the history
* accept postgres instance.

* bump kysely.

* fix test.

* update readme.
  • Loading branch information
igalklebanov authored Oct 26, 2023
1 parent 824a2e6 commit 529778b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 100 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -41,8 +41,8 @@ To fix that, add an [`import_map.json`](https://deno.land/[email protected]/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"
}
}
```
Expand All @@ -65,26 +65,13 @@ interface Database {

const db = new Kysely<Database>({
dialect: new PostgresJSDialect({
connectionString: 'postgres://admin@localhost:5434/test',
options: {
max: 10,
},
postgres,
}),
})

// or...

const db = new Kysely<Database>({
dialect: new PostgresJSDialect({
options: {
postgres: postgres({
database: 'test',
host: 'localhost',
max: 10,
port: 5434,
user: 'admin',
},
postgres,
}),
}),
})
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
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<void> {
// noop
}

async acquireConnection(): Promise<PostgresJSConnection> {
const reservedConnection = await (this.#sql as any).reserve()
const reservedConnection = await this.#config.postgres.reserve()

return new PostgresJSConnection(reservedConnection)
}
Expand All @@ -42,6 +38,6 @@ export class PostgresJSDriver implements Driver {
}

async destroy(): Promise<void> {
await this.#sql.end()
await this.#config.postgres.end()
}
}
16 changes: 4 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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<any>
postgres: typeof postgres
}
| {
options: Options<any>
postgres: typeof postgres
}
export interface PostgresJSDialectConfig {
readonly postgres: Sql
}
10 changes: 0 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import type {Sql} from 'postgres'

import type {PostgresJSDialectConfig} from './types'

export function freeze<T>(obj: T): Readonly<T> {
return Object.freeze(obj)
}

export function createPostgres(config: PostgresJSDialectConfig): Sql {
return 'connectionString' in config
? config.postgres(config.connectionString, config.options)
: config.postgres(config.options)
}
11 changes: 5 additions & 6 deletions tests/nodejs/index.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()

Expand Down
57 changes: 15 additions & 42 deletions tests/nodejs/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ interface PetInsertParams extends Omit<Pet, 'id' | 'owner_id'> {
}

export interface TestContext {
config: KyselyConfig
db: Kysely<Database>
}

Expand All @@ -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<TestContext> {
export async function initTest(ctx: Mocha.Context, log?: Logger): Promise<TestContext> {
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<void> {
Expand Down

0 comments on commit 529778b

Please sign in to comment.