Node.js library for mapping mongo-style query objects to SQL queries.
Перейти к файлу
Dimitar c9f3e480f5 add support for count 2016-11-21 12:12:56 +02:00
docs Fix typo at docs/README.md 2016-07-31 14:47:05 +03:00
lib add support for count 2016-11-21 12:12:56 +02:00
tests add distinctOn for postgres 2016-07-08 18:06:17 +03:00
.gitignore documentation for templates, a lot of examples some fixes 2015-08-17 14:14:15 +03:00
.jshintrc jslintrc file for tests was added 2015-06-08 14:37:14 +03:00
.npmignore documentation for templates, a lot of examples some fixes 2015-08-17 14:14:15 +03:00
LICENSE documentation for templates, a lot of examples some fixes 2015-08-17 14:14:15 +03:00
README.md update readme 2016-02-07 10:27:41 +03:00
gulpfile.js add specific `template:block` block names format, add postgresql explain query type 2015-10-27 17:32:24 +03:00
package.json release 0.3.8 2016-07-08 18:11:55 +03:00

README.md

JSON-SQL

Node.js library for mapping mongo-style query objects to SQL queries.

Quick Start

Install it with NPM or add it to your package.json:

$ npm install json-sql

Then:

var jsonSql = require('json-sql')();

var sql = jsonSql.build({
	type: 'select',
	table: 'users',
	fields: ['name', 'age'],
	condition: {name: 'Max', id: 6}
});

sql.query
// sql string:
// select name, age from users where name = $p1 && id = 6;

sql.values
// hash of values:
// { p1: 'Max' }

Documentation

Documentation is available at the ./docs directory.

Examples

Select with join:

var sql = jsonSql.build({
	type: 'select',
	table: 'users',
	join: {
		documents: {
			on: {'user.id': 'documents.userId'}
		}
	}
});

sql.query
// select * from users join documents on user.id = documents.userId;

sql.values
// {}

Insert:

var sql = jsonSql.build({
	type: 'insert',
	table: 'users',
	values: {
		name: 'John',
		lastname: 'Snow',
		age: 24,
		gender: 'male'
	}
});

sql.query
// insert into users (name, lastname, age, gender) values ($p1, $p2, 24, $p3);

sql.values
// { p1: 'John', p2: 'Snow', p3: 'male' }

Update:

var sql = jsonSql.build({
	type: 'update',
	table: 'users',
	condition: {
		id: 5
	},
	modifier: {
		role: 'admin'
		age: 33
	}
});

sql.query
// update users set role = $p1, age = 33 where id = 5;

sql.values
// { p1: 'admin' }

Remove:

var sql = jsonSql.build({
	type: 'remove',
	table: 'users',
	condition: {
		id: 5
	}
});

sql.query
// delete from users where id = 5;

sql.values
// {}

For more examples, take a look at the ./docs directory or ./tests directory.

Tests

Clone repository from github, cd into cloned dir and install dev dependencies:

$ npm install

Then run tests with command:

$ gulp test

Or run tests coverage with command:

$ gulp coverage

License

MIT