mind is lightweight asynchronous persistence layer for node
mind is not a database / minimal database is lightweight asynchronous persistence layer for node.
mind was inspired by anti-db's idea that often, "you don't need a database, you need an in-memory object that saves itself to disk". However, I needed Windows support, evented API, and asynchronous loading, which, while simpler and more elegant in implementation, it forgoes.
npm install mind
var mind = require'mind';var opts = encoding: 'UTF-8' autosave: 60000 ;var dbm = 'path/to/db.json' opts;dbmon'open'console.log'[INFO] Opened db: ' + emsg;edb'foo' = 'bar';edb'baz' = 1 2 3 4;;dbmon'close'console.log'[INFO] Closed db: ' + emsg;;dbmon'save'console.log'[INFO] Saved db: ' + emsg;;dbmon'error'console.error'[ERROR] Error in db: ' + emsg;console.errore.error;;dbmopen;
var mind = require'mind';mindopen'path/to/db.json'
fn: String - Path writeable by the node process
opts: Object - Options hash containing any of:
autosave: Number - ms between save operations (0 or negative disables autosave). Default:
encoding: String - encoding used to read and write JSON file. Default:
Create a new
Open and read the database file, creating it if necessary.
Asynchronously save the database file to disk.
Synchronously closes the database file, persisting it to disk. This function
is synchronous to support non-autosaved use of mind, and is suitable for
Synchronously start and stop the autosave process.
db: Object - Database object for which to get the associated
Get the MindManager for a particular database object.
cb: Callback used to respond to all events emitted by the created manager:
dbas the new database object
erras original error thrown, and
dataas the complete event data object
Handles the creation of a manager object in a simplified single-callback
model. It is recommended to not
bind your callback to any particular context
so you can access the manager instance at
this. If you must, though,
the instance managing a particular db object can be accessed by:
event.db: The database object opened.
event.msg: Any message regarding the open operation.
Emitted on successful opening of a database.
event.msg: Any message regarding the close operation.
Emitted on successful closing of a database.
event.msg: Any message regarding the save operation.
Emitted on successful save of a database to persistent storage, either explicitly-initiated or autosave.
event.error: The originally-thrown error, if available.
event.msg: Any message regarding the error.
Emitted on any non-fatal caught error.