my-jsql
Easy to use SQL query builder
Install
npm install my-jsql
About
This module allows you to run basic SQL queries by passing JavaScript objects. You also don't need to worry about escaping the variables you pass.
ES8 Ready
async { const jsql = con let first = 'John' let last = 'Doe' // SELECT * FROM users WHERE first='John' AND last='Doe' const results = await jsql console}
Basic Usage
Here are some examples
// As you can see, we are using the mysql module to create a connectionconst mysql = const MyJsql = // Create your connection and pass it to MyJsqlconst con = mysqlconconst jsql = con // Set the tablejsql // SELECT * FROM users WHERE last='Doe' ORDER BY first DESC LIMIT 10 OFFSET 5 jsql // INSERT INTO users (first, last, email) VALUES ('John','Doe','email@email.com')jsql // UPDATE users SET email='email@email.com' WHERE email IS NULL OR NOT first='John'jsql // DELETE FROM users WHERE first='John' AND email IS NOT NULLjsql // Manually write a WHERE condition// SELECT * FROM users WHERE email LIKE 'e%'jsql // Manually write a queryjsql
API
This section is devoted to the API documentation.
MyJsql(connection[, options])
Pass a database connection. Currently only supports the mysql module.
const mysql = const con = mysql con const jsql = con
Refer to mysql's docs to view which connections you can pass.
Optionally pass an options object if you want to auto clear after calling run()
:
const jsql = con autoClear: true
.t(table)
Pass a string of the table name.
// Set the table for the following queriesjsql // SELECT * FROM usersjsql // Set the table while building a query// INSERT INTO users (first) VALUES ('John')jsql
.s([columns[, column]])
Pass a string (or multiple strings) or an array of strings in order to SELECT
certain columns. If nothing is passed, all the columns will be returned SELECT * FROM...
.
// Passing strings// SELECT first, email FROM usersjsql // Passing an array// SELECT first, email FROM usersjsql // SELECT * FROM usersjsql
.i(data)
Pass an object of the data you want to INSERT
, with the keys being the table columns.
// INSERT INTO users (first, last, email) VALUES ('John', 'Doe', 'email@email.com')jsql
.u(data)
Pass an object of the data you want to UPDATE
, with the keys being the table columns.
// UPDATE users SET first='Jane' WHERE id=1jsql
.d()
Nothing needs to be passed for a DELETE FROM
.
// DELETE FROM users WHERE id=1jsql
.w([conditions1[, conditions2[, ...]]])
Pass one or more objects as conditional WHERE
statements. Statements in the same object are separated by AND
. If you pass multiple objects, they are separated by OR
. If nothing is passed, there will be no WHERE...
statement. MyJsql also saves the last where statement, so you don't have to call it again, but this also means you need to clear if you don't want to reuse your previous statement.
// SELECT * FROM users WHERE id=1 AND name='John'jsql // SELECT * FROM users WHERE (id=1 AND email IS NULL) OR name='John'jsql // This will use the last WHERE statement// UPDATE users SET name='Jane' WHERE (id=1 AND email IS NULL) OR name='John'jsql
If you want to use the NOT
statement, pass a nested object with not
as it's key.
// SELECT * FROM users WHERE NOT id=1jsql
Currently, when passing objects, this function only uses the =
operator. If you need to use other operators, you will need to use the API below.
.w([condition[, values]])
Pass the WHERE
condition as a string. If you want to escape the values using the ?
replacement, then pass the values as an array.
// SELECT * FROM products WHERE price>=99.99 AND name LIKE 'a%'jsql
.o([orderBy])
Pass the ORDER BY
statement as an object with the keys being the column names and the value being asc
or desc
. Pass nothing to clear the previous order by statement.
// SELECT * FROM users ORDER BY first ASC, last DESCjsql
.l([limit[, offset]])
Pass the LIMIT
and OFFSET
statements as ints. Pass nothing to clear the previous statement.
// Only passing a limit// SELECT * FROM users LIMIT 10jsql // Passing an offset too// SELECT * FROM users LIMIT 10 OFFSET 5jsql
.run([query[, values[, callback]]])
You can manually pass a query as a string. If you want to use ?
to escape values in the query, just pass an array of values. Pass a callback function to be able to access the return values. More documentation of the callback function can be found here.
jsql
promise .run()
The .run
function also returns a promise if you prefer to use async await
or .then
and .catch
async { try const results = await jsql console catch err throw err } // or jsql
.clear()
Clears all statements (including the table). If this isn't called, all of your previous conditions are used again.
// SELECT * FROM users WHERE id=1 LIMIT 1jsql // Where and limit statements are saved // SELECT * FROM users WHERE id=1 LIMIT 1jsql // Clears everythingjsqlclear // SELECT * FROM usersjsql
.getQuery()
Returns the current SQL query as a string.
jsql console// 'select * from users where id=?'
.getValues()
Returns the current values as an array.
jsql console// [1, 'John']
.each(variable, function)
Similar to jQuery's each function, you can pass either an array as the first argument, or you can pass an object. The function you pass should have 2 arguments. The first is the index or key. The second is the value. This will make it easier to cycle through returned rows after you run a query.
// SELECT * FROM usersjsql