arraysjsontypescriptpgtyped

With pgtyped, how to select rows where a json array contains a dynamic value?


This is something I can easily do in Postgresql:

SELECT * FROM my_table
WHERE my_col->'my_field' @> '["awesome value"]'

However, my current project uses pgtyped, and I can't seem to figure how to syntax it. If I try like this:

SELECT * FROM my_table
WHERE my_col->'my_field' @> :dyn_val! ;

... the related typescript file generates without error notification but the run function ends up having 'never' as a parameter instead of recognising dyn_val. I have tried a bunch of alternative syntax gimmicks, but I am not even sure this is supported, as the documentation is somewhat empty (either that or I haven't found a complete one).


Solution

  • After quite a bit of fiddling around, I found out I can make use of CONCAT, as long as I cast it properly:

    SELECT * FROM my_table
    WHERE my_col->'my_field' @> ('["' || :dyn_val! || '"]')::jsonb;
    

    This behaves as expected: it uses my dynamic string, encloses it in with the right syntax and once casted as jsonb, can be used with the @> operator.