drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: SQLite JSON should be TEXT, not blob

Open arjunyel opened this issue 2 years ago • 2 comments

What version of drizzle-orm are you using?

0.24.2

What version of drizzle-kit are you using?

0.17.5

Describe the Bug

Right now drizzle has JSON under blob when it should be under text https://github.com/drizzle-team/drizzle-orm/tree/main/drizzle-orm/src/sqlite-core#column-types

SQLite does not (currently) support a binary encoding of JSON. (...) All JSON functions currently throw an error if any of their arguments are BLOBs because BLOBs are reserved for a future enhancement in which BLOBs will store the binary encoding for JSON. https://www.sqlite.org/json1.html

Expected behavior

I'd expect Drizzle stores JSON as TEXT and eventually natively supports JSON operators. It would be cool if the driver automatically handled JSON.stringify and JSON.parse too :)

Environment & setup

No response

arjunyel avatar Apr 19 '23 07:04 arjunyel

@arjunyel just read through the sqlite's JSON functions and I wonder about their actual use case. They take strings (which look like JSON), so why not just do all JSON operations on app level before a final JSON.stringify() and before they enter sqllite as text?

Pros:

  • You put load on the app servers/edge functions which are way easier to scale and not the db
  • You keep JSON logic/validations always in the app
  • With Drizzle custom types and mapFromDriverValue() you can automate the last JSON.stringify() before insert, IDK about the way around or I missed it, @dankochetov ?

Cons:

  • JSONs in the db are just dumb data which can't be queried but tbh, to do proper stuff you would need native JSON support like MongoDB

For my use case I don't have the con because if I wanted the JSON or some of its props to have "meaning", I would make columns out of it bc of type safety, otherwise I could use Mongo right away

So, what does speak against saving JSONs just as text and stringified before?

205g0 avatar May 03 '23 06:05 205g0

Forget what I've written before, it's BS.

We need to have those JSON ops, hence OP's expected behaviour makes total sense. An example:

Without these sqlite fucntions we'd need to get the entire JSON, with these we can just slice a portion (with the -> or ->> operators) and send the smaller portion to the app server. This can make a difference in perfomance but cost as well.

So, until this is fixed, why not just save JSON as text as use the sqlite JSON functions with a plain SQL query without Drizzle?!

205g0 avatar May 03 '23 15:05 205g0

fyi jsonb soon to be supported in SQLite

https://sqlite.org/draft/jsonb.html

Why don't "query builder" support the json/jsonb types provided by DB natively? Even though text/blob mode:json has its advantages, it would be better to leave it to the user's choice.

cometkim avatar Oct 20 '23 06:10 cometkim

I think this issue is outdated. See the current docs where they recommend json to be stored in text. I also just tested this and it works properly.

Edit: Please feel free to re-open if you think there's still a problem.

Angelelz avatar Dec 18 '23 23:12 Angelelz