Last active
August 19, 2024 22:37
-
-
Save dominictarr/5559302 to your computer and use it in GitHub Desktop.
level-atomic-data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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