pglite icon indicating copy to clipboard operation
pglite copied to clipboard

feat: SQL template API

Open msfstef opened this issue 6 months ago • 4 comments

Adds the sql template API that under the hood uses methods from the pglite/template export to parametrize a templated query and then run PGlite.query.

The template export has the following methods:

  • query which "executes" the template and replaces all values with parameter placeholders and returns { query: string, params: any[] } for running a parametrized query
  • identifier which escapes identifiers and prevents parametrization
  • raw which only prevents parametrization - allows regular string templating basically
  • sql which allows nesting SQL templated strings and creating custom utilities (to avoid exploding in complexity ourselves)

I've added docstrings with examples on all of them - if we're happy with the API I can add the docs to this PR as well.

Added basic test coverage for the utilities outside of PG and basic coverage of just using the API instead of the db.query one.

msfstef avatar Aug 20 '24 13:08 msfstef