1.1.0 • Published 7 years ago

sqltag v1.1.0

Weekly downloads
324
License
ISC
Repository
github
Last release
7 years ago

sqltag

SQL generation using ES6 tagged template strings for mysql / mysql2 and postgres.

install

npm install sqltag

basic usage

You can use ${} syntax for value interpolation.

import SQL from 'sqltag'

mysql.query(SQL`SELECT * FROM users WHERE id = ${15}`)
mysql.query(SQL`SELECT * FROM users WHERE id = ${15} AND age > ${30}`)

pg.query(SQL`SELECT * FROM users WHERE id = ${15}`)
pg.query(SQL`SELECT * FROM users WHERE id = ${15} AND age > ${30}`)

extensions

raw

Raw values are inserted into resulting SQL as is.

pg.query(SQL`SELECT * FROM ${SQL.raw('users')} WHERE id = ${15}`)

ident

Identifiers are escaped according to database engine. Use for table / field names.

pg.query(SQL`SELECT * FROM ${SQL.ident('users')} WHERE id = ${15}`)

where

pg.query(SQL`SELECT * FROM users ${SQL.where({ id: 15, name: 'foo' })}`)
pg.query(SQL`SELECT * FROM users ${SQL.where({ id: 15, age: SQL.op('>', 30) })}`)

set

pg.query(SQL`UPDATE users ${SQL.set({ name: 'foo', age: 40 })} WHERE id = ${33}`)

values

pg.query(SQL`INSERT INTO users ${SQL.values({ name: 'foo', age: 40 })}`)
pg.query(SQL`INSERT INTO users ${SQL.values([{ name: 'foo', age: 40 }, { name: 'bar', age: 50 }])}`)

spread

pg.query(SQL`SELECT * FROM users WHERE tags IN (${SQL.spread(['foo', 'bar', 'baz'])})`)
1.1.0

7 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago