Skip to content

Commit 7c1b3cb

Browse files
authored
chore: update PostgresDialectConfig. (#2)
* chore: update PostgresDialectConfig. * chore: limit pool to one connection.
1 parent 51728a3 commit 7c1b3cb

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

readme.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@
33
## usage
44

55
```ts
6-
import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js";
6+
import { Pool } from "https://deno.land/x/[email protected]/mod.ts";
7+
import { Kysely, Generated } from "https://esm.sh/[email protected]";
78
import { PostgresDialect } from "https://deno.land/x/kysely_postgres/mod.ts";
89

9-
const db = new Kysely<{}>({
10-
dialect: new PostgresDialect({
11-
// ...
12-
}),
10+
// Create a database pool with one connection.
11+
const pool = new Pool(
12+
{
13+
database: "postgres",
14+
hostname: "localhost",
15+
user: "postgres",
16+
port: 54322,
17+
password: Deno.env.get("DB_PASSWORD"),
18+
},
19+
1
20+
);
21+
22+
// You'd create one of these when you start your app.
23+
const db = new Kysely<Database>({
24+
dialect: new PostgresDialect({ pool }),
1325
});
1426
```
1527

src/deps/kysely.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js";
2-
export * from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/util/stack-trace-utils.js";
1+
export * from "https://esm.sh/kysely@0.23.4";
2+
export * from "https://esm.sh/kysely@0.23.4/dist/esm/util/stack-trace-utils.js";

src/deps/postgres.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "https://deno.land/x/postgres@v0.16.1/mod.ts";
1+
export * from "https://deno.land/x/postgres@v0.17.0/mod.ts";
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import type { ClientOptions } from "../deps/postgres.ts";
1+
import type { Pool } from "../deps/postgres.ts";
22

3-
export type PostgresDialectConfig = ClientOptions;
3+
export interface PostgresDialectConfig {
4+
pool: Pool | (() => Promise<Pool>);
5+
}

src/postgres/postgres-driver.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Pool } from "../deps/postgres.ts";
2-
import type { PoolClient, ClientOptions } from "../deps/postgres.ts";
2+
import type { PoolClient } from "../deps/postgres.ts";
3+
import { PostgresDialectConfig } from "./postgres-dialect-config.ts";
34
import { CompiledQuery, PostgresCursorConstructor } from "../deps/kysely.ts";
45
import type {
56
DatabaseConnection,
@@ -12,18 +13,19 @@ import { extendStackTrace } from "../deps/kysely.ts";
1213
const PRIVATE_RELEASE_METHOD = Symbol();
1314

1415
export class PostgresDriver implements Driver {
15-
readonly #config: ClientOptions;
16+
readonly #config: PostgresDialectConfig;
1617
readonly #connections = new WeakMap<PoolClient, DatabaseConnection>();
1718
#pool: Pool | null = null;
1819

19-
constructor(config: ClientOptions) {
20+
constructor(config: PostgresDialectConfig) {
2021
this.#config = config;
2122
}
2223

2324
async init(): Promise<void> {
24-
// TODO: size is required unlike the node `pg` module.
25-
// Need to figure out what is a good value to use here
26-
this.#pool = new Pool(this.#config, 1);
25+
this.#pool =
26+
typeof this.#config.pool === "function"
27+
? await this.#config.pool()
28+
: this.#config.pool;
2729
}
2830

2931
async acquireConnection(): Promise<DatabaseConnection> {

0 commit comments

Comments
 (0)