0.0.9 • Published 5 years ago
sqlbind v0.0.9
Introduce
A simple SQL parameterized query generator for most SQL language
Install
npm install sqlbind.js
Basic Using
const {sql_bind, sql_value} = require(sqlbind);
const tb_name = "post";
const title = "hello";
const body = "hello world";
const sql = sql_bind`insert into ${tb_name}(title, body) values(${sql_value(title)}, ${sql_value(body)})`
console.log(sql)
/* output
{
str: sql_bind`insert into post(title, body) values(?, ?);`,
bindings: ["hello", "hello world"],
...
}
*/
// then you can use it with other database libary
const knex = requrie("knex")({client: 'pg'});
knex.raw(sql.str, sql.bindings)
// other dialect
console.log(sql.pg)
/* output
{
str: sql_bind`insert into post(title, body) values($0, $1);`,
bindings: ["hello", "hello world"]
}
*/
console.log(sql.mysql)
/* output
{
str: sql_bind`insert into post(title, body) values(?c0, ?c1);`,
bindings: {
"c0": "hello",
"c1": "hello world"
}
}
console.log(sql.mssql)
/* output
{
str: sql_bind`insert into post(title, body) values(@c0, @c1);`,
bindings: {
"c0": "hello",
"c1": "hello world"
}
}
console.log(sql.sqlite)
/* output
{
str: sql_bind`insert into post(title, body) values($c0, $c1);`,
bindings: {
"c0": "hello",
"c1": "hello world"
}
}
*/
API
function sql_bind(expressions: any[])=>SqlBind
sql_bind`string text ${expression} string text`=>SqlBind
alias sb
, generate sql statement with parameters
function sql_value(value: any)=\>SqlValue
alias sv
, stored the value that will parameterized
function is_sql(arg: any)=>boolean
true if it is SqlBind or SqlBind
Nests Usage sqb_bind
TODO
- [] add custom validate
- [] support more dialect