Skip to content

Commit

Permalink
chore: update PostgresDialectConfig. (#2)
Browse files Browse the repository at this point in the history
* chore: update PostgresDialectConfig.

* chore: limit pool to one connection.
  • Loading branch information
thorwebdev authored Apr 19, 2023
1 parent 51728a3 commit 7c1b3cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
22 changes: 17 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Database>({
dialect: new PostgresDialect({ pool }),
});
```

Expand Down
4 changes: 2 additions & 2 deletions src/deps/kysely.ts
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion src/deps/postgres.ts
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 4 additions & 2 deletions src/postgres/postgres-dialect-config.ts
Original file line number Diff line number Diff line change
@@ -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<Pool>);
}
14 changes: 8 additions & 6 deletions src/postgres/postgres-driver.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<PoolClient, DatabaseConnection>();
#pool: Pool | null = null;

constructor(config: ClientOptions) {
constructor(config: PostgresDialectConfig) {
this.#config = config;
}

async init(): Promise<void> {
// 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<DatabaseConnection> {
Expand Down

0 comments on commit 7c1b3cb

Please sign in to comment.