node.js的快速進程中的平面文件數據庫,該數據庫支持JSON並在內存中緩存所有數據。使用僅附加算法確保緊湊的文件大小和強大的一致性,將所有數據持續到打開文件。
npm install flat-file-db
將數據庫文件傳遞給Flat-File-DB構造函數,並等待數據庫打開。打開時,所有數據都已加載到內存中。
var flatfile = require ( 'flat-file-db' ) ;
var db = flatfile ( '/tmp/my.db' ) ;
db . on ( 'open' , function ( ) {
db . put ( 'hello' , { world : 1 } ) ; // store some data
console . log ( db . get ( 'hello' ) ) // prints {world:1}
db . put ( 'hey' , { world : 2 } , function ( ) {
// 'hey' is now fully persisted
} ) ;
} ) ;
如果您不想等待它打開,請使用flatfile.sync
var db = flatfile . sync ( '/tmp/my.db' ) ;
console . log ( db . get ( 'hello' ) ) ; // prints {world:1}
如果您發行多個寫作,最後一篇將永遠獲勝
for ( var i = 0 ; i < 10 ; i ++ ) {
db . put ( 'test' , { count : i } , ... ) ;
}
console . log ( db . get ( 'test' ) ) ; // {count:9} which also the persisted value of 'test'
db = flatfile(path, opts)
創建一個新的DB實例。根據默認情況下,fsync被稱為所有排名。禁用此組opts.fsync = false
db = flatfile.sync(path, opts)
與上述相同,除了您不需要等待公開事件
db.put(key, val, [cb])
插入或更新新密鑰
db.del(key, [cb])
刪除鍵
db.get(key) -> doc
獲取鍵的值
db.has(key) -> bool
true如果db具有密鑰
db.keys() -> list
將所有鑰匙作為數組獲取
db.clear([cb])
清除所有值的數據庫
db.close()
關閉數據庫
db.on('open')
開放並準備使用時發射。
db完全關閉時db.on('close')
發射
db.on('drain')
所有put和刪除均沖洗到磁盤
麻省理工學院