drizzle-team / drizzle-team/drizzle-orm

[BUG]: Cloudflare D1 JSON

Open
#1,175 0 comments 3 reactions 0 assignees View on GitHub
bug db/sqlite driver/d1 priority qb/crud
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.28.5

### What version of `drizzle-kit` are you using?

0.19.13

### Describe the Bug

Hi!
While working on D1, I've stumbled upon a potential problem while following this awesome package documentation.

When using a SQLite database and the need is to store a JSON value, the docs advise to use a blob value:
`blob('blob', { mode: 'json' })`

But Cloudflare D1 docs specify to use TEXT
`JSON data is stored as a TEXT column in D1.` (https://developers.cloudflare.com/d1/learning/querying-json/)

In my case, I've saved a bunch of data on a table, using blob, causing an error while querying.

Using this as example

```ts
type GQLFile = {
filename: string;
url: string;
};

export const foo = sqliteTable('foos', {
id: text('id').primaryKey(),
image: blob('image', { mode: 'json' }).$type()
});
```

and querying like (i.e. using relational queries, but the same happen using query builder)
```ts
await db.query.foo.findMany({
columns: {
id: true,
image: true
},
});
```
give me this error
`Unexpected non-whitespace character after JSON at position 3`

Trying to use a custom parser for JSON, ie:
```ts
const customJson = (name: string) =>
customType<{ data: TData; driverData: string }>({
dataType() {
return 'json';
},
toDriver(value: TData) {
return JSON.stringify(value);
},
fromDriver(value: string) {
console.log(value);
return JSON.parse(value);
}
})(name);
```

when hits the console.log, it prints a ArrayBuffer, as expected from blob.

I've tried to reproduce on nodejs using better-sqlite, but here the conversion from Arraybuffer to json happens magically without any error.

### Expected behavior

_No response_

### Environment & setup

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.