Package Exports
- mongo-sql
- mongo-sql/lib/utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongo-sql) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
MoSQL - JSON to SQL
Put value and semantic meaning back into your queries by writing your SQL as JSON:
var builder = require('mongo-sql');
var usersQuery = {
type: 'select'
, table: 'users'
, where: { $or: { id: 5, name: 'Bob' } }
};
var result = builder.sql(usersQuery);
result.values // Array of values
result.toString() // Sql string valueResult:
select "users".* from "users" where "users.id" = $1 or "users"."name" = $2Want to play around with the syntax? Check out the playground, documentation, and examples.
Installation:
Node.js:
npm install mongo-sqlRequire.js:
jam install mongo-sqlWhy JSON?
There are plenty of SQL building libraries that use a very imperative style of building SQL queries. The approach is linear and typically requires a bunch of function chaining. It removes your ability to use the query as a value and requires the library consumer to build their queries in large clumps or all at once. It's sometimes impossible with some of these libraries to reflect on the current state of the query programmatically. What columns have I added? Have I already joined against my groups table? MoSQL uses standard data structures to accomplish its query building, so you can figure out the state of the query at all times.
JSON is also a prime candidate for becoming a universally understand data representation. By using Javascript objects, we do not rule out the possibility of interoping with and porting to other languages.
It may not be as pretty as other libraries, but prettiness is not a design principle of this library. The design principles are:
Extensibility
If a feature is not supported, you should be able to add your own functionality to make it supported.
Semantic Value
The query should be represented in a manner that makes sense to developer and machine. The use of standard data structures allows the developer to use standard APIs to manipulate the query.
Examples
Sorry, these are no particular order.
- Simple select
- Simple insert
- Insert with values from a select
- Simple select with conditions
- Joins
- Various conditional stuff
- Not in sub-query
- Create view
- Multi-row inserts
- Ridiculous 'with' query with selecting JSON literal
- Various column selection methods
- Two different ways to specify a function
- Rename column
- Alias a table in select
- Drop table
- Create table
- Select distinc
- Update with increment
- Group by
- Various table specification methods
- Insert with sub-query as second value
- With sub-queries
- Adding a constraint with alter table
- Registering a conditional helper
- Updates increment decrement
- Expand foreign key array of integers to an array of JSON results
Contributing
I will happily accept pull requests. Just please write a test for whatever functionality you're providing. Coding style is an evolving thing here. I'll be JSHinting this repo soon and will make the coding style consistent when I do.