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

js-sql-syntax

v1.1.4

Published

Create SQL-queries using JavaScript functions

Downloads

12

Readme

JS-SQL-SYNTAX

Example usage

const sql = require('js-sql-syntax')

const syntax = sql().select(['col1', 'col2']).from('myTable').where({'col1': '1'})

const query = syntax.getQuery()
// SELECT col1, col2 FROM myTable WHERE 1=1 AND col1=?

const values = syntax.getValues()
// ['1']

API

sql()

Returns a new SQL instance

.getQuery()

Returns the generated SQL query as a string from an instance

.getValues()

Returns an array of values matching the order of placeholders in the query

.addCustomFunc(name, func)

Add a custom function

const query = sql()
query.addCustomFunc('foo', (arg1, arg2) => {
  /**
   * The function can take any arguments
   */

  /**
   * Should return an array where the first value (required)
   * is the generated SQL-string and the second value (optional)
   * is an array of values
   */
  return ['FOO', ['val1', 'val2']]
})

// Call your custom function like this
query.custom.foo(arg1, arg2)

// Chaining is still supported, with or without default functions
query.custom.foo(arg1, arg2).select().from('myTable')

SQL functions

.select([cols])

cols Unescaped An array of strings representing columns to select, defaults to *
Renders: SELECT *

.insert()

Renders: INSERT

.update([table])

table Unescaped The name of the table to update as a string
Renders: UPDATE table

.delete()

Renders: DELETE

.from(table)

table Unescaped A tablename as a string
Renders: FROM table

.where(query)

query Unescaped keys, escaped values An object where key-value pairs renders patterns.

Using equals

const q = sql().where({ foo: 'bar' }).getQuery()
// WHERE 1=1 AND foo=?

Using arrays (will become an IN-statement)

const q = sql().where({ foo: ['bar', 'baz'] }).getQuery()
// WHERE 1=1 AND foo IN (?, ?)

Using other operators
Valid operators are $gt, $gte, $lt, $lte, $eq
Trying to use an invalid operator will throw an error with code ERR_WHERE_INVALID_OPERAND

const q = sql().where({ foo: { $gt: 1 })).getQuery()
// WHERE 1=1 AND foo>?

Renders: WHERE 1=1 AND foo=?
Renders: WHERE 1=1 AND bar IN (?,?,?)
Renders: WHERE 1=1 AND foo>?

.in(vals)

vals Escaped An array of values.
Renders: IN (?, ?, ?)

.values(values)

values Unescaped keys, escaped values An object or array of objects where keys will translate to columns.

const values = [{
  'col1': 'val1',
  'col2': 'val2'
},{
  'col1': 'val3',
  'col2': 'val4'
}]

sql().values(values)
// (col1,col2) VALUES (?,?),(?,?)

Renders (single object): (col1,col2) VALUES (?,?)
Renders (array of objects): (col1,col2) VALUES (?,?),(?,?)

.into(table)

table Unescaped The name of a table as a string
Renders: INTO table

.set(data)

data Unescaped keys, escaped values An object where keys will translate to columns
Renders: SET col1=?, col2=?

.on(col, val)

col Unescaped The name of a column as a string
val Unescaped A value, will be cast to a string

TODO: Add option to escape values

Renders: ON col=val

.and(col, val)

col Unescaped The name of a column as a string
val Escaped A value as any type supported by the database connection

TODO: Add option to leave values unescaped

Renders: AND col=?

.subquery(query)

query Another SQL-query instance, values will be appended to the primary query
Renders: ( The query provided by query.getQuery() within parentheses )

.leftJoin([table])

table Unescaped The name of the table to join as a string, defaults to an empty string
Renders: LEFT JOIN table

.rightJoin([table])

table Unescaped The name of the table to join as a string, defaults to an empty string
Renders: RIGHT JOIN table

.innerJoin([table])

table Unescaped The name of the table to join as a string, defaults to an empty string
Renders: INNER JOIN table

.limit(n)

n Escaped The limit as an integer
Renders: LIMIT ?

.offset(n)

n Escaped The offset as an integer
Renders: OFFSET ?

.as(alias)

alias Unescaped An alias as a string
Renders: AS alias

.orderBy(col)

col Unescaped The column to order by as a string
Renders: ORDER BY col

.groupBy(col)

col Unescaped The column to group by as a string
Renders: GROUP BY col

.descending()

Renders: DESC

.ascending()

Renders: ASC

.onDuplicateKeyUpdate(data)

data Unescaped keys, escaped values An object where keys translate to columns
Renders: ON DUPLICATE KEY UPDATE col1=?, col2=?

.raw(sql, values)

sql Unescaped A string with SQL code
values Escaped An array of values to push to the prepared statement
Renders: The sql string