npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cadena

v1.1.0

Published

sql builder

Downloads

5

Readme

cadena

NPM Version Build Status Node.js Version

cadena helps you to write safer SQL

  • supports parameterized query
  • run() returns promise
  • supports mysql, sqlite3 and rqlite

usage

npm install --save cadena
const { select, connect, query, sql } = require('cadena')
const db = connect('mysql', 'mysql://guest:[email protected]/test')

select('name', 'id')
    .from('players')
    .where({score: { '>=': 90 }})
    .limit(0, 10)
    .orderBy('score').desc().run(db)

// just get SQL as string
select().from('players').toString()

let id = '33b27b80-bee3-4d1b-aa9a-231bf250f344'
sql`select * from tbl where id=${id}`.run(db)

sql`select * from tbl where id=${id}`.toString()
// select * from tbl where id='33b27b80-bee3-4d1b-aa9a-231bf250f344'

select

select(['name', 'id']) // SELECT name, id

select() // SELECT *

where

where({key: 'value'}) // WHERE `key` = 'value'

where({
    score: {between: [80, 90]},
    name: {contains: 'alberto'}
    group: ['A', 'B']
}) // WHERE `score` BETWEEN (80, 90) AND `name` LIKE '%alberto%' AND `group` IN ('A', 'B')

where({key: {'<': min}}).or({key: {'>': max}}) // WHERE `key` < min OR `key` > max

undefined values are ignored

where({key: undefined}) // not output
where({key: undefined, key2: 'value'}) // WHERE `key2` = 'value'
where({key: undefined}).or({key2: 'value'}) // WHERE `key2` = 'value'

nested and/or

where(and(
    or({ n: 1 }, { n: 2 }),
    { k: 1 }))
// WHERE (`n` = 1 OR `n` = 2) AND `k` = 1

a more verbose form

where({
    $and: [
        { $or: [{n: 1}, {n: 2}] },
        { k: 1 }
    ]
})

orderBy

orderBy('field1', 'field2') // ORDER BY `field1`, `field2`

orderBy(['field1', 'field2']) // ORDER BY `field1`, `field2`
orderBy(['-field1', 'field2']) // ORDER BY `field1` DESC, `field2`

orderBy('field1', 'ASC', 'field2', 'DESC') // ORDER BY `field1` ASC, `field2` DESC

orderBy('field1').desc().orderBy('field2').asc() // ORDER BY `field1` DESC, `field2` ASC

join

select('name', 'id').from('tbl').leftJoin('tbl2').on({tbl.id: "tbl2.tblId"})

subquery

let tbl = select('name', 'id').from('tbl')
select().from(tbl) // SELECT * FROM (SELECT `name`, `id` FROM `tbl`)

count

let query = select().from('tbl').where({key: value}).orderBy('created_at').desc()
query.count().sql // SELECT COUNT(*) AS `total` FROM `tbl` WHERE `key` = 'value'

insert

insert({name: 'foo', score: 1}).into('players')
insert(['name', 'score'], [['foo', 1], ['bar', 2]]).into('players')

upsert

upsert({name: 'foo', score: 1}).into('players')

update

update('players').set({name: 'foo', score: 1}).where({id: 1})
update('players').set('name', 'foo').set('score', 1).limit(1)

delete

del('players').where({id: 1})
del('players').where({name: {contains: 'alberto'}}).limit(1)

parameterized query

const { sql } = require('cadena')
let name = 'first'
let id = 1
sql`insert into tbl set name=${name} where id=${id}`.run(db)

database intergration

mysql

const db = cadena.connect('mysql', 'mysql://root:@127.0.0.1/test')

sqlite3

const db = cadena.connect('sqlite3', ':memory:')

rqlite

const db = cadena.connect('rqlite', 'http://127.0.0.1:4001')