JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q28934F
  • License MIT

Write SQL conveniently

Package Exports

  • friendly-sql

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (friendly-sql) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

friendly-sql

Write SQL conveniently in node.js projects.

This lightweight library provide a way to separate SQL statements from the JavaScript file. Thus, writing and maintaining the SQL can become easier without a lot of string concatenation or joining with array of strings.

Install

npm install friendly-sql

Usage

Provided a SQL file family.sql with sqlite statements (pay attention to the id in the comment)

-- id: Create person
CREATE person (
  person_id   INTEGER PRIMARY KEY,
  first_name  TEXT,
  last_name   TEXT,
  age         INTEGER
);

-- id: Read person
-- inside the braces are the variables to be replaced.
SELECT {field}
FROM person
WHERE first_name = $firstName
ORDER BY {field} {order};

We can use the definitions above in the JavaScript file as follows

var sql = require('friendly-sql').parse('family.sql');

// Get the SQL by id
console.log(sql('Create person'));
/*
CREATE person (
  person_id   INTEGER PRIMARY KEY,
  first_name  TEXT,
  last_name   TEXT,
  age         INTEGER
);
*/

// Get the SQL by id, and provide the value for the variables defined in the sql file.
console.log(sql('Read person', { field: 'first_name', order: 'DESC'}));
/*
SELECT first_name
FROM person
WHERE first_name = $firstName
ORDER BY first_name DESC;
*/

The above examples can be found in the /examples directory. To try it out, clone this repository and run

node examples/family