drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.44k stars 484 forks source link

[BUG]: BLOB json values are not being stringified before insert. #2509

Open SupremeTechnopriest opened 2 weeks ago

SupremeTechnopriest commented 2 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

Trying to insert an object into a blob JSON column fails. It has to be encoded as a string first, but this results in type errors:

const table = sqliteTable('table', {
  id: integer('id').primaryKey(),
  jsonCol: blob('json_col', { mode: 'json' }).$type<string[]>()
}

await db.update(table).set({ jsonCol: ["foo", "bar"] }).where(eq(table.id, 0)).execute()
// "Unexpected non-whitespace character after JSON at position 2 (line 1 column 3)"

await db.update(table).set({ jsonCol: '["foo","bar"]' }).where(eq(table.id, 0)).execute()
// Works, but type error: Type 'string' is not assignable to type 'string[] | SQL<unknown> | null | undefined'.

Expected behavior

Should be able to insert a javascript object and have it stringified under the covers.

Environment & setup

I'm using D1. It happens local with drizzle studio and I assume in production as well.

gerhardcit commented 1 week ago

@SupremeTechnopriest , I've went down this road, solved it with text column.

const table = sqliteTable('table', {
  id: integer('id').primaryKey(),
  jsonCol: text('json_col', { mode: 'json' }).$type<string[]>()
}

https://orm.drizzle.team/docs/column-types/sqlite#blob The docs recommend text for json as well.

SupremeTechnopriest commented 1 week ago

Yeah Im a little deep into the blob right now and the migration would be a nightmare to change to text. I didnt need json functions so I thought blobs would be fine. Im pretty sure this worked at one point.