Issue querying with relations in bun+sqlite

👋 repro + context here - https://github.com/sammccord/drizzle-relations-repro It seems like the query goes through fine, but the row comes back as json and not a list of values, so trying to access the fields by index won't work. Not sure if it's a bun or drizzle issue, but since the query works fine it could be a simple parsing fix but I'm not 100% the best way to go about fixing it myself.
GitHub
GitHub - sammccord/drizzle-relations-repro
Contribute to sammccord/drizzle-relations-repro development by creating an account on GitHub.
9 Replies
Rph
Rph•8mo ago
I've ran into this same issue, @sammccord were you ever able to resolve this?
sammccord
sammccordOP•8mo ago
Nope
Rph
Rph•8mo ago
i was able to get it to work by adding this:
if (!Array.isArray(row)) {
let or = row;
row = [];
for (let b of buildQueryResultSelection) {
row.push(or[b.dbKey]);
}
}
if (!Array.isArray(row)) {
let or = row;
row = [];
for (let b of buildQueryResultSelection) {
row.push(or[b.dbKey]);
}
}
on line 262 of node_modules/drizzle-orm/relations.js but i am unsure if this breaks something or not Maybe someone from the drizzle team could take a look at this? I feel like this is an issue to be resolved at the bun-sqlite driver level, since there could be other behaviors that depend on the row being an array
sammccord
sammccordOP•8mo ago
This looks good to me, I’ve moved to a different orm but would definitely be nice to get this looked at
Rph
Rph•8mo ago
which orm are you using now, out of curiosity?
sammccord
sammccordOP•8mo ago
Prisma, I’d rather use drizzle but I’m firmly in bun and this is is the only thing holding me back
Rph
Rph•8mo ago
hey @alexblokh, are you aware of this issue/discrepancy in behavior between the better-sqlite3 and bun:sqlite driver? I did some more debugging, it seems like the better-sqlite3 driver calls
const row = stmt.raw().get(...params);
const row = stmt.raw().get(...params);
which returns an array of values ([ '0.9687278431702451', '[["0.07639715656862389","0.9687278431702451"]]'] in this example) after this block
if (!fields && !customResultMapper) {
return stmt.get(...params);
}
if (!fields && !customResultMapper) {
return stmt.get(...params);
}
inside PreparedQuery.get where the get would return a mapped value. Meanwhile, the bun driver seemingly always calls the api to get a mapped value ( const row = this.stmt.get(...params);) and works with that value for the rest of the PreparedQuery.get function, including in parts where an array is expected
Rph
Rph•8mo ago
oh, it seems there is a PR for this already: https://github.com/drizzle-team/drizzle-orm/pull/1276
GitHub
Fix bun sqlite returning when using get by dardanbujupaj · Pull Req...
Fix returning with bun sqlite when using get. Bun sqlite get returns an object, which is not handled well by the result mapper. By using values (but only the first index) we receive the needed arra...
Rph
Rph•8mo ago
opened last year I also found this PR: https://github.com/drizzle-team/drizzle-orm/pull/1885 that seems to already be merged but forcing drizzle-orm to the latest version (which has this code change) doesn't seem to work 🤔
Want results from more Discord servers?
Add your server