like-sql
Simple SQL query builder
const SQL = require('like-sql')
const builder = new SQL()
const [sql] = builder.createDatabase('myapp')
const [sql] = builder.dropDatabase('myapp')
const [sql, values] = builder.insert('ips', { addr: req.ip, hits: 0 })
const [sql, values] = builder.select('ips', ['addr', 'hits'], 'addr = ?', req.ip)
const [sql, values] = builder.selectOne('ips', ['addr', 'hits'], 'addr = ?', req.ip)
const [sql, values] = builder.exists('ips', 'addr = ?', req.ip)
const [sql, values] = builder.count('ips', 'addr = ?', req.ip)
const [sql, values] = builder.update('ips', { hits: 1 }, 'addr = ? LIMIT 1', req.ip)
const [sql, values] = builder.update('ips', [{ hits: 'hits + ?' }, 1], 'addr = ?', req.ip)
const [sql, values] = builder.delete('ips', 'addr = ? LIMIT 1', req.ip)
Extends:
const SQL = require('like-sql')
class MySQL extends SQL {
constructor (opts = {}) {
super(opts)
this.pool = mysql2.createPool(...)
}
async _insert (sql, values) {
const [res] = await this.pool.execute(sql, values)
return res.insertId
}
async _select (sql, values) {
...
}
async _selectOne (sql, values) {
...
}
}
const db = new MySQL()
const id = await db.insert('ips', { addr: req.ip, hits: 0 })
Install
npm i like-sql
Tests
npm run test
License
Code released under the MIT License.