Skip to content

Commit

Permalink
fix: d1result returning truthy value for undefined properties
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Jul 16, 2023
1 parent 406efa5 commit 30a7b00
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-pigs-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cf-bindings-proxy': patch
---

Fix D1Result undefined properties returning truthy value.
24 changes: 22 additions & 2 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prettier:format": "prettier --ignore-unknown --ignore-path=.gitignore --write .",
"tsc": "tsc --noEmit",
"test": "vitest run",
"test:kill": "sudo kill -9 `sudo lsof -i :8799 -t`",
"test:kill": "rm -rf .wrangler; sudo kill -9 `sudo lsof -i :8799 -t`",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"alter-version": "node ./scripts/alter-version.js",
Expand All @@ -45,6 +45,8 @@
"@types/node": "^20.3.3",
"eslint": "^8.41.0",
"eslint-config-ixn": "^1.4.2",
"kysely": "^0.26.1",
"kysely-d1": "^0.3.0",
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
"typescript": "^5.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const createResponseProxy = <T extends object>(
get(_, prop) {
if (!data || ['then', Symbol.iterator, Symbol.toStringTag].includes(prop)) return undefined;

if (prop in data) {
if (prop in data || ['error', 'results'].includes(prop as string)) {
return data[prop as keyof typeof data];
}

Expand Down
27 changes: 27 additions & 0 deletions tests/proxy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { resolve } from 'path';
import type { ColumnType, Generated } from 'kysely';
import { Kysely } from 'kysely';
import { D1Dialect } from 'kysely-d1';
import { afterAll, beforeAll, expect, suite, test } from 'vitest';
import type { UnstableDevWorker } from 'wrangler';
import { unstable_dev } from 'wrangler';
Expand Down Expand Up @@ -151,6 +154,30 @@ suite('bindings', () => {
expect(total).toEqual(3);
});

test('kysely -> select all', async () => {
type KyselyDatabase = {
comments: {
id: Generated<number>;
author: ColumnType<string>;
body: ColumnType<string>;
post_slug: ColumnType<string>;
};
};

const d1 = binding<D1Database>('D1');
const kysley = new Kysely<KyselyDatabase>({ dialect: new D1Dialect({ database: d1 }) });

const data = await kysley.selectFrom('comments').selectAll().execute();

const expected = [
{ id: 1, author: 'Jon', body: 'How do you use D1?', post_slug: 'd1-guide' },
{ id: 2, author: 'Markus', body: 'Hello there!', post_slug: 'hello-world' },
{ id: 3, author: 'Kristian', body: 'Great post!', post_slug: 'hello-world' },
];

expect(data).toEqual(expected);
});

test('prepare -> raw (select)', async () => {
const stmt = binding<D1Database>('D1').prepare(
'SELECT author, post_slug FROM comments LIMIT 2',
Expand Down

0 comments on commit 30a7b00

Please sign in to comment.