oguimbal / pg-mem

An in memory postgres DB instance for your unit tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jsonb '?|' operator

karlismelderis-mckinsey opened this issue · comments

I noticed that ?| operator is missing and we use it in one query.

so I'm trying to implement it and came up with this

  db.public.registerOperator({
    operator: '?|',
    left: DataType.jsonb,
    right: DataType.text,
    returns: DataType.bool,
    implementation: (a, b) => {
      if (Array.isArray(a)) {
        return a.some((key) => b.includes(key));
      }
      if (typeof a === 'object') {
        return Object.keys(a).some((key) => b.includes(key));
      }
      throw new QueryError('cannot check scalar', '22023');
    },
  });

sadly (or maybe not) right side is supposed to be string[] but I fail to do right: DataType.array

relevant part of query looks like this: data ?| ARRAY['5063098c-40fb-4375-94aa-b2c9ea14380d', '56062138-04df-4115-ad5e-80c5accbaff3']

how to make right side to work with string[] type?

if you think this operator could land in pg-mem I'm happy to open PR.