Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eknkc/node-sqltag
SQL generation using ES6 tagged template strings
https://github.com/eknkc/node-sqltag
Last synced: about 2 months ago
JSON representation
SQL generation using ES6 tagged template strings
- Host: GitHub
- URL: https://github.com/eknkc/node-sqltag
- Owner: eknkc
- Created: 2016-03-08T22:00:31.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-16T06:49:53.000Z (almost 8 years ago)
- Last Synced: 2024-10-13T01:36:52.748Z (3 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sqltag
SQL generation using ES6 tagged template strings for [mysql](https://www.npmjs.com/package/mysql) / [mysql2](https://www.npmjs.com/package/mysql2) and [postgres](https://www.npmjs.com/package/pq).
## install
```sh
npm install sqltag
```## basic usage
You can use `${}` syntax for value interpolation.
```js
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.
```js
pg.query(SQL`SELECT * FROM ${SQL.raw('users')} WHERE id = ${15}`)
```### ident
Identifiers are escaped according to database engine. Use for table / field names.
```js
pg.query(SQL`SELECT * FROM ${SQL.ident('users')} WHERE id = ${15}`)
```### where
```js
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
```js
pg.query(SQL`UPDATE users ${SQL.set({ name: 'foo', age: 40 })} WHERE id = ${33}`)
```### values
```js
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
```js
pg.query(SQL`SELECT * FROM users WHERE tags IN (${SQL.spread(['foo', 'bar', 'baz'])})`)
```