Warehouse.js
Warehouse.js is a data storage layer for node.js and browser providing unified API for many supported storage engines (MongoDB, MySQL, SQLite, in-memory and others). You can create REST server or client with only few lines of code.
- Supports multiple backends (MongoDB, MySQL, SQLite, in-memory and others)
- Create REST server using few lines of code (compatible with Backbone.js)
- Unified API for CRUD operations
- Powerful query language RQL
- MIT License
Make a quick protoype of your application without using an external database. Then switch to a robust storage (like MongoDB or MySQL) without the need to change your code.
Install
npm install warehousejs
REST server with express in node.js
var express = warehouse = MongoBackend = ; var app = express store = ; warehouse; app;
The example server above can be used with Backbone.js.
// client var Model = BackboneModel; var items = url: '/item' model: Model; items; var john = items; john; john;
CRUD
The API is similar to IndexedDB API ( add, get, put, delete ). It is based on asynchronous promises (using Q library).
var jack = id: 15 firstname: 'Jack' lastname: 'Hammer' age: 35; // Createstore ; // Readstore ; // outputs: Jack // Updatejackage = 40;store; // Deletestore; // Delete allstoreclear;
Querying
Queries are implemented using RQL.
// get items with id=15store ; // result is an Array // get items with age >= 21store; // get items with price < 100, sort by acending price and descending ratingstore;
API
Backend
Backend represents DB that has collections for storing items. Common methods are:
- objectStoreNames () - list collections or tables
- objectStore(String name, [Object options]) - gets object store with specified name. If it does not exists then it is created.
- createObjectStore (String name, [Object options]) - creates object store with specified name
- deleteObjectStore (String name) - deletes object store with specified name (equivalent for droping table in SQL)
- open () - opens database connection. You don't have to worry about it most of the times, because connection will be automatically opened first time when it is needed (lazy loading).
- close () - closes database connection
- isClosed () - is the connection closed?
Options for stores are:
- keyPath - attribute that represents primary key (default 'id').
Store
Store represents collection of items (e.g. collection for MongoDB or table for SQL). Common methods are:
- put (value, [key | directives])
- add (value, [key | directives])
- delete (key | directives)
- get (key | directives)
- clear ()
- query (String query)
Key is a built-in type - String, Number, ... Directives is a object e.g. {key: '123'}
Common attributes:
- String name
- String keyPath
Backends
- MongoDB (server)
- SQL (server)
- Filesystem (server)
- Elastic Search (server and browser)
- NeDB (server and browser)
- Memory (server and browser)
- REST (server and browser)
- Local Storage (browser)
MongoDB
Backend for MongoDB using node-mongodb-native.
var options = // default connection options host: 'localhost' port: 27017 database: 'default' // optional authentication credentials user: 'user' password: 'pass'; var MongoBackend = backend = options store = backend;
You can use db and collection methods to get native driver objects.
store; store;
SQL
Backend for SQL databases, supports MySQL and SQLite. The underlying library node-persist also supports PostgreSQL and Oracle. However, those databases were not tested.
// options when using MySQLvar options = driver: 'mysql' // default connection options host: 'localhost' port: 3306 name: 'default' // database name // optional authentication credentials user: 'user' password: 'pass'; // options when using SQLitevar options = driver: 'sqlite3' filename: '/path/to/file.db'; var SqlBackend = backend = options store = backend;
If you need to execute advanced queries, runSql and runSqlAll methods can be used.
// runSql(sql, values) - runs a sql statement that does not return results (INSERT, UPDATE, etc).store ; // runSqlAll(sql, values) - runs a sql statement that returns results (ie SELECT).store ;
Filesystem
Backend which stores items as files on disk. This backend works only on server. Please note that querying speed will be slow for large amounts of data because no index is utilized.
It is useful for providing syncing capabilities in combination with Dropbox.
Pass path option to specify directory where the files will be stored. The path must exists.
var FsBackend = backend = path: '/path/to/storage' store = backend;
ElasticSearch
Backend using ElasticSearch.
This backend works under both server and browser.
Pass url option to specify remote server address.
var ElasticSearchBackend = backend = url: 'http://example.com/index' store = backend;
NeDB
Backend for NeDB.
var NeBackend = backend = store = backend;
Be default the datastore is in-memory only. You can specify filename
option for the persistent datastore.
store = backend;
Popsat id?
Memory
Store items in memory using native Arrays and Objects. It is useful for making quick prototypes without the need of external dependencies.
This backend works under both server and browser.
Store takes optional argument json, which loads initial data into the datastore. Argument can be one of
- Array
- Object
- Array JSON-encoded in String
- Object JSON-encoded in String
- String url of an remote resource, starts with 'http'
You can alternatively use fromJson and getJSON
var MemoryBackend = backend = store data; // Arraydata = id: 1 name: 'John' id: 2 name: 'Sarah'; // Array in JSONdata = '[{"id":1,"name":"John"},{"id":2,"name":"Sarah"}]'; // Objectdata = 1: id: 1 name: 'John' 2: id: 2 name: 'Sarah'; // Object in JSONdata = "1":"id":1"name":"John""2":"id":2"name":"Sarah"; // create the store with initial datastore = backend; // create the store and set data laterstore = backend;store;
REST
This backend is using remote REST server for storage. It is useful to access remote services.
If you combine it with a server (acting as a transparent proxy), you can basically use all implemented backends as if they were available for client.
This backend works under both server and browser.
Pass url option to specify remote server address (default is '/').
var RestBackend = backend = url: 'http://example.com' store = backend;
Local Storage
Implements storage using W3C Web Storage (also known as DOM Storage or Local Storage).
Names of Object Stores have a special meaning:
- session is a storage that is available for the duration of the page session
- local is persistent (same-origin rules are applied}
This backend works only in browser.
var LocalBackend = backend = ; // session onlyvar sessionStore = backend; // persistentvar localStore = backend;