All Projects → LuKks → like-mysql

LuKks / like-mysql

Licence: MIT license
Simple and intuitive ORM for MySQL

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to like-mysql

resty
A Node.js framework
Stars: ✭ 20 (-16.67%)
Mutual labels:  simple
InstaBot
Simple and friendly Bot for Instagram, using Selenium and Scrapy with Python.
Stars: ✭ 32 (+33.33%)
Mutual labels:  like
M2A01 MuSimpron
Small yet powerful state machine coroutine library
Stars: ✭ 34 (+41.67%)
Mutual labels:  simple
Lang-app
Add a multi lang configuration to your WEB APP 'from scratch' [ANY FRAMEWORK, ANY PLUGIN, ANY API]
Stars: ✭ 15 (-37.5%)
Mutual labels:  simple
classy
Super simple text classifier using Naive Bayes. Plug-and-play, no dependencies
Stars: ✭ 12 (-50%)
Mutual labels:  simple
FireSnapshot
A useful Firebase-Cloud-Firestore Wrapper with Codable.
Stars: ✭ 56 (+133.33%)
Mutual labels:  simple
WHMCS-Discord-Notifications
A hook to push a range of different WHMCS notifications instantly to a Discord channel.
Stars: ✭ 52 (+116.67%)
Mutual labels:  simple
logquacious
Logquacious (lq) is a fast and simple log viewer.
Stars: ✭ 55 (+129.17%)
Mutual labels:  simple
agile
🌌 Global State and Logic Library for JavaScript/Typescript applications
Stars: ✭ 90 (+275%)
Mutual labels:  simple
Physics
experimenting with physics simulation
Stars: ✭ 53 (+120.83%)
Mutual labels:  simple
hexo-theme-chord
a simple hexo theme.
Stars: ✭ 15 (-37.5%)
Mutual labels:  simple
Semantic-Segmentation-BiSeNet
Keras BiseNet architecture implementation
Stars: ✭ 55 (+129.17%)
Mutual labels:  simple
DevFolio
A Modern Portfolio Template for Developers with easy setup process documented(with hosting).
Stars: ✭ 96 (+300%)
Mutual labels:  simple
node-perj
A fast, flexible JSON logger.
Stars: ✭ 16 (-33.33%)
Mutual labels:  simple
fresh
Fresh is a free blog template for Jekyll
Stars: ✭ 48 (+100%)
Mutual labels:  simple
tressa
Little test utility
Stars: ✭ 18 (-25%)
Mutual labels:  simple
MatrixLib
Lightweight header-only matrix library (C++) for numerical optimization and machine learning. Contact me if there is an exciting opportunity.
Stars: ✭ 35 (+45.83%)
Mutual labels:  simple
QuickNotes
一款简单、轻量、高效的Android记事、记账应用
Stars: ✭ 19 (-20.83%)
Mutual labels:  simple
react-edit-text
Simple 'click to edit' editable text component for React
Stars: ✭ 28 (+16.67%)
Mutual labels:  simple
JHLikeButton
❤️点赞动画,点赞星星,点赞爱心,抖音点赞 ❤️
Stars: ✭ 41 (+70.83%)
Mutual labels:  like

like-mysql

Simple and intuitive ORM for MySQL

const mysql = require('like-mysql')

// create a pool easily with good defaults
const db = mysql('127.0.0.1:3306', 'root', 'secret', 'myapp')

// wait until a connection is established
await db.ready()

// INSERT INTO `ips` (`addr`, `hits`) VALUES (?, ?)
const id = await db.insert('ips', { addr: req.ip, hits: 0 })

// SELECT `addr`, `hits` FROM `ips` WHERE addr = ?
const rows = await db.select('ips', ['addr', 'hits'], 'addr = ?', req.ip)

// SELECT `addr`, `hits` FROM `ips` WHERE addr = ? LIMIT 1
const row = await db.selectOne('ips', ['addr', 'hits'], 'addr = ?', req.ip)

// SELECT EXISTS(SELECT 1 FROM `ips` WHERE addr = ? LIMIT 1)
const exists = await db.exists('ips', 'addr = ?', req.ip)

// SELECT COUNT(1) FROM `ips` WHERE addr = ?
const count = await db.count('ips', 'addr = ?', req.ip)

// UPDATE `ips` SET `hits` = ? WHERE addr = ? LIMIT 1
await db.update('ips', { hits: 1 }, 'addr = ? LIMIT 1', req.ip)

// UPDATE `ips` SET `hits` = hits + ? WHERE addr = ?
await db.update('ips', [{ hits: 'hits + ?' }, 1], 'addr = ?', req.ip)

// DELETE FROM `ips` WHERE addr = ? LIMIT 1
await db.delete('ips', 'addr = ? LIMIT 1', req.ip)

// getConnection, beginTransaction, callback, commit/rollback, release
await db.transaction(async function (conn) {
  const id = await conn.insert('users', { username: 'lukks', ... })
  await conn.insert('profiles', { owner: id, ... })
})

// execute
const [res, fields] = await db.execute('SELECT * FROM `ips` WHERE `addr` = ?', [req.ip])

// query
const [res, fields] = await db.query('SELECT * FROM `ips` WHERE `addr` = "8.8.8.8"')

// end pool
await db.end()

Install

npm i like-mysql

Description

node-mysql2 is used to create the MySQL pool.
like-sql is used to build the SQL queries.
Operations are prepared statements made by execute.
Promise version. All custom methods are also promised.

Automatic WHERE when find argument doesn't start with:
ORDER BY, LIMIT or GROUP BY

Examples

constructor

// host:port
const db = mysql('127.0.0.1:3306', 'root', 'secret', 'mydb')

// socketPath
const db = mysql('/var/lib/mysql/mysql.sock', 'root', 'secret', 'mydb')

ready

Wait for database started by docker-compose, etc.

// default timeout (15s)
await db.ready() // will throw in case is not able to connect

// custom timeout
await db.ready(5000)

insert

// with autoincrement id:
const insertId = await db.insert('ips', { addr: req.ip, hits: 0 })
console.log(insertId) // => 1336

// otherwise it always returns zero:
const insertId = await db.insert('config', { key: 'title', value: 'Database' })
console.log(insertId) // => 0

select

const rows = await db.select('ips', ['*'], 'addr = ?', req.ip)
console.log(rows) // => [{ id: 2, addr: '8.8.4.4', hits: 2 }]

const rows = await db.select('ips', ['addr', 'hits'], 'ORDER BY hits DESC')
console.log(rows) // => [{ addr: '8.8.8.8', hits: 6 }, { addr: '8.8.4.4', hits: 2 }, ...]

selectOne

const row = await db.selectOne('ips', ['addr', 'hits'], 'addr = ?', req.ip)
console.log(row) // => { addr: '8.8.4.4', hits: 2 }

const row = await db.selectOne('ips', ['addr', 'hits'], 'addr = ?', '0.0.0.0')
console.log(row) // => undefined

exists

const exists = await db.exists('ips', 'addr = ?', req.ip)
console.log(exists) // => true

count

const total = await db.count('ips', 'addr = ?', req.ip)
console.log(total) // => 2

update

const changedRows = await db.update('ips', { hits: 1 }, 'addr = ?', req.ip)
console.log(changedRows) // => 1

const changedRows = await db.update('ips', [{ hits: 'hits + ?' }, 1], 'addr = ?', req.ip)
console.log(changedRows) // => 1

delete

const affectedRows = await db.delete('ips', 'addr = ?', req.ip)
console.log(affectedRows) // => 1

transaction

Normally with a pool you do something like:

  • conn = pool.getConnection()
  • conn.beginTransaction()
  • conn.execute('INSERT INTO users (username, password) VALUES (?, ?)')
  • conn.execute('INSERT INTO profile (owner, name) VALUES (?, ?)')
  • conn.commit()
  • conn.release()

Also checking different catchs to release and/or rollback.

This method simplifies all that and you just do the important part:

await db.transaction(async function (conn) {
  const id = await conn.insert('users', { username: 'lukks', ... })
  await conn.insert('profiles', { owner: id, ... })
})

You can also return a custom value:

const result = await db.transaction(async function (conn) {
  await conn.insert(...)
  return 'custom value'
})

console.log(result) // => 'custom value'

end

await db.end()

Tests

Start a database instance

docker run --rm -p 3305:3306 -e MYSQL_ROOT_USER=root -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0

Run tests

npm test

Stop container and due --rm will be auto deleted

docker ps
docker stop cc6

License

Code released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].