Coder Social home page Coder Social logo

jdrew1303 / level-immutable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eugeneware/level-immutable

0.0 2.0 0.0 144 KB

LevelDB/Levelup immutable history and database snapshotting based on ideas in datomic

License: Other

JavaScript 100.00%

level-immutable's Introduction

level-immutable

LevelDB/Levelup immutable history and database snapshotting based on ideas in datomic.

build status

Installation

This module is installed via npm:

$ npm install level-immutable

Background

The datomic database simplifies database by making all it's data mutable, and by storing facts and data changes with the timestamp that they happened, so you can interrogate the database at a particular point in the past with a consistent "snapshot" of the data at that time.

This module emulates this behaviour, allowing you to pass in fromTime and toTime values into the levelup options object in order to get the answers at that particular time.

Example Usage

var immutable = require('level-immutable'),
    bytewise = require('bytewise'),
    level = require('level');

// create the database and make it immutable
var db = immutable(level('/path/to/my/db',
  { keyEncoding: bytewise, valueEncoding: 'json' }));

// these commands will be execute with a slight delay
var cmds = [ put, update, del ];

function put(cb) {
  db.immutable.put('eugene', { name: 'Eugene', color: 'blue' }, cb);
}

function update(cb) {
  db.immutable.put('eugene', { name: 'Eugene', color: 'black' }, cb);
}

function del(cb) {
  db.immutable.del('eugene', cb);
}

// times will store the timestamps when the operations were each completed
var times = [], i = 0;
(function next() {
  if (cmds.length === 0) return get();
  var cmd = cmds.shift();
  cmd(function (err) {
    if (err) return done(err);
    // store the timestamp away
    times[i++] = Date.now();
    setTimeout(next, 5);
  });
})();

// get the value of 'eugene' at the time just before it was deleted
// effectively this uses a snapshot of the database at that time
function get() {
  db.immutable.get('eugene', { toTime: times[1] }, check);
}

function check(err, data) {
  if (err) return done(err);
  // the data was fetched even though it was 'deleted'
  // a db.get('eugene'), would throw a 'NotFoundError'
  expect(data).to.eql({ name: 'Eugene', color: 'black' });
}

level-immutable's People

Contributors

eugeneware avatar

Watchers

James Cloos avatar James Drew 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.