From 51728a34b3357890959aa97e8dbbfd9e57ae7a12 Mon Sep 17 00:00:00 2001 From: bart Date: Wed, 7 Sep 2022 10:25:26 +0200 Subject: [PATCH] feat: upgrade to latest version and add stream types for compatability --- readme.md | 2 +- src/deps/kysely.ts | 3 ++- src/deps/postgres.ts | 2 +- src/postgres/postgres-driver.ts | 47 ++++++++++++++++++++++++--------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/readme.md b/readme.md index 0e78130..67836aa 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ ## usage ```ts -import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely@0.16.5/dist/esm/index-nodeless.js"; +import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js"; import { PostgresDialect } from "https://deno.land/x/kysely_postgres/mod.ts"; const db = new Kysely<{}>({ diff --git a/src/deps/kysely.ts b/src/deps/kysely.ts index cb2ec63..7f8a588 100644 --- a/src/deps/kysely.ts +++ b/src/deps/kysely.ts @@ -1 +1,2 @@ -export * from "https://cdn.jsdelivr.net/npm/kysely@0.16.5/dist/esm/index-nodeless.js"; +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"; diff --git a/src/deps/postgres.ts b/src/deps/postgres.ts index 45f8a1c..3929b69 100644 --- a/src/deps/postgres.ts +++ b/src/deps/postgres.ts @@ -1 +1 @@ -export * from "https://deno.land/x/postgres@v0.14.3/mod.ts"; +export * from "https://deno.land/x/postgres@v0.16.1/mod.ts"; diff --git a/src/postgres/postgres-driver.ts b/src/postgres/postgres-driver.ts index 94b91a9..95facb6 100644 --- a/src/postgres/postgres-driver.ts +++ b/src/postgres/postgres-driver.ts @@ -1,12 +1,13 @@ import { Pool } from "../deps/postgres.ts"; import type { PoolClient, ClientOptions } from "../deps/postgres.ts"; -import { CompiledQuery } from "../deps/kysely.ts"; +import { CompiledQuery, PostgresCursorConstructor } from "../deps/kysely.ts"; import type { DatabaseConnection, QueryResult, Driver, TransactionSettings, } from "../deps/kysely.ts"; +import { extendStackTrace } from "../deps/kysely.ts"; const PRIVATE_RELEASE_METHOD = Symbol(); @@ -30,7 +31,7 @@ export class PostgresDriver implements Driver { let connection = this.#connections.get(client); if (!connection) { - connection = new PostgresConnection(client); + connection = new PostgresConnection(client, { cursor: null }); this.#connections.set(client, connection); // The driver must take care of calling `onCreateConnection` when a new @@ -82,29 +83,51 @@ export class PostgresDriver implements Driver { } } +interface PostgresConnectionOptions { + cursor: PostgresCursorConstructor | null; +} + class PostgresConnection implements DatabaseConnection { #client: PoolClient; + #options: PostgresConnectionOptions; - constructor(client: PoolClient) { + constructor(client: PoolClient, options: PostgresConnectionOptions) { this.#client = client; + this.#options = options; } async executeQuery(compiledQuery: CompiledQuery): Promise> { - const result = await this.#client.queryObject( - compiledQuery.sql, - ...compiledQuery.parameters - ); + try { + const result = await this.#client.queryObject(compiledQuery.sql, [ + ...compiledQuery.parameters, + ]); + + if (result.command === "UPDATE" || result.command === "DELETE") { + return { + numUpdatedOrDeletedRows: BigInt(result.rowCount!), + rows: result.rows ?? [], + }; + } - if (result.command === "UPDATE" || result.command === "DELETE") { return { - numUpdatedOrDeletedRows: BigInt(result.rowCount!), rows: result.rows ?? [], }; + } catch (err) { + throw extendStackTrace(err, new Error()); + } + } + + async *streamQuery( + _compiledQuery: CompiledQuery, + _chunkSize: number + ): AsyncIterableIterator> { + if (!this.#options.cursor) { + throw new Error( + "'cursor' is not present in your postgres dialect config. It's required to make streaming work in postgres." + ); } - return { - rows: result.rows ?? [], - }; + // Deno postgress does not support streaming } [PRIVATE_RELEASE_METHOD](): void {