I have a column in a Postgres that stores some JSON data. The JSON has no defined schema, but it should be possible to search all records that have some specified key.
I'm using KnexJS to build the queries and so far I came up with this:
tx.select('*').from('table')
.whereRaw('cast(data->>? as ?) = ?', [key, type, JSON.parse(value)]));
But it's not working because I don't think it will be possible to specify the type.
Still, when I try to specify it manually like this:
tx.select('*').from('table')
.whereRaw('cast(data->>? as boolean) = ?', [key, JSON.parse(value)]));
It still doesn't work! This is the log from the console when using DEBUG:knex:*
{ key: 'admin', value: 'true', type: 'boolean' }
knex:tx trx1: Starting top level transaction +0ms
knex:pool INFO pool postgresql:pg:client0 - dispense() clients=1 available=0 +2ms
knex:client acquired connection from pool: __knexUid2 +38ms
knex:query BEGIN; +2ms
knex:bindings undefined +1ms
knex:query select * from "contexts" where cast(data->>? as boolean) = ? +18ms
knex:bindings [ 'admin', true ] +0ms
knex:query COMMIT; +9ms
knex:bindings undefined +0ms
knex:tx trx1: releasing connection +6ms
knex:client releasing connection to pool: __knexUid2 +1ms
knex:pool INFO pool postgresql:pg:client0 - dispense() clients=0 available=1 +1ms
Any ideas on how I can accomplish this?
thanks in advance!