pglite icon indicating copy to clipboard operation
pglite copied to clipboard

feat: SQL template API

Open msfstef opened this issue 1 year 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

Built bundles:

  • Postgres WASM: https://github.com/electric-sql/pglite/actions/runs/10493033965/artifacts/1838150102
  • Interim build files: https://github.com/electric-sql/pglite/actions/runs/10493033965/artifacts/1838150333
  • PGlite: https://github.com/electric-sql/pglite/actions/runs/10493033965/artifacts/1838176881
  • Demos: https://github.com/electric-sql/pglite/actions/runs/10493033965/artifacts/1838190001

github-actions[bot] avatar Aug 20 '24 13:08 github-actions[bot]

🚀 Deployed on https://66c60c69685e0525373cc309--pglite.netlify.app

github-actions[bot] avatar Aug 20 '24 14:08 github-actions[bot]

@samwillis added it to the transaction interface as well - no tests for it though 👀

msfstef avatar Aug 21 '24 15:08 msfstef

SO timely! TinyBase plugin coming as soon as this ships.

jamesgpearce avatar Aug 21 '24 21:08 jamesgpearce