Query Builder Methods
This section describes all the query builder methods in detail.
Introduction
The main query builder methods all work in a similar way with respect to the arguments they accept. The short form is to pass a string which may have some additional processing applied to it (typically splitting a whitespace delimited string into multiple tokens). Where there are two, three or four possible arguments, an array can be passed. Or an object can be passed with explicit properties for the different parameters.
All the methods can generate raw SQL. This can be specified by passing an object with a single sql property:
db.select({ sql: 'COUNT(id) AS n' })Or using the sql function to create a tagged template literal:
import { sql } from '@abw/badger-database'
db.select(sql`COUNT(id) AS n`)All of the methods can be called multiple times, or passed multiple arguments. They both have the same effect. Generally speaking, you can call the methods in any order and the query builder will generate the SQL query with them in the correct order.
For the sake of brevity these examples assume that you've already connected to a database and stored the connection in the db variable.
import connect from '@abw/badger-database'
const db = connect({ database: 'sqlite:memory' });
// ...examples would go here
db.disconnect()The examples show the SQL generated by the fragment. In many cases they're not complete queries and we've omitted the final .sql() method call which is called internally to generate the SQL.
In these example we're assuming that the database is Sqlite which quotes table and columns names using double quotes, e.g. "users"."id". For Mysql the names will be quoted using backticks. Examples that show placeholders also assume Sqlite (and Mysql) which uses question marks, e.g. ?. For Postgress the placeholders are of the form $1, $2, $3, etc.
Query Types
Read more about the methods for specific query types:
Also see the documentation describing methods for executing queries: