How do I use the Sequelize.Op & Sequelize.literal?
jberall opened this issue · comments
I have a query on jsonb. I need to us the literal otherwise it escapes my where clause.
Any help is appreciated.
Sequelize.Op Sequelize.literal, sequelize.query
db.getModel(sequelizeModel).findAll( { where: { attachment_types: { '@>': '[{id:2}]::jsonb' } } })...
becomes:
WHERE "from_to_lob_state_status"."attachment_types" @> '"[{id:2}]::jsonb"'
On a side note how is the upgrade to hapi v17 going?
Thanks for the great work.
Jonathan
I think this is a sequelize specific question, but maybe this helps you:
Use .getDb()
to get the sequelize instance.
db.getModel(sequelizeModel).findAll({
$and: [
sequelize.where(sequelize.col('attachment_types'), '@>', sequelize.literal('[{id:2}]::jsonb'));
]
});
Also check the docs http://docs.sequelizejs.com/manual/tutorial/querying.html#jsonb.
The upgrade is currently stopped, haven't much time to work on it 😞 .
Thanks for the help.
I thought you might want to add notes to the readme.
I found the solution. The critical point is to use the db.sequelize from hapi-sequelizejs, otherwise it doesn't work.
Also the Operators (http://docs.sequelizejs.com/manual/tutorial/querying.html#operators) don't seem to be accessable so we have to just include them directly.
db.getModel(model).findAll({
where: {
attachment_types: { '@>': db.sequelize.literal('\'[{"id":3}]\'::jsonb') }
}
})
.then((qry) => {
return reply(qry);
})
.catch((err) => {
return reply({ err });
});
So to use raw queries it is
db.sequelize
.query('select * ...')
.spread((results, metadata) => {
// Results will be an empty array and metadata will contain the number of affected rows.
console.log(results);
console.log(metadata);
});
Best Regards,
Jonathan