oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

jsonb '?|' operator #343

Open karlismelderis-mckinsey opened 1 year ago

karlismelderis-mckinsey commented 1 year ago

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.

secam commented 8 months ago

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