SQL string maker.
npm install sqlmaster --save
const SQLMaster = require('sqlmaster');
SQLMaster.init({
prepareType: '$',
xssFilter: true,
})
// prepareType: '$' => SELECT * FROM users WHERE id = $1
// prepareType: 'tag' => SELECT * FROM users WHERE id = :id
It returns the output of the entire query.
SQLMaster
.from('users')
.delete()
.where('id = :id', {
':id': 65536
})
.exec()
/*
{
text: 'DELETE FROM users WHERE id = $1',
values: [
65536
]
}
*/
It sets the query's master table.
SQLMaster.from('users')
It sets the query's 'select params'.
SQLMaster
.from('users')
.select([
"name",
["email", "RTRIM(email)"],
["count", "COUNT(*)"],
])
// SELECT name, RTRIM(email) AS email, COUNT(*) AS count FROM users
It sets the query's 'where'.
SQLMaster
.from('users')
.where("id = :id", {
':id': 65536
})
.select([
"*",
])
// SELECT name, RTRIM(email) AS email, COUNT(*) AS count FROM users WHERE id = $1
It sets the query's 'group by'.
SQLMaster
.from('users')
.groupBy('name, email')
.select([
"name",
[ "email", "RTRIM(email)"],
["count", "COUNT(*)"]
])
// SELECT name, RTRIM(email) AS email, COUNT(*) AS count FROM users GROUP BY name, email
It sets the query's 'order by'.
SQLMaster
.from('users')
.orderBy('name, email')
.select([
"name",
[ "email", "RTRIM(email)"],
])
// SELECT name, RTRIM(email) AS email, COUNT(*) AS count FROM users GROUP BY name, email
It sets the query's 'order by desc'.
SQLMaster
.from('users')
.orderBy('name, email')
.desc()
.select([
"*"
])
// SELECT * FROM users ORDER BY name, email DESC
It sets the query's 'order by asc'.
SQLMaster
.from('users')
.orderBy('name, email')
.asc()
.select([
"*"
])
// SELECT * FROM users ORDER BY name, email ASC
It sets the query's 'limit'.
SQLMaster
.from('users')
.orderBy('name, email')
.desc()
.select([
"*"
])
// SELECT * FROM users LIMIT 200
It sets the query's 'returning'.
SQLMaster
.from('users')
.insert({
name: "GitHub Support",
email: "[email protected]",
date: new Date(),
})
.returning('id')
// INSERT INTO users (name, email, date) VALUES($1, $2, $3) RETURNING id
It sets the query's 'insert'.
SQLMaster
.from('users')
.insert({
name: "GitHub Support",
email: "[email protected]",
date: new Date(),
})
// INSERT INTO users (name, email, date) VALUES($1, $2, $3)
It sets the query's 'update'.
SQLMaster
.from('users')
.update({
name: "GitHub Support",
email: "[email protected]",
date: new Date(),
})
.where('id = :id', {
':id': 65536
})
// UPDATE users SET name = $1, email = $2, date = $3 WHERE id = $4
It sets the query's 'delete'.
SQLMaster
.from('users')
.delete()
.where('id = :id', {
':id': 65536
})
// DELETE FROM users WHERE id = $1
@mlelyakan - SQLMaster Icon