
MarsDB
MarsDB is a lightweight client-side database. It's based on a Meteor's minimongo matching/modifying implementation. It's carefully written on ES6, have a Promise based interface and may be backed with any storage implementation (see plugins). It's also supports observable cursors.
MarsDB supports any kind of find/update/remove operations that Meteor's minimongo does. So, go to the Meteor docs for supported query/modifier operations.
You can use it in any JS environment (Browser, Electron, NW.js, Node.js).
Features
- Promise based API
- Carefully written on ES6
- Very very flexible – just take a look to the plugins section
- Supports many of MongoDB query/modify operations – thanks to a Meteor's minimongo
- Flexible pipeline – map, reduce, custom sorting function, filtering. All with a sexy JS interface (no ugly mongo's aggregation language)
- Persistence API – all collections can be stored (and restored) with any kind of storage (in-memory, LocalStorage, LevelUP, etc)
- Observable queries - live queries just like in Meteor, but with simplier interface
- Reactive joins – out of the box
Bindings
Plugins
- In-memory storage (built-in default)
- LocalForage storage – fastest in-browser storage (InexedDB, WebSQL and fallback to localStorage)
- LocalStorage storage – not recommended, better prefer LocalForage
- LevelUP storage – lightweight server-less Node.js storage
- MongoDB wrapper – use MarsDB for comfortable work with MongoDB
- Validation via Mongoose – validate objects with Mongoose
Meteor compatible client/server
Sometimes you can't use Meteor infrastructure. Maybe you need to build a custom client. Maybe you need to build a custom server with express and other modules. In meteor it can be done with a ton of hack. But the only reason why it's so ugly to do a simple things is because Meteor forces you to use their infrastructure. I'm trying to solve this issue with DDP client/server modules, based on MarsDB.
Examples
Using within non-ES6 environment
The ./dist
folder contains already compiled to a ES5 code, but some polyfills needed. For using in a browser you must to include marsdb.polyfills.js
before marsdb.min.js
. In node.js you need to require('marsdb/polyfills')
.
It sets in a window/global: Promise, Set and Symbol.
Create a collection
;; // Default storage is in-memory// Setup different storage managers// (all documents will be save in a browser cache)Collection; // Create collection wit new default storageconst users = 'users';
Create an in-memory collection
;; // Set some defaults and create collectionCollection;const users = 'users'; // But it may be useful to create in-memory// collection without defined defaults// (for example to save some session state)const session = 'session' inMemory: true;
Find documents
const posts = 'posts';posts ;
Find with pipeline (map, reduce, filter)
An order of pipeline methods invokation is important. Next pipeline operation gives as argument a result of a previous operation.
const posts = 'posts'; // Get number of all comments in the DBposts ; // Result is `undefined` because posts// is not exists and additional processing// is not ran (thanks to `.ifNotEmpty()`)posts
Find with observing changes
Observable cursor returned by a find
and findOne
methods of a collection. Updates of the cursor is batched and debounced (default batch size is 20
and debounce time is 1000 / 15
ms). You can change the paramters by batchSize
and debounce
methods of an observable cursor (methods is chained).
const posts = 'posts';const stopper = posts ;
Find with joins
const users = 'users';const posts = 'posts';posts // Or just pass join spec object for fast joining // (only one `find` will be produced for all posts) // posts[i].authorId will be user object
Inserting
const posts = 'posts';postsposts;
Updating
const posts = 'posts';posts; // Upsert (insert when nothing found)posts;
Removing
const posts = 'posts';posts ;
Roadmap
- Indexes support for some kind of simple requests {a: '^b'}, {a: {$lt: 9}}
- Documentation
Contributing
I'm waiting for your pull requests and issues.
Don't forget to execute gulp lint
before requesting. Accepted only requests without errors.
License
See License