Skip to content

Commit

Permalink
feat: upgrade to latest version and add stream types for compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
bart committed Sep 7, 2022
1 parent 4e74b53 commit 51728a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<{}>({
Expand Down
3 changes: 2 additions & 1 deletion src/deps/kysely.ts
Original file line number Diff line number Diff line change
@@ -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";
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.14.3/mod.ts";
export * from "https://deno.land/x/postgres@v0.16.1/mod.ts";
47 changes: 35 additions & 12 deletions src/postgres/postgres-driver.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
const result = await this.#client.queryObject<O>(
compiledQuery.sql,
...compiledQuery.parameters
);
try {
const result = await this.#client.queryObject<O>(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<O>(
_compiledQuery: CompiledQuery,
_chunkSize: number
): AsyncIterableIterator<QueryResult<O>> {
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 {
Expand Down

0 comments on commit 51728a3

Please sign in to comment.