/
index.js
62 lines (49 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var fs = require('fs')
, path = require('path')
, utils = require('utilities')
, MemoryAdapter = require('../memory').Adapter
, _baseConfig;
_baseConfig = {
location: '.'
, filename: '_datastore.json'
};
var Adapter = function (options) {
var opts = options || {}
, config;
this.name = 'filesystem';
this.config = this.loadConfig(_baseConfig, opts);
this.client = null;
this.init.apply(this, arguments);
};
Adapter.prototype = Object.create(MemoryAdapter.prototype);
Adapter.prototype.constructor = Adapter;
utils.mixin(Adapter.prototype, new (function () {
this.destroy = function (cb) {
var cfg = this.config
, p = path.join(cfg.location, cfg.filename);
utils.file.rmRf(p, {
silent: true
});
if (typeof cb == 'function') {
cb();
}
};
this._getDatastore = function () {
var data = {}
, cfg = this.config
, p = path.join(cfg.location, cfg.filename);
if (fs.existsSync(p)) {
data = fs.readFileSync(p);
data = data.toString();
data = JSON.parse(data);
}
return data;
};
this._writeDatastore = function (data) {
var cfg = this.config
, p = path.join(cfg.location, cfg.filename);
utils.file.mkdirP(cfg.location);
fs.writeFileSync(p, JSON.stringify(data));
};
})());
module.exports.Adapter = Adapter;