LevelDB/Levelup immutable history and database snapshotting based on ideas in datomic
LevelDB/Levelup immutable history and database snapshotting based on ideas in datomic.
This module is installed via npm:
$ npm install level-immutable
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
toTime values into the levelup
options object in order to get the answers
at that particular time.
var immutable = require'level-immutable'bytewise = require'bytewise'level = require'level';// create the database and make it immutablevar db = immutablelevel'/path/to/my/db'keyEncoding: bytewise valueEncoding: 'json' ;// these commands will be execute with a slight delayvar cmds = put update del ;dbimmutableput'eugene' name: 'Eugene' color: 'blue' cb;dbimmutableput'eugene' name: 'Eugene' color: 'black' cb;dbimmutabledel'eugene' cb;// times will store the timestamps when the operations were each completedvar times = i = 0;if cmdslength === 0 return get;var cmd = cmdsshift;cmdif err return doneerr;// store the timestamp awaytimesi++ = Datenow;setTimeoutnext 5;;;// get the value of 'eugene' at the time just before it was deleted// effectively this uses a snapshot of the database at that timedbimmutableget'eugene' toTime: times1 check;if err return doneerr;// the data was fetched even though it was 'deleted'// a db.get('eugene'), would throw a 'NotFoundError'expectdatatoeql name: 'Eugene' color: 'black' ;