Skip to content

Commit

Permalink
Fix "Cannot convert undefined to a BigInt" (aidenwallis#8)
Browse files Browse the repository at this point in the history
* Fix "Cannot convert undefined to a BigInt"

* Return original variable if conversion to BigInt is not possible

* Only return `undefined` on `null` or `undefined`

Co-authored-by: Aiden <[email protected]>
  • Loading branch information
2 people authored and adam-the committed Aug 20, 2023
1 parent 31d4a90 commit 188931f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 83 deletions.
92 changes: 28 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "kysely-d1",
"description": "Kysely dialect for Cloudflare D1",
"main": "./dist/index.js",
"version": "0.0.5",
"version": "0.3.0",
"types": "./dist/index.d.ts",
"repository": "[email protected]:aidenwallis/kysely-d1.git",
"author": "Aiden <[email protected]>",
Expand All @@ -26,10 +26,10 @@
"kysely": "*"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.17.0",
"@tsconfig/node14": "^1.0.3",
"kysely": "^0.22.0",
"prettier": "^2.7.1",
"typescript": "^4.8.4"
"@cloudflare/workers-types": "^4.20230814.0",
"@tsconfig/node14": "^14.1.0",
"kysely": "^0.26.1",
"prettier": "^3.0.2",
"typescript": "^5.1.6"
}
}
}
2 changes: 1 addition & 1 deletion src/d1-api-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class D1Driver implements Driver {

async acquireConnection(): Promise<DatabaseConnection> {
const apiClient = new D1Api(this.#config.apiKey, this.#config.accountId);
const database = await apiClient.databseFromName(this.#config.databaseName);
const database = await apiClient.databaseFromName(this.#config.databaseName);

if (!database) {
throw new Error(`Database ${this.#config.databaseName} not found`);
Expand Down
4 changes: 3 additions & 1 deletion src/d1-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Headers, Request, RequestInit, URLSearchParams, fetch } from "@cloudflare/workers-types";

const BASE_URL = 'https://api.cloudflare.com/client/v4';

export class D1Api {
Expand Down Expand Up @@ -41,7 +43,7 @@ export class D1Api {
return databases;
}

async databseFromName(name: string) {
async databaseFromName(name: string) {
const allDBs = await this.listDatabases();
const matchingDB = allDBs.find((db: { uuid: string; name: string }) => db.name === name);
return matchingDB ?? null;
Expand Down
14 changes: 11 additions & 3 deletions src/d1-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
QueryCompiler,
QueryResult,
} from 'kysely';
import type { D1Database } from '@cloudflare/workers-types';

/**
* Config for the D1 dialect. Pass your D1 instance to this object that you bound in `wrangler.toml`.
Expand Down Expand Up @@ -106,10 +107,17 @@ class D1Connection implements DatabaseConnection {
throw new Error(results.error);
}

const numAffectedRows = results.meta.changes > 0 ? BigInt(results.meta.changes) : undefined;

return {
insertId: results.lastRowId !== null ? BigInt(results.lastRowId) : undefined,
insertId:
results.meta.last_row_id === undefined || results.meta.last_row_id === null
? undefined
: BigInt(results.meta.last_row_id),
rows: (results?.results as O[]) || [],
numUpdatedOrDeletedRows: results.changes > 0 ? BigInt(results.changes) : undefined,
numAffectedRows,
// @ts-ignore deprecated in kysely >= 0.23, keep for backward compatibility.
numUpdatedOrDeletedRows: numAffectedRows,
};
}

Expand All @@ -136,4 +144,4 @@ class D1Connection implements DatabaseConnection {
async *streamQuery<O>(_compiledQuery: CompiledQuery, _chunkSize: number): AsyncIterableIterator<QueryResult<O>> {
throw new Error('D1 Driver does not support streaming');
}
}
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './d1-dialect';

// Marked as unstable as the D1 HTTP API is currently undocumented and may change.
export { D1APIDialect as unstable_D1APIDialect, D1DialectConfig as unstable_D1DialectConfig } from './d1-api-dialect';
export { D1APIDialect as unstable_D1APIDialect, D1DialectConfig as unstable_D1DialectConfig } from './d1-api-dialect';
15 changes: 10 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
"skipLibCheck": true,
"strict": true,
"preserveConstEnums": true,
"declaration": true,
"types": ["@cloudflare/workers-types"]
"declaration": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "test"]
}
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"test"
]
}

0 comments on commit 188931f

Please sign in to comment.