Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking presence of specific JSON data/path with Prisma

Tags:

prisma

I have a jsonb field in postgres, where I want prisma to check if it has a certain path with underlaying objects.

Field: locations

Data example #1:
{"hotels":{"id":984}}

Data example #2:
{"restaurants":{"id":293}}

I would like to somehow query this field via Prisma, to return rows where the field contains one or more hotel ids. Does anyone know how this can be achieves with Prisma? I can only seem to find ways to query on actual data, not on path existence or path data presence.

Worst case I could accept just checking if the "hotels" object is in the json, without actually checking for presence of child ids.

like image 999
user5801710 Avatar asked Jan 26 '26 09:01

user5801710


1 Answers

You could use the queryRaw prisma method for passing raw queries and then checking for path existence.

You can pass the below select query to check if the hotels key exists.

SELECT '{"hotels":{"id":984}}'::jsonb ? 'hotels'

The query would return true if the hotels key exists.

like image 166
Nurul Sundarani Avatar answered Jan 28 '26 23:01

Nurul Sundarani