diff --git a/readme.md b/readme.md index 67836aa..92094ec 100644 --- a/readme.md +++ b/readme.md @@ -3,13 +3,25 @@ ## usage ```ts -import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js"; +import { Pool } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; +import { Kysely, Generated } from "https://esm.sh/kysely@0.23.4"; import { PostgresDialect } from "https://deno.land/x/kysely_postgres/mod.ts"; -const db = new Kysely<{}>({ - dialect: new PostgresDialect({ - // ... - }), +// Create a database pool with one connection. +const pool = new Pool( + { + database: "postgres", + hostname: "localhost", + user: "postgres", + port: 54322, + password: Deno.env.get("DB_PASSWORD"), + }, + 1 +); + +// You'd create one of these when you start your app. +const db = new Kysely({ + dialect: new PostgresDialect({ pool }), }); ``` diff --git a/src/deps/kysely.ts b/src/deps/kysely.ts index 7f8a588..d88b389 100644 --- a/src/deps/kysely.ts +++ b/src/deps/kysely.ts @@ -1,2 +1,2 @@ -export * from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js"; -export * from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/util/stack-trace-utils.js"; +export * from "https://esm.sh/kysely@0.23.4"; +export * from "https://esm.sh/kysely@0.23.4/dist/esm/util/stack-trace-utils.js"; diff --git a/src/deps/postgres.ts b/src/deps/postgres.ts index 3929b69..e374969 100644 --- a/src/deps/postgres.ts +++ b/src/deps/postgres.ts @@ -1 +1 @@ -export * from "https://deno.land/x/postgres@v0.16.1/mod.ts"; +export * from "https://deno.land/x/postgres@v0.17.0/mod.ts"; diff --git a/src/postgres/postgres-dialect-config.ts b/src/postgres/postgres-dialect-config.ts index e410724..294b534 100644 --- a/src/postgres/postgres-dialect-config.ts +++ b/src/postgres/postgres-dialect-config.ts @@ -1,3 +1,5 @@ -import type { ClientOptions } from "../deps/postgres.ts"; +import type { Pool } from "../deps/postgres.ts"; -export type PostgresDialectConfig = ClientOptions; +export interface PostgresDialectConfig { + pool: Pool | (() => Promise); +} diff --git a/src/postgres/postgres-driver.ts b/src/postgres/postgres-driver.ts index 95facb6..4be1f6b 100644 --- a/src/postgres/postgres-driver.ts +++ b/src/postgres/postgres-driver.ts @@ -1,5 +1,6 @@ import { Pool } from "../deps/postgres.ts"; -import type { PoolClient, ClientOptions } from "../deps/postgres.ts"; +import type { PoolClient } from "../deps/postgres.ts"; +import { PostgresDialectConfig } from "./postgres-dialect-config.ts"; import { CompiledQuery, PostgresCursorConstructor } from "../deps/kysely.ts"; import type { DatabaseConnection, @@ -12,18 +13,19 @@ import { extendStackTrace } from "../deps/kysely.ts"; const PRIVATE_RELEASE_METHOD = Symbol(); export class PostgresDriver implements Driver { - readonly #config: ClientOptions; + readonly #config: PostgresDialectConfig; readonly #connections = new WeakMap(); #pool: Pool | null = null; - constructor(config: ClientOptions) { + constructor(config: PostgresDialectConfig) { this.#config = config; } async init(): Promise { - // TODO: size is required unlike the node `pg` module. - // Need to figure out what is a good value to use here - this.#pool = new Pool(this.#config, 1); + this.#pool = + typeof this.#config.pool === "function" + ? await this.#config.pool() + : this.#config.pool; } async acquireConnection(): Promise {