sqlite http-proxy can't handle undefined values on `.get()` ?

Hi drizzle team! I've been playing with the http-proxy plugin to see how it works, and I hit a funny edge case trying to handle undefined values on get(). The docs show this barebones example to set up an http proxy:
const db = drizzle(async (sql, params, method) => {
try {
const rows = await axios.post('http://localhost:3000/query', { sql, params, method });

return { rows: rows.data };
} catch (e: any) {
console.error('Error from sqlite proxy server: ', e.response.data)
return { rows: [] };
}
});
const db = drizzle(async (sql, params, method) => {
try {
const rows = await axios.post('http://localhost:3000/query', { sql, params, method });

return { rows: rows.data };
} catch (e: any) {
console.error('Error from sqlite proxy server: ', e.response.data)
return { rows: [] };
}
});
They, I attempted a .get() request for a value that doesn't exist in the database:
const email = await db.select().from(emails).where(eq(emails.email, '[email protected]')).get();
const email = await db.select().from(emails).where(eq(emails.email, '[email protected]')).get();
I tested the better-sqlite3 adapter and found email should return undefined (which doesn't match the type inference mind you!). But in the SQlite proxy, I get a much funnier result:
email = {
id: undefined,
email: undefined,
submitted: Invalid Date
}
email = {
id: undefined,
email: undefined,
submitted: Invalid Date
}
It seems it's trying to parse the value even when rows is an empty array in the adapter. Is there a way to return undefined for this case as in the better-sqlite adapter? Thanks!
12 Replies
Andrii Sherman
Andrii Sherman15mo ago
Thanks for the feedback! I'll try to reproduce it Yes, it's possible to do that Also, what was the issue with 'email doesn't match the type inference'? Can you show the schema object and a query where types are wrongly inferred?
Bholmesdev
Bholmesdev15mo ago
Ah, just commenting that the type signature gaurantees a return value when it could be undefined
Bholmesdev
Bholmesdev15mo ago
Andrii Sherman
Andrii Sherman15mo ago
oh, it't a known issue We actually have a PR for that didn't have a chance to merge it yet also saw you GH issue about custom types It's a known issue that toDriver is not always called on inserts, especially when the value is undefined, for example. However, for your case, we will provide the ability to pass a custom function to a default in a schema Tracking issue for that https://github.com/drizzle-team/drizzle-orm/issues/304 will duplicate this message in GH issue
Bholmesdev
Bholmesdev15mo ago
Ah that would be fantastic. The custom type was definitely a workaround after trying a lambda in default first! So you mean .default(() => nanoid()) could be possible in the future?
Andrii Sherman
Andrii Sherman15mo ago
We were thinking .$default(() => nanoid()) just to separate database defualts and runtime defaults but yes we will have it 2 super important things for us, that we are working on 1. Relational Queries performance improvements(almost there) 2. Slow TypeScript Intellisense on a big amount of tables/columns -> https://discord.com/channels/1043890932593987624/1095436601426972822 After that will clean PR's and add such features like defaults, etc.
Bholmesdev
Bholmesdev15mo ago
All very helpful, thanks! So about the undefined issue in my http-proxy, is this a case of me holding it wrong? Or are there planned changes here?
Andrii Sherman
Andrii Sherman15mo ago
I think it may be just a bug on sqlite-proxy side. I'll go through tests there and try to spot this issue I'll have a time to check it tomorrow and will ping you back here
Bholmesdev
Bholmesdev15mo ago
Sounds good. I can just check if entry?.id is undefined as a workaround for now
Andrii Sherman
Andrii Sherman15mo ago
Yeah, sorry for that and thanks for a bug report, it's always very helpful
Bholmesdev
Bholmesdev15mo ago
Also traced the code if it helps! Looks like empty arrays are still mapped against the column names, even when that array is empty. Would explain why the values are present but undefined https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/sqlite-proxy/session.ts#L129-L134
GitHub
drizzle-orm/drizzle-orm/src/sqlite-proxy/session.ts at main · drizz...
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
Andrii Sherman
Andrii Sherman15mo ago
Thanks!
Want results from more Discord servers?
Add your server