Skip to content

eugeneware/sqlt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlt

Simple SQL Templating helper inspired by Yesql

build status

Give this library the location of a SQL template file, and it will return a function that you can call with a mysql or pg connection, an optional array of parameters, and a callback.

Installation

This module is installed via npm:

$ npm install sqlt

Example Usage

Simple SQL query with no params

Given a SQL template file located in /path/to/queries/getUsers.sql

SELECT
  *
FROM
  users;

You can get a function that is easily callable with a database connection handle, and get a callback:

var sqlt = require('sqlt'),
    mysql = require('mysql');

var conn = mysql.createConnection({
  host: 'yourdatabase.com',
  database: 'yourdbname',
  user: 'yourdbusername',
  password: 'yourpassword'
});

var getUsers = sqlt('/path/to/queries/getUsers.sql');
getUsers(conn, function (err, results) {
  if (err) throw err;
  console.log(results);
});

SQL query with params

Given a SQL template file located in /path/to/queries/getUsersByIdOrEmail.sql

SELECT
  *
FROM
  users
WHERE
  id = ? OR email = ?;

You can get a function that is easily callable with a database connection handle, an array of parameters, and get a callback:

var sqlt = require('sqlt'),
    mysql