yesql
Read named SQL statements from .sql files. Also named parameters for prepared statements.
Last updated a year ago by pihvi .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install yesql 
SYNC missed versions from official npm registry.

Greenkeeper badge Known Vulnerabilities

Read named SQL statements from .sql files and/or use named parameters in prepared statements.

Read named SQL statements from .sql files

Put your statements in a .sql file and name them with a comment above. e.g. /myproject/sql/pokemon.sql

-- getPokemon
SELECT * from pokemon
  WHERE id = ?; -- raw style

-- addPokemon
INSERT INTO pokemon(name, price)
  VALUES ($name, $price); -- SQLite named parameter style

-- updatePokemon
UPDATE pokemon
  SET price = :price; -- PostgreSQL / MySQL named parameter style

Raw / SQLite

Use them in code by giving the directory where .sql files(s) are

var sql = require('yesql')('/myproject/sql/')
var db = new sqlite3.Database('/myproject/sql/db.sqlite3')

db.all(sql.getPokemon, 1337, function(err, rows) {...})

db
  .prepare(sql.addPokemon)
  .run({name: 'pikachu', price: 99}, function(err) {...}

MySQL / MariaDB

Prepared statements for MySQL / MariaDB are supported

var sql = require('yesql')('/myproject/sql/', {type: 'mysql'})
var named = require('yesql').mysql
var mysql = require('mysql').createConnection...

// read from file
mysql.query(sql.updatePokemon({price: 5}), function(err, result) {...})

// use only named parameters
mysql.query(named('UPDATE ::ptable SET price = :price;')({price: 5, ptable: 'pokemon'}), function(err, result) {...})

PostgreSQL

Prepared statements for node-postgres (pg) are supported

var sql = require('yesql')('/myproject/sql/',  {type: 'pg'})
var named = require('yesql').pg
var pg = require('pg').connect...

// read from file
pg.query(sql.updatePokemon({price: 5}), function(err, result) {...})

// use only named parameters
pg.query(named('UPDATE pokemon SET price = :price;')({price: 5}), function(err, result) {...})

Changelog

3.2.2
3.2.1
  • Add security build and badge
  • Update deps
3.2.0
  • Support Windows new lines
3.1.6
  • Add CI build and Greenkeeper check
  • Update dev dependencies
3.1.5
  • Add MySQL table name as parameter to example
3.1.4
  • Fix pg type cast and docs
3.1.1
  • Support mysql prepared statements
2.6.0
  • Support pg prepared statements

Current Tags

  • 3.2.2                                ...           latest (a year ago)

11 Versions

  • 3.2.2                                ...           a year ago
  • 3.2.1                                ...           2 years ago
  • 3.2.0                                ...           2 years ago
  • 3.1.6                                ...           2 years ago
  • 3.1.5                                ...           3 years ago
  • 3.1.4                                ...           3 years ago
  • 3.1.3                                ...           3 years ago
  • 3.1.2                                ...           3 years ago
  • 3.1.1                                ...           3 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.11                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 13
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |