mongo-sql

A mongo-like interface for sql generation, postgres-style

npm install mongo-sql
5 downloads in the last day
21 downloads in the last week
156 downloads in the last month

MoSQL - JSON to SQL

Put value and semantic meaning back into your queries by writing your SQL as JSON:

NPM

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 value

Result:

select "users".* from "users" where "users.id" = $1 or "users"."name" = $2

Want to play around with the syntax? Check out the playground, documentation, and examples.

Installation:

Node.js:

npm install mongo-sql

Require.js:

jam install mongo-sql

Why 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.

The reason we use standard JavaScript data structures is so everything is easily manipulated. Arrays are arrays and objects are objects. Everyone knows how to interface with them.

JSON is also a prime candidate for becoming a universally understood 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

{
  type: 'create-table'
, table: 'jobs'
, definition: {
    id:         { type: 'serial', primaryKey: true }
  , user_id:    { type: 'int', references: { table: 'users', column: 'id' } }
  , name:       { type: 'text' }
  , createdAt:  { type: 'timestamp', default: 'now()' }
  }
}

Sorry, these are in no particular order.

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

How does it work?

Every MoSQL query has a query type specified that maps to a SQL string template. Query types are composed of various strings and query helpers whose output maps to functions.

So type: 'select' uses the query type defined as 'select'. Every other property in the query object maps to a query helper. The 'select' query type starts off like this:

{with} select {columns} {table}...

When you have the following query:

{ type: 'select', table: 'users' }

The table property is mapped to the table query helper.

98% of the functionality in MoSQL is defined through various helper interfaces. If the functionality you need doesn't exist, you can easily register your own behavior and keep on moving along. To see how all of the functionality was implemented, just check out the helpers folder. It uses the same API as library consumers to add its functionality.

Contributing

I will happily accept pull requests. Just 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.

Upgrading from 2.4.x to 2.5.x

There are two things you need to look out for:

Do not rely on adding parenthesis to strings (like in columns or returning helpers) in order to prevent MoSQL from attempting to quote the input. Instead use the expression query type:

// select something_custom - another_custom as "custom_result" from "users"
{
  type: 'select'
, table: 'users'
, columns: [
    { expression: 'something_custom - another_custom', alias: 'custom_result' }
  ]
}

If you were relying on expression objects without a type specified to be converted into a function type, this will no longer happen. Queries without types with expression specified in them will get converted to the new expression type.

npm loves you