Hey folks! I’m exploring the Json type as well and...
# orm-help
a
Hey folks! I’m exploring the Json type as well and was wondering what’s an appropriate way to access nested objects; all of the examples I see in the documentation are about accessing arrays and my queries in the playground are all very slow (4 seconds versus 60ms in postgres) e.g. let’s say we have a record like this:
Copy code
{
  "data": {
    "field_one": {
      "field_two": "hello"
    }
  }
}
In postgres, I could quickly find records matching on field_two’s value like so:
Copy code
SELECT doc_col FROM "MyTable" WHERE doc_col @> '{"data": {"field_one": {"field_two": "hello"}}}'
I have my table created in
schema.prisma
with doc_col set as Json and enabled prisma’s “filterJson” previewFeature. I wrote the following graphql query, which takes 4 seconds:
Copy code
query { myTables(where: {doc_col: {path: ["data", "field_one", "field_two"], equals: "0004"}}) }
I enabled postgres logging to see what graphql generates and the key difference seems to be the usage of the
#>
operator, but I’m unsure if there’s a way to encourage the desired behavior above
n
Hey Alex 👋 Could you check what time does it take for prisma to execute your query? You could use this Logging middleware to know the exact time. Doing this should help in understanding the bottleneck and gaining insights into which component is taking considerable time.
a
Hey, I just saw your message, sorry! The performance issue was because of the
#>
operator, which causes postgres to spend a long time searching for records. I worked around this by creating a custom resolver like so:
Copy code
@Query(() => [MyModel])
    async myModel(@Args() { key, take }: GetMyModelArgs) {
        return await prisma.$queryRaw(Prisma.sql`SELECT * FROM "MyModel" WHERE "document" @> ${key} LIMIT ${take};`);
    }
If I pass in a json string for
key
, then everything runs very quickly 🙂
👍 1