Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sequelize, query property on array of objects

I have looked extensively for an answer but could not find a simple solution.

I have a table that contains a column subscriptionHistory

The data can look like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "fromDate": "2023-01-24T10:11:57.150Z",
    "userSubscribedToo": "EuuQ13"
  },
  {
    "fromDate": "2022-01-24T10:11:57.150Z",
    "tillDate": "2022-02-24T22:59:59.999Z",
    "userSubscribedToo": "a4ufoAB"
  }
]

I’m trying to find the records of the subscriptions.

In Mongo we do

'subscriptionHistory.$.userSubscribedToo' = 'a4ufoAB'

Nice and easy.

I’m using PostgreSQL and Sequelize,

The following doesn’t work.

const totalEarnings = await SubscriptionToken.count({
  where: {
    'subscriptionHistory.$.userSubscribedToo': user.id,
  },
});

Neither do any direct queries

SELECT *
FROM vegiano_dev."subscription-tokens"
WHERE "subscriptionHistory"->>'userSubscribedToo' = 'a4ufoAB'
--WHERE "subscriptionHistory" @> '{"userSubscribedToo": "a4ufoAB"}'

Not sure where to go now :-/

>Solution :

You can use a JSON path condition with the @@ (exists) operator:

select *
from vegiano_dev."subscription-tokens"
where "subscriptionHistory" @@ '$[*].userSubscribedToo == "a4ufoAB"'

The @> will work as welll, but because subscriptionHistory is an array, you need to use an array with that operator:

where "subscriptionHistory" @> '[{"userSubscribedToo": "a4ufoAB"}]'

This assumes that subscriptionHistory is defined as jsonb which it should be. If it’s not, you need to cast it: "subscriptionHistory"::jsonb

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading