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
This repo was no longer maintained, because of a new Version of json-sql-builder. Have a look at json-sql-builder2
npm install json-sql-builder --saveconstSQLBuilder=require('json-sql-builder');// create a new instance of the SQLBuilder and load the language extension for mysqlvarsqlbuilder=newSQLBuilder('mysql');// lets start some query funvartotalSalary=sqlbuilder.build({$select: {$columns: ['job_title',{total_salary: {$sum: 'salary'}}],$from: 'people',$where: {job_title: {$in: ['Sales Manager','Account Manager']},age: {$gte: 18},country_code: 'US',},$groupBy: ['job_title'],}});Result
// totalSalary.sqlSELECT`job_title`,SUM(`salary`)AS`total_salary`FROM`people`WHERE`job_title`IN(?, ?)AND`age`>= ?
AND`country_code`= ?
GROUPBY`job_title`// totalSalary.values['Sales Manager','Account Manager',18,'US']// general outputqueryOutput={sql: 'Your SQL-query-string'values: ['Array','with','all','Query-values']timeout: 10000// depends on the options}- Join Support for MySQL and PostgreSQL should work now
- Fix Support for Sub-Select's with AS clause
- 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.
- ANSI using
$create: { $view: 'myView', $select: {...} }
varquery=sqlbuilder.build({$create: {$view: {$cor: 'v_people'},$select : {$from: 'people',$columns: ['first_name','last_name']}}});// OUTPUTCREATEORREPLACEVIEW`v_people`ASSELECT`first_name`,`last_name`FROM`people`;- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
varquery=sqlbuilder.build({$select: {$from: 'public.users',$joins: {'public.users_profiles': {$as: 'profile',$innerJoin: {'public.users.id': {$eq: {$column: 'profile.user_id'}}}},'public.users_likes': {$as: 'likes',$leftJoin: {$and: [{'likes.user_id': {$eq: {$column: 'public.users.id'}}},{'likes.score': {$gt: 1}}]}}}}});- $rowToJson
- $jsonBuildObject
// Example using $jsonBuildObjectvarquery=sqlbuilder.build({$select: {$columns: [{peopleData: {$jsonBuildObject: {firstName: 'John',lastName: 'Doe'}}}]}});SELECTjson_build_object('firstName',$1,'lastName',$2)AS"peopleData"FROM"people"// Example using $rowToJsonvarquery=sqlbuilder.build({$select: {$from: 'people',$columns: [{peopleData: {$rowToJson: 'people'}}]}});SELECTrow_to_json("people")AS"peopleData"FROM"people";- ANSI using
$create: { $index: 'myidx', $table: 'mytable', $columns: {...} } - Move
$ineto Basic Helpers and support Boolean and String expressions - Update tests and docs
varquery=sqlbuilder.build({$create: {$index: 'idx_people_last_name',$table: 'people',$columns: {last_name: {$asc: true},first_name: {$asc: true},},$using: 'BTREE'}}// OUTPUTCREATEINDEX`idx_people_last_name`ON`people`USINGBTREE(`last_name`ASC,`first_name`ASC);- ANSI
- PostgreSQL
- MySQL
- Update tests and docs
varquery=sqlbuilder.build({$create: {$table: 'users',$define: {_id: {$column: {$type: 'VARCHAR',$length: 32,$notNull: true}},username: {$column: {$type: 'TEXT'}},first_name: {$column: {$type: 'TEXT'}},last_name: {$column: {$type: 'TEXT',$default: 'John'}},createdAt: {$column: {$type: 'DATETIME',$notNull: true}},pk_users: {$constraint: {$primary: true,$columns: '_id'}},uc_users_username: {$constraint: {$unique: true,$columns: 'username'}}}}});// OUTPUTCREATETABLE`users`(`_id`VARCHAR(32)NOTNULL,`username`TEXT,`first_name`TEXT,`last_name`TEXTDEFAULT ?,`createdAt`DATETIMENOTNULL,CONSTRAINT`pk_users`PRIMARYKEY(`_id`),CONSTRAINT`uc_users_username`UNIQUE(`username`));LIMITandLIMIT ALLusing$limitOFFSETusing$offset- add
sqlDialectproperty to sqlBuilder to use it inside of helper-functions
ON CONFLICTclause using$confict- Update documetation
- Function
json_agg()using$jsonAgg - Function
to_json()using$json