calypso
- Use a SQL-like query language over any queryable data store.
- Automatically map query results to models.
- Protect against SQL injection with prepared statements.
- Create your own driver for your favorite database or web service!
Install
npm install calypso
You'll also need a driver. Available drivers:
- MongoDB: https://github.com/kevinswiber/calypso-mongodb
- Usergrid: https://github.com/kevinswiber/calypso-usergrid
- LevelDB/LevelUP: https://github.com/kevinswiber/calypso-level
- Memory: https://github.com/kevinswiber/calypso-memory
- Postgres: https://github.com/mdobson/calypso-postgres
Usage
var calypso = ;var Query = calypsoQuery;var UsergridDriver = ; var engine = calypso; engine; // Output:// [ { title: 'A Brief History of Time', writer: 'Stephen Hawking' } ]
Query.of
Takes a collection name or a constructor function that has been mapped. (See: Mappings)
Returns a Query instance.
Query#ql
Accepts Calypso Query Language as a string.
var query = Query ;
Query#params
Add parameters to prepared statements. Parameters will be properly escaped to prevent SQL injection attacks.
var query = Query ;
Mappings
Constructor functions can be assigned mappings. Calypso will automatically instantiate the object when receiving query results. Here's a quick Getting Started guide.
1. Require dependencies.
var calypso = ;var Query = calypsoQuery;var UsergridDriver = ;
2. Set up a constructor function that assigns properties to instances.
var { thistitle = null; thiswriter = null;};
3. Set up a mapping for the constructor.
var { config };
Notice we're mapping Book#writer to the data store's author property.
4. Configure a new Calypso engine.
var engine = calypso;
5. Query based on the JavaScript properties.
engine;
Output: [ { title: 'A Brief History of Time', writer: 'Stephen Hawking' } ]
Sessions
Sessions are created by drivers. See the Usergrid Driver for more information: https://github.com/kevinswiber/calypso-usergrid
Session#find(query, callback)
This method returns an array of results. It takes a query object and a callback in the form of function(err, results)
.
Session#get(query, id, callback)
The query passed to this method is without fields or filters.
Example:
session;
Repositories
A repository can be used instead of sessions. They provide similar functionality to sessions, except they are tied to a particular constructor.
Example:
var calypso = ;var Query = calypsoQuery;var RepositoryFactory = calypsoRepositoryFactory;var UsergridDriver = ; var { thistitle = null; thiswriter = null;}; var { config }; var engine = calypso; engine;
Output: [ { title: 'A Brief History of Time', writer: 'Stephen Hawking' } ]
Calypso Query Language
The Calypso Query Language (CaQL) has support for field selection, field aliases, filters, and ordering.
See the full specification here: Calypso Query Language Specification
License
MIT