drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Native PG jsonb query support

Open
#1,690 8 comments 52 reactions 0 assignees View on GitHub
db/mysql db/postgres enhancement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

If I have a column with a `jsonb` type, and I supply `$type`, there should be a native way for me to query this field without having to use the sql operator and rawdog the SQL here. The `$type` field gives correct typings but returned data from queries, but if I want to query into the JSON, the types should already be there and have some native support for querying this.

For instance, say I have:
```ts
myData: jsonb('myData').$type<{ a: { b: string } }>).notNull()
```
and I want to find a row where a.b === value. Right now, I have to do something like this:
```ts
await db.query.users.findFirst({
where: sql`myData->'a'->>'b' = ${value}`
});
```
There is no type safety here. If I update, the typing of `myData` in the schema file, I won't get any type error in this query. I should be able to have a more ergonomic query that also provides type safety. Something like:
```ts
await db.query.users.findFirst({
where: eq(myData.a.b, value)
});
```
Prisma also has a means of doing this that works well: https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/working-with-json-fields#filter-on-nested-object-property

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.