Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

sql-composer

0.1.1 • Public • Published

node-sql-composer

Composable SQL template strings for Node.js

build status npm version

Description

Programmatically constructing SQL queries is a tedious process when composing subqueries and collecting query parameters. This library utilizes ES6 tagged template strings to allow for easy parameterized query creation and composition.

Installation

npm install --save sql-composer

Usage

import { sql, rawsql } from 'sql-composer';
 
// basic query
const query = sql`SELECT * FROM Users`;
expect(query).to.deep.equal({
  text: 'SELECT * FROM Users',
  values: []
});
 
// parameterized query
const userId = 1;
const query = sql`SELECT * FROM Users WHERE UserID = ${userId}`;
expect(query).to.deep.equal({
  text: 'SELECT * FROM Users WHERE UserID = ?',
  values: [1]
});
 
// query composition
const userId = 1;
const where = sql`WHERE UserID = ${userId}`;
const query = sql`SELECT * FROM Users ${where}`;
expect(query).to.deep.equal({
  text: 'SELECT * FROM Users WHERE UserID = ?',
  values: [1]
});
 
// raw string (not parameterized)
const table = rawsql('Users');
const query = sql`SELECT * FROM ${table}`;
expect(query).to.deep.equal({
  text: 'SELECT * FROM Users',
  values: []
});

License

MIT

Install

npm i sql-composer

DownloadsWeekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • avatar