Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
like-sql
Advanced tools
Readme
Simple SQL query builder
const SQL = require('like-sql')
const builder = new SQL()
// CREATE DATABASE IF NOT EXISTS `myapp`
const [sql] = builder.createDatabase('myapp')
// DROP DATABASE IF EXISTS `myapp`
const [sql] = builder.dropDatabase('myapp')
// INSERT INTO `ips` (`addr`, `hits`) VALUES (?, ?)
const [sql, values] = builder.insert('ips', { addr: req.ip, hits: 0 })
// SELECT `addr`, `hits` FROM `ips` WHERE addr = ?
const [sql, values] = builder.select('ips', ['addr', 'hits'], 'addr = ?', req.ip)
// SELECT `addr`, `hits` FROM `ips` WHERE addr = ? LIMIT 1
const [sql, values] = builder.selectOne('ips', ['addr', 'hits'], 'addr = ?', req.ip)
// SELECT EXISTS(SELECT 1 FROM `ips` WHERE addr = ? LIMIT 1)
const [sql, values] = builder.exists('ips', 'addr = ?', req.ip)
// SELECT COUNT(1) FROM `ips` WHERE addr = ?
const [sql, values] = builder.count('ips', 'addr = ?', req.ip)
// UPDATE `ips` SET `hits` = ? WHERE addr = ? LIMIT 1
const [sql, values] = builder.update('ips', { hits: 1 }, 'addr = ? LIMIT 1', req.ip)
// UPDATE `ips` SET `hits` = hits + ? WHERE addr = ?
const [sql, values] = builder.update('ips', [{ hits: 'hits + ?' }, 1], 'addr = ?', req.ip)
// DELETE FROM `ips` WHERE addr = ? LIMIT 1
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 })
npm i like-sql
npm run test
Code released under the MIT License.
FAQs
Simple SQL query builder
The npm package like-sql receives a total of 95 weekly downloads. As such, like-sql popularity was classified as not popular.
We found that like-sql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.