Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Native PG jsonb query support #1690

Open
jakeleventhal opened this issue Dec 23, 2023 · 2 comments
Open

[FEATURE]: Native PG jsonb query support #1690

jakeleventhal opened this issue Dec 23, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@jakeleventhal
Copy link

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:

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:

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:

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

@jakeleventhal jakeleventhal added the enhancement New feature or request label Dec 23, 2023
@jakeleventhal
Copy link
Author

@hilja
Copy link

hilja commented Jan 23, 2024

Someone implemented a cool type-safe JSON util here #1511 (comment). My TS server ground to a halt when using it though :D But nonetheless that's a nice concept.

It makes me almost dizzy to think if jsonb_path_query would be type-safe, or if there'd be an abstraction on top of it. It's so capable.

const foo = sql`jsonb_path_query_array(
  ${reports.lighthouseResult},
  'strict $.**
    ? (exists(@.categories))
    ? (exists(@.audits)).audits.*
    ? (
        (
          (@.scoreDisplayMode == "binary" && @.score < 1)
          || ((@.scoreDisplayMode == "numeric" || @.scoreDisplayMode == "metricSavings") && @.score < 1)
        )
        && (${isPerf} || ${isDefault})
      )'
    )`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants