Skip to content

Commit 51728a3

Browse files
author
bart
committed
feat: upgrade to latest version and add stream types for compatability
1 parent 4e74b53 commit 51728a3

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## usage
44

55
```ts
6-
import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely@0.16.5/dist/esm/index-nodeless.js";
6+
import { Kysely } from "https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js";
77
import { PostgresDialect } from "https://deno.land/x/kysely_postgres/mod.ts";
88

99
const db = new Kysely<{}>({

src/deps/kysely.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from "https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/index-nodeless.js";
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";

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.14.3/mod.ts";
1+
export * from "https://deno.land/x/postgres@v0.16.1/mod.ts";

src/postgres/postgres-driver.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Pool } from "../deps/postgres.ts";
22
import type { PoolClient, ClientOptions } from "../deps/postgres.ts";
3-
import { CompiledQuery } from "../deps/kysely.ts";
3+
import { CompiledQuery, PostgresCursorConstructor } from "../deps/kysely.ts";
44
import type {
55
DatabaseConnection,
66
QueryResult,
77
Driver,
88
TransactionSettings,
99
} from "../deps/kysely.ts";
10+
import { extendStackTrace } from "../deps/kysely.ts";
1011

1112
const PRIVATE_RELEASE_METHOD = Symbol();
1213

@@ -30,7 +31,7 @@ export class PostgresDriver implements Driver {
3031
let connection = this.#connections.get(client);
3132

3233
if (!connection) {
33-
connection = new PostgresConnection(client);
34+
connection = new PostgresConnection(client, { cursor: null });
3435
this.#connections.set(client, connection);
3536

3637
// The driver must take care of calling `onCreateConnection` when a new
@@ -82,29 +83,51 @@ export class PostgresDriver implements Driver {
8283
}
8384
}
8485

86+
interface PostgresConnectionOptions {
87+
cursor: PostgresCursorConstructor | null;
88+
}
89+
8590
class PostgresConnection implements DatabaseConnection {
8691
#client: PoolClient;
92+
#options: PostgresConnectionOptions;
8793

88-
constructor(client: PoolClient) {
94+
constructor(client: PoolClient, options: PostgresConnectionOptions) {
8995
this.#client = client;
96+
this.#options = options;
9097
}
9198

9299
async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
93-
const result = await this.#client.queryObject<O>(
94-
compiledQuery.sql,
95-
...compiledQuery.parameters
96-
);
100+
try {
101+
const result = await this.#client.queryObject<O>(compiledQuery.sql, [
102+
...compiledQuery.parameters,
103+
]);
104+
105+
if (result.command === "UPDATE" || result.command === "DELETE") {
106+
return {
107+
numUpdatedOrDeletedRows: BigInt(result.rowCount!),
108+
rows: result.rows ?? [],
109+
};
110+
}
97111

98-
if (result.command === "UPDATE" || result.command === "DELETE") {
99112
return {
100-
numUpdatedOrDeletedRows: BigInt(result.rowCount!),
101113
rows: result.rows ?? [],
102114
};
115+
} catch (err) {
116+
throw extendStackTrace(err, new Error());
117+
}
118+
}
119+
120+
async *streamQuery<O>(
121+
_compiledQuery: CompiledQuery,
122+
_chunkSize: number
123+
): AsyncIterableIterator<QueryResult<O>> {
124+
if (!this.#options.cursor) {
125+
throw new Error(
126+
"'cursor' is not present in your postgres dialect config. It's required to make streaming work in postgres."
127+
);
103128
}
104129

105-
return {
106-
rows: result.rows ?? [],
107-
};
130+
// Deno postgress does not support streaming
108131
}
109132

110133
[PRIVATE_RELEASE_METHOD](): void {

0 commit comments

Comments
 (0)