yepql
v2.2.1
Published
Javascript & SQL rethought
Downloads
8
Readme
yepql - Javascript & SQL rethought
Yepql is a javascript library for using SQL.
It is heavily inspired by yesql
Install
$ yarn add yepql
Driver
You'll need a database driver
|Database|npm install --save
package|
|---|---|
|PostgreSQL|pg|
|SQL Server|mssql|
Use
-- get-users-by-id.sql
select *
from Users
where Users.ID = @ID
// db.js
var mssql = require("mssql");
var yepql = require("yepql")(mssql);
var getUsersByID = yepql.makeQuery("./get-users-by-id.sql");
module.exports = { getUsersByID };
// env.js
module.exports = {
connectionString: process.env.CONNECTION_STRING || "Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true";
}
var db = require("./db");
var env = require("./env");
db
.getUsersByID(env.connectionString, { ID: 1 })
.then((rows) => {
/*
rows === [{
ID: 1,
Username: 'username',
Email: '[email protected]',
...
}]
*/
})
.catch(/* handle error */)
Have a lot of sql files?
-- sql/getUsersByID.sql
select *
from Users
where Users.ID = @ID
-- sql/getUsersByEmail.sql
select *
from Users
where Users.Email = @Email
// db.js
var yepql = require("yepql");
var queries = yepql.makeQueries("./sql");
/*
queries === {
getUsersByID: function(connectionString, params),
getUsersByEmail: function(connectionString, params)
}
*/
module.exports = queries;