ちょっとした永続化にnStoreを使ってみる
nodeのシンプルな KVS。
DBMを立ち上げる必要がないJSONベースのKVSが欲しかった。
npm install nstore
試す。相変わらずcoffeescript。
./test.dbで起動
mizchi-mba:mzi%[~] coffee
coffee> nstore = require('nstore')
{ initialize: [Function: initialize],
length: [Getter],
genKey: [Function: genKey],
loadDatabase: [Function],
save: [Function: save],
get: [Function: getByKey],
checkQueue: [Function: checkQueue],
remove: [Function: removeByKey] }
coffee> testdb = nstore.new("./test.db")
{ filename: './test.db',
fd: null,
index: {},
toWriteCallbacks: [],
toWrite: {},
isWriting: {},
stale: 0,
dbLength: 0,
busy: true,
filterFn: null }save & get
coffee> testdb.save 'foo',a:1,b:'str',c:new Date, (e)->console.log 'save done'
save done
coffee> testdb.get 'foo', (e,doc,key)->console.log doc,key
{ a: 1, b: 'str', c: Mon, 14 Nov 2011 14:22:39 GMT } 'foo'remove
coffee> testdb.remove 'foo', -> console.log 'remove done' remove done coffee> testdb.get 'foo', (e,doc,key)->console.log doc,key undefined undefined
簡単。一応条件指定クエリもある。
GitHub - creationix/nstore: nStore is a simple, in-process key/value database for node.js
追記
特定のオブジェクトが保存できねーなーと思ってたら循環参照してた。オンメモリの間は普通に保持し続けるからややこしかった。