I am trying to build a storage manager where users can store their lab samples/data. Unfortunately, this means that the tables will end up being quite dynamic, as each sample might have different data associated with it. I will still require users to define a schema, so I can display the data properly, however, I think this schema will have to be represented as a JSON field in the underlying database.
I was wondering, in Prisma, is there a way to fuzzy search through collections. Could I type something like help and then return all rows that match this expression ANYWHERE in their columns? (including the JSON fields). Could i do something like this at all with posgresql? Or with MongoDB?
thank you
2
Answers
You can easily do that with
jsonb
in PostgreSQL.If you have a table defined like
You can create an index like this
and search efficiently with
Here,
@>
is the JSON containment operator in PostgreSQL.Yes, in PostgreSQL you surely can do this. It’s quite straightforward. Here is an example.
Let your table be called
the_table
aliased astht
. Cast an entire table row as texttht::text
and use case insensitive regular expression match operator~*
to find rows that contain help in this text. You can use more elaborate and powerful regular expression for searching too.Please note that since the
~*
operator will defeat any index, this query will result in a sequential scan.