Skip to content
/ sqlt Public

Safe SQL template API inspired by postgres.js

Notifications You must be signed in to change notification settings

Minigugus/sqlt

Repository files navigation

SQLt - SQL template without a backend

SQLt is a proof-of-concept API for building reusable safe SQL queries easily, using the powerfull ES6 tagged template string feature to ensure SQL safety.

Features

  • Fully typed
  • Very simple API
  • 0 dependencies by default
  • Do not need a database connection
  • CommonJS and ESM support (tree-shakable)
  • Works in the browser (for SQL generation or with a real database like sql.js)
  • Multi dialects (integration with postgres, sqlite3 and sql.js at the moment)
  • Leverage parameters encoding to user-provided library (dependencies injection)

Examples

More in the tests folder

// Inspired by https://github.com/porsager/postgres/issues/156#issuecomment-788230327

import createSql, { renderAsFile } from 'sqlt';
import postgresql from 'sqlt/dist/postgres.mjs';

const sql = createSql(postgresql());

const authed = id => sql`
  user_id = ${id} or user_role = 'admin'
`

const insert = (data, id) => sql`
  update comments set
    content = ''
  where ${authed(id)} 
`;

console.log(renderAsFile(insert('something', 'john')));

// -- Generated by SQLt / Tue, 02 Mar 2021 19:37:04 GMT / Node v14.15.1
//
// update comments set
//   content = ''
// where user_id = 'john' or user_role = 'admin';

Copied from tests/readme.test.mjs

Getting started

$ git clone https://github.com/Minigugus/sqlt.git
$ cd sqlt
$ yarn
$ yarn build
$ node tests/readme.test.mjs

About

Safe SQL template API inspired by postgres.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published