-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from prey/local_db
Store/restore running commands on shutdown/boot.
- Loading branch information
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var fs = require('fs'), | ||
common = require('./common'), | ||
db_path = common.system.tempfile_path('local.db'); | ||
|
||
var db; | ||
|
||
var load = function(cb) { | ||
if (db) return cb(); | ||
|
||
fs.readFile(db_path, 'utf8', function(err, data) { | ||
if (err || data.trim() == '') return cb(err); | ||
|
||
try { | ||
db = JSON.parse(data); | ||
} catch(e) { | ||
db = {}; | ||
err = e; | ||
} | ||
cb(err); | ||
}) | ||
} | ||
|
||
var save = function(cb) { | ||
var err, str = JSON.stringify(db, null, 0); | ||
try { | ||
fs.writeFileSync(db_path, str); | ||
} catch(e) { | ||
err = e; | ||
} | ||
cb(err); | ||
} | ||
|
||
exports.set = function(key, data, cb) { | ||
load(function(err) { | ||
if (err) return cb(err); | ||
db[key] = data; | ||
save(cb); | ||
}) | ||
} | ||
|
||
exports.get = function(key, cb) { | ||
load(function(err) { | ||
if (err) return cb(err); | ||
|
||
cb(null, db[key]); | ||
}) | ||
} | ||
|
||
exports.all = function(cb) { | ||
load(function(err) { | ||
if (err) return cb(err); | ||
|
||
cb(null, db); | ||
}) | ||
} | ||
|
||
exports.clear = function(cb) { | ||
db = {}; | ||
// fs.writeFile(db_path, '', cb); | ||
fs.unlink(db_path); | ||
} |