1.1.0 • Published 1 year ago

@geogeim/sqltemplate v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

sqltemplate

micro sql template libray for pg and mysql modules

  • <100 lines of code, zero dependencies ^1
  • typescript friendly

usage

const sql = require("@geogeim/sqltemplate");
const { Client } = require('pg')

...

const res = await client.query(sql`delete from options where id in (${sql.join(obsoleteOptions)}) returning *`);

The template recognizes the following argument types:

  • undefined will throw an exception
  • null values will output NULL string inside the text of the query
sql`${null}` == { text: 'NULL', values: []}
  • strings wrapped with sql.raw() will be directly inserted into the query
sql`${sql('--\n')}` == { text: '--\n', values: []}
  • strings wrapped with sql.id() are wrapped in double quotes and properly escaped
sql`${sql.id('long row name"')}` == { text: '"long row name """', values: []}
  • a nested template will be evaluated recursively
sql`select * from "table" where id=${1} and ${sql`status=${2}`}` == 
  { text: 'select * from "table" where "id"=$1 and "status"=$2', values: [1, 2]}
  • use sql.join() to join array of values or templates into a new template
sql.join([1, sql`hello`, 2], ",") == 
  { text: '$1,hello,$2', values: [1, 2]}

tips

^1: you can also install pg-minify to make the outputted queries a bit nicer, no other config needed!

1.1.0

1 year ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago