json-sql-builder
Writing your SQL-Queries in a way like mongo. Use JSON to define all the queries you like to run.
By default json-sql-builder
supports the ANSI-SQL language. In addition to this you can specify a dialect like mysql
or postgreSQL
.
At this time we will support additional language helpers and operators for:
- ANSI
- MySQL
- PostgreSQL
- Oracle
- Microsoft SQL Server
For further details on the language specific helpers and operators have a look at the complete documentation at https://planetarydev.github.io/json-sql-builder/.
Current Dev Stage
The developing of this module is currently still in work, for details have a look at the roadmap. If you like to support the current development feel free and contribute on github. Any pull requests are welcome if you supply:
- Tests
- Documentation
- Support backward compatibility
Install
npm install json-sql-builder --save
Getting Started
const SQLBuilder = ;// create a new instance of the SQLBuilder and load the language extension for mysqlvar sqlbuilder = 'mysql'; // lets start some query funvar totalSalary = sqlbuilder;
Result
// totalSalary.sqlSELECT `job_title` AS `total_salary`FROM `people`WHERE `job_title`
Release notes
1.0.19 Bugfixing
- Join Support for MySQL and PostgreSQL should work now
- Fix Support for Sub-Select's with AS clause
1.0.17+18 Bugfixing, Update docs
- Parameterized queries for PostgreSQL using $create operator. The params will now safely escaped by pg-format because PostgreSQL does not support parameters on CREATE statements.
CREATE VIEW
Support with new operators and helpers
1.0.16 Add - ANSI using
$create: { $view: 'myView', $select: {...} }
var query = sqlbuilder; // OUTPUTCREATE OR REPLACE VIEW `v_people` AS SELECT `first_name` `last_name` FROM `people`;
JOIN
operators
1.0.15 Add Support for ANSI - INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
var query = sqlbuilder;
1.0.15 Add PostgreSQL JSON helpers
- $rowToJson
- $jsonBuildObject
// Example using $jsonBuildObjectvar query = sqlbuilder; SELECT AS "peopleData"FROM "people" // Example using $rowToJsonvar query = sqlbuilder; SELECT AS "peopleData"FROM "people";
CREATE INDEX
operators and helpers for
1.0.14 Add - ANSI using
$create: { $index: 'myidx', $table: 'mytable', $columns: {...} }
- Move
$ine
to Basic Helpers and support Boolean and String expressions - Update tests and docs
var query = sqlbuilder
CREATE TABLE
operators and helpers for
1.0.13 Add - ANSI
- PostgreSQL
- MySQL
- Update tests and docs
var query = sqlbuilder; // OUTPUTCREATE TABLE `users` `_id` NOT NULL `username` TEXT `first_name` TEXT `last_name` TEXT DEFAULT ? `createdAt` DATETIME NOT NULL CONSTRAINT `pk_users` PRIMARY CONSTRAINT `uc_users_username` );
1.0.12 Add helpers and operators for postgreSQL
LIMIT
andLIMIT ALL
using$limit
OFFSET
using$offset
- add
sqlDialect
property to sqlBuilder to use it inside of helper-functions
1.0.11 Add helpers and operators for postgreSQL
ON CONFLICT
clause using$confict
- Update documetation
1.0.10 Add helpers and operators for postgreSQL
- Function
json_agg()
using$jsonAgg
- Function
to_json()
using$json