pg-mem icon indicating copy to clipboard operation
pg-mem copied to clipboard

jsonb '?|' operator

Open karlismelderis-mckinsey opened this issue 1 year ago • 1 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.

karlismelderis-mckinsey avatar Jul 19 '23 11:07 karlismelderis-mckinsey

Yes, exactly the same problem I was experimenting with it a little registerOperator also accepts IType as left and right operands, I was trying to construct one for text[] with no luck

secam avatar Jan 06 '24 05:01 secam