drizzle-team / drizzle-team/drizzle-orm
[BUG]: Single Selects don't work with JSON fields with bun:sqlite
- 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.29.3
### What version of `drizzle-kit` are you using?
0.20.12
### Describe the Bug
It seems like tables with a `text("name", { mode: "json" })` column can only be selected if more than one entry get's selected (e.g. `DB.query.Table.findMany().sync()` or `DB.select().from(Table).all()`)
### Expected behavior
If i use `.get()` or `.findFirst()` it gives me a error containing this
```m
291 | } else if (is(field, SQL)) {
292 | decoder = field.decoder;
293 | } else {
294 | decoder = field.sql.decoder;
295 | }
296 | result[selectionItem.tsKey] = value === null ? null : decoder.mapFromDriverValue(value);
^
SyntaxError: JSON Parse error: Unexpected identifier "undefined"
at mapRelationalRow (/Users/xxx/Documents/Privat/tests/drizzle/api/node_modules/drizzle-orm/relations.js:296:61)
at map (:1:21)
at /Users/xxx/Documents/Privat/tests/drizzle/api/node_modules/drizzle-orm/sqlite-core/query-builders/query.js:100:22
at /Users/xxx/Documents/Privat/tests/drizzle/api/src/index.ts:22:9
```
It looks like this originated from the utils.js file line 28 and relations.js line 296 (basically everytime it hits mapFromDriverValue)
This is because for example the code in utils.js
```js
const rawValue = row[columnIndex];
const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
```
Results in rawValue being `undefined` because row is something like
```json
{
"count(*)": 8
}
```
Or any other object containing id and other key value columns and the code tries to index this object
I was able to temporary fix it by doing following:
utils.js
```js
const rawValue = row[columnIndex] || Object.values(row)[columnIndex];
const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
```
relations.js
```js
const value = mapColumnValue(row[selectionItemIndex] || Object.values(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);
```
I basically saw that the code tried to index the values so i just converted the single objects to its values so it could get indexed properly
### Environment & setup
> Bun@1.0.23 (using bun:sqlite)
default setup with sqlite table using text with mode json and trying to select a single column
Contributor guide
Assessment
This issue has not been assessed yet.