Skip to content

refactor: accept postgres instance. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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