Giter VIP home page Giter VIP logo

mesh-loki's Introduction

Build Status Coverage Status Dependency Status

Streamable database adapter for LokiJS, an in-memory JavaScript database. This library also pairs nicely with crudlet, along with all the other crudlet plugins such as crudlet-pubnub, and crudlet-http.

var crud   = require("crudlet");
var lokidb = require("crudlet-loki");
var loki   = require("loki");
var _      = require("highland");

// setup the DB
var db = lokidb(new loki(__dirname + "/db.json"));
// db = lokidb(__dirname + "/db.json"); // also works

// setup the child collection
var peopleDb = crud.child(db, { collection: "people" });

// insert one, or many items
peopleDb("insert", {
  data: [
    { name: "Sleipnir"    , legs: 8 },
    { name: "Jormungandr" , legs: 0 },
    { name: "Hel"         , legs: 2 }
  ]
).

// collect all the inserted items & put them in an array using HighlandJS
// this is similar to something like cursor.toArray() in mongodb
pipe(_().collect()).

// wait for the data to be emitted
on("data", function(people) {

  // load all people who have more than 0 legs
  peopleDb("load", {
    multi: true,
    query: {
      legs: { $gt: 0 }
    }
  }).
  pipe(_().collect()).
  on("data", function(people) {
      // do stuff with loaded people
  });

});

db lokidb(targetOrOptions)

Creates a new crudlet-based db

  • targetOrOptions - the target loki DB or the options for a new loki db
var db = lokidb(__dirname + "/db.json");
var db = lokidb(new loki(__dirname + "/db.json"));

db(operationName, options)

Runs a new operation on the loki DB.

Note that options.collection must be present when performing operations. The easiest & probably best way to do this is to create a child crudlet db.

// remove all people where ages are greater than zero
db("remove", {
  collection: "people",
  query: {
    age: { $gt: 0 }
  }
}).on("data", function() {

});

insert

Insert operation.

var peopleDb = crud.child(db, { collection: "people" });

// insert multiple
peopleDb("insert", { data: [{ name: "john"}, { name: "matt" }]}).on("data", function() {
  // this is called twice.
});

update

Update operation

peopleDb("update", {
  query: { /* mongodb query here */ },
  data: { /* data to update*/ },
  multi: true // TRUE if you want to update multiple items
}).on("data", function() {
  // emits updated documents
});

upsert

Updates a document if it's found, or inserts one.

peopleDb("upsert", {
  query: { /* mongodb query here */ },
  data: { /* data to update or insert here */ }
}).on("data", function() {
  // emits updated documents
});

remove

Removes a document

peopleDb("upsert", {
  query: { /* mongodb query here */ },
  data: { /* data to update*/ },
  multi: true, // TRUE if you want to remove multiple items
}).on("end", function() {

});

load

Removes a document

peopleDb("upsert", {
  query: { /* mongodb query here */ },
  multi: true, // TRUE if you want to load multiple items
}).on("data", function() {

});

Interoperability with other database

crudlet-loki works well with other crudlet adapters. Below are a some examples of what you can do.

Realtime data

Use whatever realtime adapter you want - pubnub, webrtc, socket.io. Here's an example with pubnub:

var pubnub = require("crudlet-pubnub");
var loki   = require("crudlet-loki");
var crud   = require("crudlet");

var remotedb = pubnub(ops);

// tailable makes "tail" an option for databases that don't support it (such as lokidb).
// "tail" gets emmited whenever there's an operation executed against the database
var memdb    = crud.tailable(loki());

// tail all remote operations to the memory database
remotedb("tail").pipe(crud.open(memdb));

// tail operations on the in-memory database & pass them back to pubnub
// NOTE that operations coming from pubnub won't get re-published.
memdb("tail").pipe(crud.open(remotedb));

// insert data to the local database - this will also get sent to pubnub
memdb("insert", {
  collection: "people"
  data: {
    name: "Will Ferrell"
  }
});

mesh-loki's People

Watchers

Chua Kiem Fai avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.