james-elicx / cf-bindings-proxy

Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages
MIT License
94 stars 4 forks source link

Accessing D1 `raw()`'s non-existent element returns a function #12

Closed Yihao-G closed 1 year ago

Yihao-G commented 1 year ago

Firstly thanks for your great work.

It seems if I access an index-out-of-bound element in the returned array of raw(), it returns a function rather than undefined:

const stmt = binding<D1Database>('D1').prepare(
    'SELECT author, post_slug FROM comments LIMIT 0',
);

const raw = await stmt.raw();
expect(raw[0]).toBeUndefined(); // AssertionError: expected [Function] to be undefined

The raw[0] function seems to be pointing to here: https://github.com/james-elicx/cf-bindings-proxy/blob/a19f37277a742ff9333675c8d0af977a2c822dc9/src/proxy.ts#L68-L72

Drizzle ORM checks if there is a row in the returned result by checking if raw[0] is truthy. Because of this issue, Drizzle will return an object with every field set to undefined instead: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/d1/session.ts#L139-L141

james-elicx commented 1 year ago

Hey there, thanks for raising this. I've just added a fix and it is included in the latest release (v0.2.4).

Yihao-G commented 1 year ago

Works like a charm! Thank you so much for such a speedy response!