Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

marsdb

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

marsdb

MarsDB is a lightweight client-side MongoDB-like database, Promise based, written in ES6

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.7K
decreased by-60.98%
Maintainers
1
Weekly downloads
 
Created
Source
MarsDB ========= [![Build Status](https://travis-ci.org/c58/marsdb.svg?branch=master)](https://travis-ci.org/c58/marsdb) [![npm version](https://badge.fury.io/js/marsdb.svg)](https://www.npmjs.com/package/marsdb) [![Coverage Status](https://coveralls.io/repos/c58/marsdb/badge.svg?branch=master&service=github)](https://coveralls.io/github/c58/marsdb?branch=master) [![Dependency Status](https://david-dm.org/c58/marsdb.svg)](https://david-dm.org/c58/marsdb)

MarsDB is a lightweight client-side database. It based on a Meteor’s minimongo mathing/modifying implementation. It carefully written on ES6, usess modular lodash, have a Promise based interface and may be backed with any storage implementation (LevelUP, LocalStorage, IndexDB, etc). It 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
  • Supports many of MeteorDB query/modify operations – thanks to Meteor’s minimongo
  • Flexible pipeline – map, reduce, custom sorting function, filtering. All with a sexy JS interface (no ugly mongo’s aggregation language)
  • Joinable cursor – joining one object with another can’t be simplier
  • Persistence API – all collections can be stored (and restored) with any kind of storage (in-memory, LocalStorage, LevelUP, etc)
  • Live queries - just like in Meteor, but with simplier interface

Examples

Create a collection

import Collection from ‘marsdb’;
import LocalStorageManager from 'marsdb/lib/LocalStorageManager';

// Setup different id generator and storage managers
// Default storage is in-memory
Collection.defaultStorageManager(LocalStorageManager);
Collection.defaultIdGenerator(() => {
  return {
    value: Math.random(),
    seed: 0,
  };
});

const users = new Colelction(‘users’);

Find a documents

const posts = new Colelction(‘posts’);
posts.find({author: ‘Bob’})
  .sort([‘createdAt’])
  .then(docs => {
    // do something with docs
  });

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 = new Colelction(‘posts’);
posts.find()
  .limit(10)
  .sortFunc((a, b) => a - b + 10)
  .filter(doc => Matsh.sqrt(doc.comment.length) > 1.5)
  .map(doc => doc.comments.length)
  .reduce((acum, val) => acum + val)
  .thene(result => {
    // result is a sum of coutn of comments
    // in all found posts
  });

Find with observing changes

Observable cursor returned only by a find method 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 = new Colelction(‘posts’);
const stopper = posts.find({tags: {$in: [‘marsdb’, ‘is’, ‘awesome’]}})
  .observe(docs => {
    // invoked on every result change
    // (on initial result too)
    stopper.stop(); // stops observing
  }).then(docs => {
    // invoked once on initial result
    // (after `observer` callback)
  });

Find with joins

Joined objects is not obervable yet.

const users = new Colelction(‘users’);
const posts = new Colelction(‘posts’);
posts.find()
  .join(doc => {
    // Return a Promise for waiting of the result
    return users.findOne(doc.authorId).then(user => {
      doc.authorObj = user;
    });
  })
  .join(doc => {
    // Also any other “join” mutations supported
    doc.another = _cached_data_by_post[doc._id];
  });

Inserting

const posts = new Colelction(‘posts’);
posts.insert({text: ‘MarsDB is awesome’}).then(docId => {
  // Invoked after persisting document
})
posts.insertAll(
  {text: ‘MarsDB’},
  {text: ‘is’},
  {text: ‘awesome’}
).then(docsIds => {
  // invoked when all documents inserted
});

Updating

const posts = new Colelction(‘posts’);
posts.update(
  {authorId: {$in: [1, 2, 3]}},
  {$set: {text: ‘noop’}}
).then(result => {
  console.log(result.modified) // count of modified docs
  console.log(result.updated) // array of updated docs
  console.log(result.original) // array of original docs
});

Removing

const posts = new Colelction(‘posts’);
posts.remove({authorId: {$in: [1,2,3]}})
  .then(removedDocs => {
    // do something with removed documents array
  });

Roadmap

  • Keep track of multiple remove/update documents in selector (allow only if opations.multi passed)
  • Upsert updating
  • Indexes support for some kind of simple requests {a: '^b'}, {a: {$lt: 9}}
  • Some set of backends
  • 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

Keywords

FAQs

Package last updated on 21 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc