sequelize postgres combine fn 'date' with jsonb value

Viewed 657

I need to compare a string of format MM/DD/YYYY with another one in postgres on a jsonb column using sequelize.

On a regular column I would do something like

sequelize.where(sequelize.fn('date', sequelize.col('created_at'), '>=', moment().subtract(1, 'days').format('MM/DD/YYY'))),

but once the value is in JSONB I can't seem to find the way to reference it.

I tried multiple variations:

sequelize.col("data ->> 'created_at'")

sequeliez.json("data.created_at")

etc...

What is the right way to implement this?

1 Answers

One way I finally found how to do this is using literal

sequelize.where(sequelize.fn('date', sequelize.literal(`data ->> 'created_at'`)), '>=', moment().subtract(1, 'days').format('MM/DD/YYY')))

This works find for me.

Related