Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bookshelf.js where with json column Postgresql

I have a problem with Bookshelf, I want use query to columns json type My table have columns 'data' type json, I want get all element with in this columns 'team' = 'PSG'

I test:

  collection.query('whereRaw', "data->'team'->>'PSG'");

I have this error

"argument of WHERE must be type boolean, not type text"

Or I test

collection.query('where', "data", "#>", "'{team, PSG}'");

I have this error

"The operator \"#>\" is not permitted"

I think that have a report with https://github.com/tgriesser/bookshelf/issues/550

like image 584
Jonathan Jalouzot Avatar asked Jan 05 '15 13:01

Jonathan Jalouzot


Video Answer


1 Answers

Short answer:

collection.query('where', 'data', '@>', '{"team": "PSG"}');

Explanation:

Say you have a table foos, where an element foo is

 ------------------------
| id | attr              |
 ------------------------
| 0  |{ "bar": "fooBar"} | 
 ------------------------
| 1  |{ "bar": "fooFoo"} | 
 ------------------------

The raw query for that would be like.

select * from "foos" where "attr" @> '{"bar":"fooBar"}';

Now in Bookshelf if you have a model Foo that represent table foos it the query should be like.

Foo.where('attr', '@>', '{"bar":"fooBar"}').fetch().then(function(rows){});

Now for your case it should be like

collection.query('where', 'data', '@>', '{"team": "PSG"}');

Hopefully Zlatan Approves it.

like image 172
Muhammad Raihan Muhaimin Avatar answered Sep 28 '22 05:09

Muhammad Raihan Muhaimin