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

brick

v2.0.0

Published

SQL query builder for Node.js

Downloads

44

Readme

Brick

Build SQL strings without attempting to abstract away SQL.

Philosophy:

Writing raw SQL is fun, but there are occasional pain points, such as conditionally appending expressions or converting an object into a WHERE clause. Brick makes those things easier, but otherwise gets out of your way.

A brick in its simplest form is an array of SQL strings and an array of parameters. Bricks can be composed from strings, objects, or other bricks, but ultimately they are reduced down to an SQL string and an array of parameters.

If any parameters are present, the SQL expression must specify their position with a placeholder ?. Parameters can be scalar values or bricks. If a brick is passed as a parameter, it will replace the placeholder when the query is built.

Usage:

Simple brick

var brick = require('brick')
var id = 1
var query = brick('SELECT * FROM events WHERE id = ?', id)
query.build() // => { text: 'SELECT * FROM events WHERE id = $1', params: [1] }

Compose bricks with other bricks

var brick = require('brick')
var conditions = []
conditions.push(brick('category = ?', 'Blues'))
var query = brick('SELECT * FROM events WHERE ?', brick('category = ?', 'Blues'))
query.build() // => { text: 'SELECT * FROM events WHERE category = $1', params: ['Blues'] }

Write subqueries

var brick = require('brick')
var cities = brick('SELECT id FROM cities WHERE name = ?', 'Ann Arbor')
var query = brick('SELECT * FROM events WHERE city_id in (?)', cities)
query.build() // => { text: 'SELECT * FROM events WHERE city_id in (SELECT id FROM cities WHERE name = $1)', params: ['Ann Arbor'] }

Join bricks

var brick = require('brick')
var columns = []
columns.push(brick('id as event_id'))
columns.push('headline')
columns.push('city_id')
var query = brick('SELECT ? FROM events', brick.join(columns))
query.build() // => { text: 'SELECT id as event_id, headline, city_id FROM events', params: [] }

Conditions

var brick = require('brick')
var where = brick.conditions({
  city: 'Ann Arbor',
  category: 'Jazz',
  deleted_at: null
})
var query = brick('SELECT * FROM events WHERE ?', where)
query.build() // => { text: 'SELECT * FROM events WHERE city = ? AND category = ? AND deleted_at IS NULL', params: ['Ann Arbor', 'Jazz'] }

Complex conditions

var brick = require('brick')
var searches = [
  { generic_type: 128, specific_type: 256 },
  { generic_type: 128, specific_type: null },
]

var clauses = searches.map(function(search) {
  return brick.fn.wrap(brick.conditions(search))
})

var where = brick.join(clauses, 'OR')

var query = brick('SELECT * FROM devices WHERE ?', where)
query.build() // => { text: 'SELECT * FROM devices WHERE (generic_type = ? AND specific_type = ?) OR (generic_type = ? AND specific_type IS NULL)', params: ['128', '256'] }

Using Brick with Postgresql

var pg = require('pg')
var brick = require('brick')

var query = brick(/* ... */)

pg.connect('database', function(client, done) {
  client.query(query.build('pg'), function(result) {
    // handle result...
    done()
  })
})