Skip to content

Commit 06138c6

Browse files
authored
add connectionString support. (#2)
1 parent a728ad0 commit 06138c6

File tree

9 files changed

+107
-25
lines changed

9 files changed

+107
-25
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,24 @@ interface Database {
6161
}
6262
}
6363

64+
const db = new Kysely<Database>({
65+
dialect: new PostgresJSDialect({
66+
connectionString: 'postgres://admin@localhost:5434/test',
67+
options: {
68+
max: 10,
69+
},
70+
postgres,
71+
}),
72+
})
73+
74+
// or...
75+
6476
const db = new Kysely<Database>({
6577
dialect: new PostgresJSDialect({
6678
options: {
6779
database: 'test',
6880
host: 'localhost',
81+
max: 10,
6982
port: 5434,
7083
user: 'admin',
7184
},

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@types/chai-as-promised": "^7.1.5",
4545
"@types/chai-subset": "^1.3.3",
4646
"@types/mocha": "^10.0.1",
47+
"@types/mocha-each": "^2.0.0",
4748
"@types/node": "^18.15.11",
4849
"@typescript-eslint/eslint-plugin": "^5.57.1",
4950
"@typescript-eslint/parser": "^5.57.1",
@@ -57,6 +58,7 @@
5758
"eslint-plugin-prettier": "^4.2.1",
5859
"kysely": "^0.24.2",
5960
"mocha": "^10.2.0",
61+
"mocha-each": "^2.0.1",
6062
"postgres": "^3.3.4",
6163
"prettier": "^2.8.7",
6264
"prettier-plugin-organize-imports": "^3.2.2",

pnpm-lock.yaml

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

src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {Sql} from 'postgres'
33

44
import {PostgresJSDialectError} from './errors.js'
55
import type {PostgresJSDialectConfig} from './types.js'
6-
import {freeze} from './utils.js'
6+
import {createPostgres, freeze} from './utils.js'
77

88
export class PostgresJSConnection implements DatabaseConnection {
99
readonly #config: PostgresJSDialectConfig
@@ -22,7 +22,7 @@ export class PostgresJSConnection implements DatabaseConnection {
2222

2323
const {isolationLevel} = settings
2424

25-
this.#transaction = this.#config.postgres({...this.#config.options, max: 1})
25+
this.#transaction = createPostgres({...this.#config, options: {...this.#config.options, max: 1}})
2626

2727
const statement = `start transaction${isolationLevel ? ` ${isolationLevel}` : ''}`
2828

src/driver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import type {Sql} from 'postgres'
33

44
import {PostgresJSConnection} from './connection.js'
55
import type {PostgresJSDialectConfig} from './types.js'
6-
import {freeze} from './utils.js'
6+
import {createPostgres, freeze} from './utils.js'
77

88
export class PostgresJSDriver implements Driver {
99
readonly #config: PostgresJSDialectConfig
1010
readonly #sql: Sql
1111

1212
constructor(config: PostgresJSDialectConfig) {
1313
this.#config = freeze({...config})
14-
this.#sql = this.#config.postgres(this.#config.options)
14+
15+
this.#sql = createPostgres(this.#config)
1516
}
1617

1718
async init(): Promise<void> {

src/types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import type postgres from 'postgres'
22
import type {Options} from 'postgres'
33

4-
export interface PostgresJSDialectConfig {
5-
options: Options<any>
6-
postgres: typeof postgres
7-
}
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+
}

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import type {Sql} from 'postgres'
2+
3+
import type {PostgresJSDialectConfig} from './types'
4+
15
export function freeze<T>(obj: T): Readonly<T> {
26
return Object.freeze(obj)
37
}
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import {DeleteResult, InsertResult, UpdateResult, sql, type Kysely, type Transaction} from 'kysely'
22

33
import {
4+
CONFIGS,
45
DEFAULT_DATA_SET,
56
POOL_SIZE,
7+
TestConfig,
68
clearDatabase,
79
destroyTest,
810
expect,
11+
forEach,
912
initTest,
1013
insertDefaultDataSet,
1114
testSql,
@@ -14,11 +17,11 @@ import {
1417
type TestContext,
1518
} from './test-setup'
1619

17-
describe('PostgresJSDialect', () => {
20+
forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => {
1821
let ctx: TestContext
1922

2023
before(async function () {
21-
ctx = await initTest(this)
24+
ctx = await initTest(this, config.config)
2225
})
2326

2427
beforeEach(async () => {

tests/nodejs/test-setup.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import postgres from 'postgres'
1616

1717
import {PostgresJSDialect} from '../../src'
1818

19+
export {default as forEach} from 'mocha-each'
20+
1921
chai.use(chaiSubset)
2022

2123
export interface Person {
@@ -66,31 +68,56 @@ export const PLUGINS: KyselyPlugin[] = []
6668

6769
export const POOL_SIZE = 20
6870

69-
export const CONFIG: KyselyConfig = {
70-
dialect: new PostgresJSDialect({
71-
options: {
72-
database: 'test',
73-
host: 'localhost',
74-
max: POOL_SIZE,
75-
onnotice() {},
76-
port: 5434,
77-
user: 'admin',
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+
}),
7887
},
79-
postgres,
80-
}),
81-
}
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]
82109

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

86113
const db = await connect({
87-
...CONFIG,
114+
...config,
88115
log,
89116
})
90117

91118
await createDatabase(db)
92119

93-
return {config: CONFIG, db}
120+
return {config, db}
94121
}
95122

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

0 commit comments

Comments
 (0)