drizzle-team / drizzle-team/drizzle-orm
[BUG]: Change the behavior with http proxy
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
0.30.7
### What version of `drizzle-kit` are you using?
0.20.14
### Describe the Bug
First, I want to thank you for this amazing tool.
Now, if you're a member of the drizzle team reading this issue, please read this one https://github.com/tdwesten/tauri-drizzle-sqlite-proxy-demo/issues/1 before, at the same time or after you read this. You can use it as further explanation, real world example or just context.
I want to apologize in advance for the possible mistakes I will make, english is not my first language.
## Context
Like in this repository https://github.com/tdwesten/tauri-drizzle-sqlite-proxy-demo , I use drizzle as orm with tauri and I use the [tauri-plugin-sql](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/sql) as custom engine. But as drizzle dont support it, I use the amazing [http proxy feature](https://orm.drizzle.team/docs/get-started-sqlite#http-proxy) and I think's it's one of the best thing ever in ORM/DB world.
## The problem
But here's the thing, in the doc, you exaplain that you expect an array when setting up the http proxy :
> Drizzle always waits for {rows: string[ ] [ ]} or {rows: string[ ]} for the return value.
```
import { drizzle } from 'drizzle-orm/sqlite-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: [] };
}
});
```
The problem is that it's hard to predict the exact order of the returned value and the exact order the drizzle ORM expect. In my case, I founded a fancy solution by ordering the returned value in the alphabetical order of the keys (too long to explain further here but again, please read [this](https://github.com/tdwesten/tauri-drizzle-sqlite-proxy-demo/issues/1)). In simple cases it works, but it can get trickier realy fast, especially when dealing with types other that simple text or number.
I investigated in the drizzle code and I found this:
```
function mapRelationalRow(tablesConfig, tableConfig, row, buildQueryResultSelection, mapColumnValue = (value) => value) {
const result = {};
for (const [
selectionItemIndex,
selectionItem
] of buildQueryResultSelection.entries()) {
if (selectionItem.isJson) {
const relation = tableConfig.relations[selectionItem.tsKey];
const rawSubRows = row[selectionItemIndex];
const subRows = typeof rawSubRows === "string" ? JSON.parse(rawSubRows) : rawSubRows;
result[selectionItem.tsKey] = is(relation, One) ? subRows && mapRelationalRow(
tablesConfig,
tablesConfig[selectionItem.relationTableTsKey],
subRows,
selectionItem.selection,
mapColumnValue
) : subRows.map(
(subRow) => mapRelationalRow(
tablesConfig,
tablesConfig[selectionItem.relationTableTsKey],
subRow,
selectionItem.selection,
mapColumnValue
)
);
} else {
const value = mapColumnValue(row[selectionItemIndex]);
const field = selectionItem.field;
let decoder;
if (is(field, Column)) {
decoder = field;
} else if (is(field, SQL)) {
decoder = field.decoder;
} else {
decoder = field.sql.decoder;
}
result[selectionItem.tsKey] = value === null ? null : decoder.mapFromDriverValue(value);
}
}
return result;
}
```
in my `node_modules/drizzle-orm/relation.js:260`
From what I understand, drizzle also doesn't really expect the array to be ordered the wrong way, and the issue I'm facing is when the field expect a json text but gets a simple text so it throws an error at ` result[selectionItem.tsKey] = value === null ? null : decoder.mapFromDriverValue(value);`.
### Expected behavior
I dont know if the http-proxy features was expected to deal with such cases but it would be very helpfull if one has more control over the data flow.
## Some personal ideas
May be expect a javascript map or an object in which the keys are the exact name of the rows and when drizzle made the mathcing, it does it just by row name. Or in the same idea, a tuple of [key,value].
Just something to make the behavior more predictable and to give more control.
If you are from the drizzle team and you didn't read yet this issue https://github.com/tdwesten/tauri-drizzle-sqlite-proxy-demo/issues/1 , I jusst want to remind you to do so. It will realy help both of us to make this more clear.
### Environment & setup
I use the drizzle ORM with the http-proxy feature and the [tauri-plugin-sql](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/sql).
tauri-plugin-sql: github:tauri-apps/tauri-plugin-sql#v1
drizzle-orm: 0.30.7
typescript: 5.3.3
blood type: A+
Contributor guide
Assessment
This issue has not been assessed yet.