• Stars
    star
    263
  • Rank 155,624 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight SQL query builder
sqliterally

Composable and safe parameterized queries using tagged template literals


SQLiterally makes it easy to compose safe parameterized SQL queries using template literals. Clauses are automatically arranged which means you can re-use, subquery and append new clauses as you like – order doesn't matter. All queries are well formatted and ready to be passed directly to node-pg and mysql.

Use SQLiterally as a lightweight alternative to extensive query builders like Knex.js or when big ORMs are over-kill.

OBS: SQLiterally provides a lot of freedom by design and it's not meant to reduce the SQL learning curve. It won't prevent you from writing incorrect queries.

Features

  • Build queries programmatically
  • Works directly with node-pg and mysql
  • Supports nested sub-queries
  • Queries are parametrized to protect against SQL injections
  • Write SQL as you like with no restrictions using string literals
  • Produces well-formatted queries with line breaks
  • Lightweight with no dependencies!

This module exposes two module definitions:

  • ES Module: dist/sqliterally.mjs
  • CommonJS: dist/sqliterally.js

Installation

npm install sqliterally --save

Usage

The module exposes two functions:

  • sql: Use this to construct any query. Useful for complex SQL scripts or when you know the full query and all you need is a parameterized query object.
  • query: Use this to programmatically compose parameterized queries. Useful for constructing queries as you go.
import {sql, query} from 'sqliterally';

let movie = 'Memento', year = 2001;

sql`SELECT director FROM movies WHERE title = ${movie}`;
// => {
//  text: 'SELECT director FROM movies WHERE title = $1'
//  sql => 'SELECT director FROM movies WHERE title = ?'
//  values => ['Memento']
// }

let q = query
   .select`director`
   .select`year`
   .from`movies`
   .where`title = ${movie}`
   .limit`5`;

if (year) q = q.where`year >= ${year}`;
if (writers) q = q.select`writers`;

q.build();
// => {
//  text: `SELECT director, year FROM movies WHERE title = $1 AND year >= $2 LIMIT 5'
//  sql => 'SELECT director, year FROM movies WHERE title = ? AND year >= ? LIMIT 5'
//  values => ['Memento', 2001]
// }

API

sql`string`

Returns: Object

The string can contain nested SQLiterally query and sql objects. Indexes and values are taken care of automatically.

You can pass this directly to node-pg and mysql.

let name = 'Harry Potter';
let max = 10, min = 0;

sub = sql`age > ${min} AND age < ${max}`;
sql`SELECT * FROM x WHERE name = ${name} OR (${sub}) LIMIT 2`;
// => {
//  text: 'SELECT * FROM x WHERE name = $1 OR (age > $2 OR age < $3) LIMIT 2',
//  sql: 'SELECT * FROM x WHERE name = ? OR (age > ? OR age < ?) LIMIT 2',
//  values: ['Harry Potter', 0, 10]
// }

let script = sql`
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.modified = now();
    RETURN NEW;
END;
$$ language 'plpgsql';
`
// => { text: 'CREATE OR REPL...', sql: 'CREATE OR REPL...' values: [] }

text

Type: String

Getter that returns the parameterized string for Postgres.

sql

Type: String

Getter that returns the parameterized string for MySQL.

values

Type: Array

Getter that returns the corresponding values in order.

query

Build a query by adding clauses. The order in which clauses are added doesn't matter. The final output is sorted and returned in the correct order no matter what order you call the methods in.

You can nest as many query and sql as you like. You don't have to build sub-queries before nesting them.

query is immutable and all method calls return a new instance. This means you can build up a base query and re-use it. For example, with conditional where clauses or joins.

OBS: If you call a method multiple times, the values are concatenated in the same order you called them.

let age = 13, limit = 10, page = 1, paginate = false;

let sub = query
    .select`id`
    .from`customers`
    .where`salary > 45000`;

let main = query
    .select`*`
    .from`customers`
    .where`age > '${age}'`
    .where`id IN (${sub})`;

main = paginate ? main.limit`${limit} OFFSET ${limit * page}` : main;

main.build();

build(delimiter?)

Constructs the final query and returns a sql query object ready for node-pg and mysql.

You can still append to the returned sql object or use it as a sub-query. You don't have to call .build() when nesting queries – there's no reason to call build before you need the parameterized string and values.

delimiter

Type: String
Default: \n

Change the delimiter used to combine clauses. The default is a line break.

select`string`

Returns: query

All .select calls get reduced and joined with , on .build().

query.select`*`.build()
// => SELECT *
query.select`cat`.select`zebra`.build()
// => SELECT cat, zebra
query.select`cat, dog`.select`zebra`.build()
// => SELECT cat, dog, zebra
query.select`something`.select`5 * 3 AS result`.build()
// => SELECT something, 5 * 3 AS result

update`string`

Returns: query

Calling .update more than once result in the clause being overwritten.

query.update`film`.build()
// => UPDATE film
query.update`film`.update`books`.build()
// => UPDATE books

set`string`

Returns: query

All .set calls get reduced and joined with , on .build().

query.set`a = b`.build()
// => SET a = b
query.set`a = b`.set`z = y`.build()
// => SET a = b, z = y

from`string`

Returns: query

Calling .from more than once result in the clause being overwritten.

query.from`film`.build()
// => FROM film
query.from`film AS f`.build()
// => FROM film AS f
query.from`film`.from`books`.build()
// => FROM books

join`string`

Returns: query

query.join`c ON d`.build()
// => JOIN c ON d
query.join`a ON b.id`.join`c ON d`.build()
// => JOIN a ON b.id\nJOIN c ON d

leftJoin`string`

query.leftJoin`c ON d`.build()
// => LEFT JOIN c ON d
query.leftJoin`a ON b.id`.leftJoin`c ON d`.build()
// => LEFT JOIN a ON b.id\nLEFT JOIN c ON d

where`string`

Returns: query

All .where calls get reduced and joined with AND on .build().

query.where`a < b`.build()
// => WHERE a < b
query.where`a < b`.where`z = y`.build()
// => WHERE a < b AND z = y
query.where`a = z OR a = y`.build()
// => WHERE a = z OR a = y

orWhere`string`

Returns: query

All .orWhere calls get reduced and joined with OR on .build().

query.orWhere`a < b`.build()
// => WHERE a < b
query.orWhere`a < b`.orWhere`z = y`.build()
// => WHERE a < b OR z = y

having`string`

Returns: query

All .having calls get reduced and joined with AND on .build().

query.having`MAX (list_price) > 4000`
// => HAVING MAX (list_price) > 4000
query.having`MAX (list_price) > 4000`.having`MIN (list_price) < 500`
// => HAVING MAX (list_price) > 4000 AND MIN (list_price) < 500'

orHaving`string`

Returns: query

All .orHaving calls get reduced and joined with OR on .build().

query.orHaving`MAX (list_price) > 4000`
// => HAVING MAX (list_price) > 4000
query.orHaving`MAX (list_price) > 4000`.orHaving`MIN (list_price) < 500`
// => HAVING MAX (list_price) > 4000 OR MIN (list_price) < 500'

groupBy`string`

Returns: query

All .groupBy calls get reduced and joined with , on .build().

query.groupBy`a, b`.groupBy`c`.groupBy`d`.build()
// => GROUP BY a, b, c, d

orderBy`string`

Returns: query

All .orderBy calls get reduced and joined with , on .build().

query.orderBy`a, b`.orderBy`COUNT(c) DESC`.orderBy`d`.build()
// => ORDER BY a, b, COUNT(c) DESC, d

limit`string`

Returns: query

Calling .limit more than once result on the clause being overwritten.

query.limit`5`.build()
// => LIMIT 5
query.limit`5 OFFSET 2`.build()
// => LIMIT 5 OFFSET 2
query.limit`5`.limit`10`.build()
// => LIMIT 10

returning`string`

Returns: query

All .returning calls get reduced and joined with , on .build().

query.returning`a, b`.returning`c`.returning`d`.build()
// => RETURNING a, b, c, d

lockInShareMode

Returns: query

Getter method. Multiple invocations get ignored.

query.lockInShareMode.build()
// => LOCK IN SHARE MODE
query.select`*`.from`x`.lockInShareMode.build()
// => SELECT * FROM x LOCK IN SHARE MODE

forUpdate

Returns: query

Getter method. Multiple invocations get ignored.

query.forUpdate.build()
// => FOR UPDATE
query.select`*`.from`x`.forUpdate.build()
// => SELECT * FROM x FOR UPDATE
query.select`*`.from`x`.lockInShareMode.forUpdate.build()
// => SELECT * FROM x LOCK IN SHARE MODE FOR UPDATE

Credit

This module is inspired by sql-concat but with a different implementation, support for Postgres, single queries and with a reduced API.

The sql function and merge algorithm are based on prepare-sql.

License

MIT © Terkel Gjervig

More Repositories

1

awesome-creative-coding

Creative Coding: Generative Art, Data visualization, Interaction Design, Resources.
HTML
12,228
star
2

prompts

❯ Lightweight, beautiful and user-friendly interactive prompts
JavaScript
8,805
star
3

ramme

Unofficial Instagram Desktop App.
JavaScript
3,319
star
4

tiny-glob

Super tiny and ~350% faster alternative to node-glob
JavaScript
856
star
5

zet

Set() as it should be.
JavaScript
521
star
6

terkelg

A website inside an SVG, inside an image, inside HTML, inside markdown, inside a GitHub readme.md.
TypeScript
231
star
7

facon

Tiny utility (365B) to create DOM elements with manner.
JavaScript
222
star
8

deakins

🎥 Small Canvas 2D Camera
TypeScript
135
star
9

math

Math snippets with graphic programming in mind.
113
star
10

zuckerberg.smile

🤖 Control Mark Zuckerbergs smile property
JavaScript
89
star
11

skaler

A (329B) client-side image resizer.
JavaScript
88
star
12

workshy

A small (376B) lazy function scheduler for a butter smooth main thread.
JavaScript
80
star
13

math-toolbox

Lightweight and modular math toolbox
JavaScript
76
star
14

globrex

Glob to regular expression with support for extended globs.
JavaScript
70
star
15

cantinflas

Tiny mustache-like template engine in ~50 LOC.
JavaScript
65
star
16

sisteransi

ANSI escape codes for some terminal swag.
JavaScript
58
star
17

eliminate

Delete files and directories without all the bullshit.
JavaScript
52
star
18

terkel.com-2016

My Personal website. Build with Vue and ThreeJS.
JavaScript
40
star
19

powerwalker

🏃Walk directories recursively.
JavaScript
28
star
20

hent

A small utility to fetch remote files into buffers
JavaScript
23
star
21

npm-scripts-as-build-tool

Nuggets on how to use NPM Scripts as your build tool. You don't need Grunt or Gulp
21
star
22

cursor-travel

📏 Measure how far your cursor travels.
Swift
19
star
23

favorite-awesomeness

A tiny collection of my favorite awesomelists
18
star
24

shrinktome

📘Shrink facebook by 5% every 10th second. You're welcome!
JavaScript
16
star
25

stopgap

Easily create/remove temporary directories.
JavaScript
16
star
26

simultan

Simultaneously run an async function on any iterable with limited concurrency
JavaScript
15
star
27

cursormuseum

A very brief history of pointing devices
CSS
14
star
28

mkdirplz

Make directories recursively -plz 🙏
JavaScript
13
star
29

antedate

A tiny pre-renderer for client side applications.
JavaScript
13
star
30

webpack-starter

'Just Add Water' Webpack 2, babel and glslify gourmet mix.
JavaScript
13
star
31

starter

No tooling starter because simplicity is the ultimate sophistication.
CSS
13
star
32

1d

🥞Make multi-dimensional arrays as flat as a pancake.
JavaScript
11
star
33

utters

Small (257B) promise wrapper for SpeechSynthesisUtterance
JavaScript
11
star
34

globalyzer

Detect and extract the static part of a glob string.
JavaScript
10
star
35

vimrc

I heard you like escape rooms?
Vim Script
10
star
36

foldersstructure

macOS Automator workflow to scaffold my project directory structure.
10
star
37

threejs-create

Create a generic three.js application for quick prototyping
JavaScript
9
star
38

exclamation

Holy Stratosphere, Robin exclamations❗
JavaScript
7
star
39

invisible

The Invisible Game: Huckle buckle beanstalk
JavaScript
6
star
40

brolly

Internet of Things Umbrella Game.
JavaScript
6
star
41

yeezify

Instantly change all images on any given webpage to Kanye West.
JavaScript
6
star
42

IMDb-Runtimes

A simple Google Chrome extension to convert runtimes on IMDb to hours and minutes.
JavaScript
4
star
43

judgedbycover

A Twitter bot that judge books based on their cover.
JavaScript
3
star
44

dmjx-intro

Website for DMJX freshers' weekend
HTML
1
star
45

terkeliknibe

I created this website after nearly four months of apartment searching
SCSS
1
star