Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Last active December 17, 2015 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominictarr/5559302 to your computer and use it in GitHub Desktop.
Save dominictarr/5559302 to your computer and use it in GitHub Desktop.
level-atomic-data
//datomic clone
var log = db.sublevel('log')
var audit = db.sublevel('audit')
//var sha1sum = require('sha1sum')
//initialize current sha
db.pre(function (op, add) {
var ts = timestamp() //or get timestamp from a transactor service
//save this value at this timestamp.
add({
key: op.key + '!' + ts,
value: op.value,
prefix: log
})
//also, save a log that this key was updated at this time
//maybe also track who did it, and maybe include the hash of the previous ts
//which would make it impossible to insert fake old records
add({
key: ts, value: op.key, prefix: audit
}
})
/*
put KEY VALUE ->
KEY -> VALUE //latest value is stored without timestamp, for fast access.
KEY : timestamp -> VALUE //can check out records at any timestamp
timestamp -> KEY //can read what was updated at which timestamp
normally, you retrive data like this:
*/
db.get(key, function (err, value) {
})
/*
but if you want to get data from a particular timestamp
use `level-peek` to get the last item in the range,
for that key:
*/
var peek = require('level-peek')
peek.last(log, key+'!'+timestamp, function (err, val) {
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment