json-sql-builder2
Levelup your Queries with json-sql-builder2
.
Table of Content
- Why to use json-sql-builder2
- Supported SQL-Dialects
- Documentation
- Getting Started
- Writing new Helpers and Operators
- Tests
- Generating docs
- License
Why to use json-sql-builder2
You are working with javascript and got the power of json, so why do you concat string by string or worry about query parameters. When you need to write dynamic queries defined by the user it is also much easier to use JSON instead of generating a string-based query.
Another point is that in most cases the readability and structuring the query is much better than using strings.
Working with the JSON DataType is also much easier (see JSON-Example below).
Supported SQL-Dialects
By default json-sql-builder2
supports the follwing languages.
- MySQL
- MariaDB
- PostgreSQL
- SQLite
- Oracle
- Microsoft SQL Server
Documentation
Each Operator and Helper is well documented. And you've got a lot of examples for each.
For further details on each Helper or Operators have a look at the complete documentation at https://github.com/planetarydev/json-sql-builder2/tree/master/sql and search or browse through the directories.
Getting Started
Install
npm install json-sql-builder2 --save
First Example
const SQLBuilder = ;// create a new instance of the SQLBuilder and load the language extension for mysqlvar sql = 'MySQL'; // lets start some query funvar totalSalary = sql;
Result
// totalSalary.sqlSELECT `job_title` AS `total_salary`FROM `people`WHERE `job_title`
Setup SQLBuilder
By default you will create a new Instance of SQLBuilder by passing the language-dialect as String you would like to work with.
const SQLBuilder = ;// Syntax:// SQLBuilder(<dialect>[, options]);//// dialect: String | Function// options: Object // Setup a new instance for MySQLvar sql = 'MySQL'; // Setup a new instance for MariaDBvar sql = 'MariaDB'; // Setup a new instance for PostgreSQLvar sql = 'PostgreSQL'; // Setup a new instance for SQLitevar sql = 'SQLite'; // Setup a new instance for Oraclevar sql = 'Oracle'; // Setup a new instance for SQLServervar sql = 'SQLServer'; // Passing a function as dialect param to setup your individual needsvar sql = { // set the name to one of the supported language-dialects sql; // set the left and right handed quoting character sql; // if there the character is the same for left and right sql; // setup the placholder for each value pushed to the value-stack sql { return '@param' + sql_valueslength; } // if neccessary turn the array of values to a object sql { let resultAsObj = {} sql; return resultAsObj; }};
Options
quoteIdentifiers
true | false (Default=false)
If this option is true
each identifier will be quoted. If this option is set to false
only invalid, unsafe identifiers will be quoted.
attachGlobal
true | false (Default=false)
This Option will attach each Operator like $select
in uppercase SELECT
, each callee like left
in lowercase $left
and each Keyword in uppercase letters to the globals
Object, so you could write your code like:
var sql = 'SQLServer' attachGlobal: true; let myQuery = ; // Results// myQuery.sqlSELECT AS people_nameFROM peopleWHERE job_title AND last_name = @param5 // myQuery.values param1: 1 param2: '. ' param3: 'Sales Manager' param4: 'Account Manager' param5: 'Doe'
useOuterKeywordOnJoin
true | false
Have a look at the source of /sql/helpers/queries/join/.joinhelper.js
Support different Data Types
Each Operator and Helper can be used with different Data Types, so it is easy to take the Type that fits your needs. Have a look at the README.md for the Helpers, Operator you like to use. Each of them are well documented and the file is located directly beside the source-code.
myQuery = sql; // is the same as:myQuery = sql; // is the same as:myQuery = sql; // SQL outputINSERT INTO VALUES $1 $2 $3 // Values "$1": "John" "$2": "Doe" "$3": 40
Using Keywords
A complete List of all Keywords you will find at ./sql/keywords/.
💡 Exmple using Keyword DEFAULT
myQuery = sql; // SQL outputINSERT INTO VALUES $1 $2 DEFAULT // Values "$1": "John" "$2": "Doe"
More Examples
Working with SQL-Functions
myQuery = sql; // instead using $concat helper as js-function is also possiblemyQuery = sql; // SQL outputSELECT AS people_nameFROM people // Values "$1": " "
💡 PostgreSQL update jsonb column
myQuery = sql; // SQL outputUPDATE "people"SET "data" = WHERE "people_id" = $5 // Values "$1": "{profile,firstName}" "$2": "\"John\"" "$3": "{profile,lastName}" "$4": "\"Doe\"" "$5": 456
Writing new Helpers and Operators
What are the differences between Operators and Helpers?
Any Operator can build a valid SQL result by it's own without additional stuff.
So each operator will be directly attached as $<operator-name>
to the SQLBuilder instance.
That means you can directly call each Operator like:
// standard build-calllet myQuery = sql; // '$select' is an Operator and so you can use this directlylet myQuery = sql; // instead $from is a Helper// and this could'nt be directly called like sql.$from({...})
Using a Template
A Template for writing new Helpers and Operators:
Each Operator should be located in /sql/operators/<operator-name/
.
Use the following template to create a new Operator.
class <operator-name> extends SQLBuilderSQLOperator | SQLHelper { supersql; // definition of allowed types and it's SQL Syntax // more details on writing your Syntax (see 'Define a new Syntax' below) this; // optional declaration of Keywords this; // optional registration of private Helpers that are used inside the // Syntax declared above with this.Types({...}) this; // the helper must be located in "./private/<helper-name>/<helper-name>.js" // or register your private helper by your own this$<helper-name> = new <helperClass>sql; } // optional function callee definition if the standard // callee - generated by Syntax - doesn't fit { // ... return sqlResultString; } // optional linker-method { // ... } // optional preBuild-method { // ... return query; } // optional postBuild-method { // ... return result; } moduleexports = definition: <class-definition> description: <String: a short description of the or Operator> supportedBy: // list all SQL-dialects that support this new Helper or Operator // SQL-dialect can be: MySQL, MariaDB, PostgreSQL, SQLite, Oracle, SQLServer <SQL-dialect>: <'' | url to official docs> <another SQL-dialect>: <'' | url to official docs> examples: // write at minimum one Test "Basic Usage" for each Type of Syntax defined above // using the same structure as used by this.Types({...}) in the class constructor. // Additional Tests and Examples allways wellcome! // Example of a Test-Case for the Type "Object": Object: { return // if the test is restricted to special SQL dialects supportedBy: // list all dialects (MySQL, MariaDB, PostgreSQL, SQLite, Oracle, SQLServer) // the Test is valid for <SQL-dialect>: true { return sql; } expectedResults: sql: 'SELECT * FROM people' values: {} // List any SQL-dialect if the Result on Test is different to the generell Result <SQL-dialect>: sql: <Result-String if there are different using a special SQL-dialect> values: $1: ... $2: ... } { // ... }
Example writing LEFT-Function Helper
If there is something missing you can easily extend all your required stuff.
If you will create a new Helper or Operator I would be glad if you will contribute and share your magic stuff.
The only thing you have to do is browse to the right place inside the /sql/
directory
and create a new folder and file named with the Helper or Operator.
In our Example we will create the LEFT()
SQL-Function.
This file will be located at /sql/helpers/functions/string/
:
- create a new Folder
left
- create a new File
left/left.js
Here is the code you need to write for left.js
using the Template:
'use strict'; // give the new Class the name of the Helper or OperatorSQLHelper { // always call the construtor of SQLHelper first supersql; // declare the possible types that can be used this; // add each Helper defined in the Syntax as private, predefined Helper this$str = sql; this$len = sql; } // export the new $left Helpermoduleexports = // exports the class-definition (this is used by SQLBuilder to setup the new Helper) definition: left // add some description for the auto-generation of the docs README.MD description: `Specifies the \`LEFT\` function.` // define the SQL-dialects that support this new Helper // Each dialect listed by supported by can use this Helper. If you pass a string // that starts with 'http://' or 'https://' inside the docs it automatically creates a link // to the official docs supportedBy: MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_left' MariaDB: 'https://mariadb.com/kb/en/library/left/' PostgreSQL: 'https://www.postgresql.org/docs/9.1/static/functions-string.html' // SQLite did not support the left() function at this time // Oracle did not support the left() function at this time --> instead we can use SUBSTR( string, start_position [, length ] ) defined as new SQLHelper SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/functions/left-transact-sql' // Always add at least "Basic Usage" as test and example for each type you // have declared by the new class using this.Types() examples: Number: // Basic Usage is the Name of the Test and must always declared as function(sql) // Parameter sql will be passed as instance of the SQLBuilder { // the function always returns an Object {test: function() {}, expectedResults: {} [, supportedBy: { PostgreSQL: true, MySQL: true, ... }] } return // write your test and return always the result { return sql; } // define the expected results of the test with // sql as String and values as Object. If there are no values expected, so define an empty Object like values: {} expectedResults: sql: 'SELECT LEFT(first_name, $1) AS first_name' values: $1: 1 } Object: // Basic Usage is the Name of the Test and must always declared as function(sql) // where sql is the instance of the SQLBuilder { // the function always returns an Object {test: function() {}, expectedResults: {} [, supportedBy: { PostgreSQL: true, MySQL: true, ... }] } return // write your test and return always the result { return sql; } // define the expected results of the test with // sql as String and values as Object. If there are no values generated, so define an empty Object values: {} expectedResults: sql: 'SELECT LEFT($1, $2) AS test' values: $1: 'Hello World' $2: 5 } // Optionally add some more Tests and Examples with a different name: { return { return sql; } expectedResults: sql: 'SELECT LEFT($1, $2) AS test' values: $1: 'Hello World' $2: 5 }
Understanding, Writing the Syntax
Every Helper or Operator will be defined by a Type, iterateable sub-Type and optionally value restrictions. For each of this situation you have to support a valid Syntax, that will generate the SQL-Result-String for the Helper or Operator.
Example of SUBSTR-Syntax for Type Object
SQLHelper { ... this
Okay, lets explain the magic stuff we have declared above.
By defining only the Type Object
you can only use the $substr
Helper like first_name: { $substr: {...} }
.
If you try to use it with a Number-Value like first_name: { $substr: 5 }
the SQLBuilder will throw an Error like Type Number is not allowed by Syntax
.
In our case we define two required Parameters (or better say Helpers). The first one is <$str>
and the second one is <$start>
.
The third [$len]
Helper is optional. With this informations we could write the following examples:
let myQuery = sql; // SQL-OutputSELECT AS test
Sub-Syntaxing
In most cases of SQL there are optional parts that also include keywords. In our Example of the SUBSTR Function it's the FOR
option
that specifies the length of the substring. So we need a Sub-Syntax that will only be generated when the Helper inside is supported.
The Sub-Syntax is defined by using curly braces like { FOR [$len]}
.
Native js Function support - using SQLBuiler.CALLEE
What the hell is that?
Sometimes it's quite usefull to use a Helper as native js-Function like test: sql.substr('Hello World', 3)
, so
you can specifiy one Syntax per Helper or Operator as callee by adding an optional parameter to the Syntax-Function SQLBuiler.CALLEE
.
This option will automatically turn the Syntax into a native js-function located on the current instance of the SQLBuilder.
In our case the Syntax of the Function will be: sql.substr(<str>, <start> [, options])
where options is an Object that could take all
optional Helpers defined by the Syntax.
Example
let myQuery = sql; // SQL-OutputSELECT AS test
Write an individual callee**
Sometimes the auto generated callee does not fit our rules, so you can easily write your own callee by adding a method named callee
to the new Helper class.
In this case you could define each Function-Parameter by your own and add code to build the SQL-Result.
Just a small Example how the substr-callee can look like.
Example
SQLHelper { ... this
Dialect-Specific SQL-Parts
About 70% or 80% of all SQL comply with the ANSI SQL-Standard. So you can write code for each Helper and Operator for each SQL-dialect or you can add your specific Helper-Syntax as dialect-specific expression.
Example
SELECT $top-->SQLServer DISTINCT$distinct SQL_CALC_FOUND_ROWS$calcFoundRows-->MySQLMariaDB <$columns> $into-->MySQLMariaDBSQLServer FROM $from $join WHERE $where GROUP BY $groupBy WITH ROLLUP$withRollup-->MariaDBMySQL HAVING $having ORDER BY $orderBy LIMIT $limit-->MariaDBMySQLPostgreSQLSQLite OFFSET $offset-->MariaDBMySQLPostgreSQLSQLite
The Example shows the current Syntax of the SELECT
Statement. Here you can see some
dialect-specific Helpers marked with -->(<sql-dialect>[,<sql-dialect>,...])
.
So, if you are running SQLBuilder with PostgreSQL the $info
Helper for the INTO
clause
is only supported by MySQL, MariaDB and SQLServer. If you write $select: { $from: 'people', $into: ... }
the SQLBuilder will throw an Error that $into Helper is not permitted by Syntax
.
Concating iterateable Informations
Sometimes you need to define an Array of columns or an Object with column-identifiers and they all need to concatenated by comma with the previouse one. To archive this you can specifiy a concatination-String or Joiner.
Let's build a part of the $columns
Helper from the $select
Operator.
let myQuery = sql // SQL-OutputSELECT first_name last_name ... // This is the definition of the $columns Helper.// Have a look at the joiner "[ , ... ]" inside the Syntax.SQLHelper { supersql; this; } // Joiner [ , ... ] explained:// Each joiner has this style: "[ <joiner-def>... ]"// ! note of the space chars >---^---------------^
Every time the Syntax includes such a declaration the SQLBuilder extracts the joiner-definition and concatenates each item with this string-definition.
Another Example of a joiner-Definition you will find at /sql/helpers/locical/and/and.js
which is a Helper that is used by the $where Helper.
The joiner is defined as [ AND ... ]
.
BuiltIn Parameters
Each Syntax can take - so called BuiltIn-Params - to interact with the JSON-Data. For this you have to following parameter definitions you could use inside each Syntax:
<key>
,<key-ident>
or<key-param>
<value>
,<value-ident>
or<value-param>
<identifier>
<key>
, <value>
:
Native replacement for the Objects Key (or Index when using an Array) and replacement for the value of an Item.
extends... ... this; ... let myQuery = sql //SQL-OutputSELECT my_key AS my_string_value
<key-ident>
, <value-ident>
:
Safely quoted replacement for the Objects Key (or Index when using an Array - does not really make sense :-) ) and replacement for the value of an Item.
extends... { ... this; ... } let myQuery = sql //SQL-Output for PostgreSQLSELECT "my_key" AS "my_string_value" "my_schema""my_table""my_col" AS "my_test_alias" //SQL-Output for MySQLSELECT `my_key` AS `my_string_value` `my_schema``my_table``my_col` AS `my_test_alias` //SQL-Output for SQLServerSELECT my_key AS my_string_value my_schemamy_tablemy_col AS my_test_alias
<key-param>
, <value-param>
Parameterized Key and Value replacement where the value itself is pushed to the value-stack.
extends... { ... this
More Examples writing new stuff
For more informations browse through the /sql/operator
or /sql/helper
directories. There are a lot
of Helpers and Operators and they all give you the best examples to write your own magic stuff.
Tests
After changing an existing Helper, Operator or maybe creating some new stuff you should run the Test. For this use always:
npm test
If you like to Test only a specific language dialect or specific helper or operator you could pass arguments for that:
npm test -- [ --language|dialect <MySQL|MariaDB|PostgreSQL|SQLite|Oracle|SQLServer> ] [ --helper|operator <path of helper located in ./sql> ] # Test only the $left-helper for language-dialect MySQL npm test -- --helper /helpers/functions/string/left/left.js --language MySQL
Generating docs
The documentation will automatically rebuild with every successful Test run. Please note that a successful Test will only be archived by running a complete Tests without a specific language, helper or operator.
License
MIT License
Copyright (c) 2017-2018 planetarydev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.