Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

like-sql

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

like-sql

Simple SQL query builder

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by700%
Maintainers
0
Weekly downloads
 
Created
Source

like-sql

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 })

Install

npm i like-sql

Tests

npm run test

License

Code released under the MIT License.

Keywords

FAQs

Package last updated on 26 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc