npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crudlet-loki

v1.0.0

Published

lokijs adapter for crudlet

Downloads

16

Readme

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"
  }
});