GitXplorerGitXplorer
r

sql-tag

public
35 stars
1 forks
1 issues

Commits

List of commits on branch master.
Unverified
f2802ca2027a050e9e4ae9a86458081cfdb18153

Release 1.0.1

committed 8 years ago
Unverified
8cae6c908b7c996a38e007155538665329e2fdd9

Update examples in README.md

committed 8 years ago
Unverified
1c9ed399a419311746c7df644bf66dd0472019e4

Merge pull request #7 from seegno/bugfix/whitespacing-removal

rruimarinho committed 8 years ago
Unverified
7c4df7f2c1f800e57a5710299e5c546303a2840c

Remove whitespace manipulation to avoid unintended consequences

committed 8 years ago
Unverified
91770c4f4e94e8874d6b7befb163af7e00afcb05

Add .npmignore

committed 8 years ago
Unverified
1fc4ccf51720a3457e6f2dcd1599459f0349fa54

Release 1.0.0

committed 8 years ago

README

The README file for this repository.

sql-tag

A template tag for writing elegant parameterized SQL queries based on ES2015 tagged template literals.

Compatible with pg, pg-native, mysql and mysql2. Read more about sequelize support.

Status

npm version build status

Installation

Install the package via npm:

$ npm install --save sql-tag

Usage

Arguments

  1. query (string): The sql query.
  2. [...*] (...*): The query replacements.

Returns

(Object): A structured object with the sql query string and its replacements.

Examples

const sql = require('sql-tag');
const out = sql`SELECT * FROM biz WHERE id = ${'foo'}`;
// => { sql: 'SELECT * FROM biz WHERE id = ?', query: 'SELECT * FROM biz WHERE id = $1', values: ['foo'] }
const sql = require('sql-tag');
const foo = 'bar';
const out = sql`SELECT * FROM biz
  WHERE id = ${foo}
`;
// => { sql: 'SELECT * FROM biz\n  WHERE id = ?\n', query: 'SELECT * FROM biz\n  WHERE id = $1\n', values: ['bar'] }

The tag itself is framework agnostic. It should just require a small modification to the query generator function.

NOTE: the sql tag does not provide any kind of escaping safety. It delegates that work to the underlying framework.

Integration with pg/pg-native

The output format is sql-tag is directly compatible with pg and pg-native parameterized queries.

const pg = require('pg');
const client = new pg.Client();
const sql = require('sql-tag');

client.connect(function (err) {
  if (err) throw err;

  client.query(sql`SELECT * FROM foo WHERE id = ${'foo'}`).then(console.log);
});

Integration with mysql/mysql2

const mysql = require('mysql');
const connection = mysql.createConnection({ user: 'root', password: 'root' });
const sql = require('sql-tag');

connection.query(sql`SELECT * FROM foo WHERE id = ${'foo'}`, (err, rows) => console.log(err, rows));

Integration with sequelize

Sequelize requires a special format to be able to handle parameterized queries. Check out the sequelize-sql-tag plugin which builds on top of sql-tag to provide this functionality.

Tests

npm test

Release

npm version [<newversion> | major | minor | patch] -m "Release %s"

License

MIT