-
Notifications
You must be signed in to change notification settings - Fork 8
/
fs.js
153 lines (131 loc) · 4.18 KB
/
fs.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var fs = require('fs'),
path = require('path'),
Q = require('q'),
_ = require('underscore-data'),
BaseBackend = require('./base');
function resolvedPromise(val) {
var d = Q.defer();
d.resolve(val);
return d.promise;
}
/** @class FsBackend
@extends BaseBackend */
var FsBackend = BaseBackend.extend(
/** @lends FsBackend# */
{
/** @method */
initialize: function(options) {
BaseBackend.prototype.initialize.call(this, options);
},
/** @method */
objectStoreNames: function() {
return Q.ninvoke(fs, 'readdir', this.options.path);
},
/** @method */
objectStore: function(name, options) {
name = path.basename(name);
return new FsStore(this, name, options);
},
/** @method */
createObjectStore: function(name, options) {
var d = Q.defer();
name = path.basename(name);
fs.mkdir(path.join(this.options.path, name), function() {
d.resolve(this.objectStore(name, options));
}.bind(this));
return d.promise;
},
/** @method */
deleteObjectStore: function(name) {
name = path.basename(name);
this.objectStore(name).clear().then(function() {
return Q.ninvoke(fs, 'rmdir', path.join(this.options.path, name));
});
},
/** Encode object into string, by default uses JSON.stringify
Override to use different serialization
@method */
encode: JSON.stringify,
/** Decode string into object, by default uses JSON.parse
Override to use different serialization
@method */
decode: JSON.parse
});
/** @class FsStore
@extends BaseStore */
var FsStore = BaseBackend.BaseStore.extend(
/** @lends FsStore# */
{
/** @method */
initialize: function(backend, name, options) {
BaseBackend.BaseStore.prototype.initialize.call(this, backend, name, options);
},
/** @method */
get: function(directives) {
var key = this._getObjectKey({}, directives);
return Q.ninvoke(fs, 'readFile', this.filename(key), 'utf-8')
.then(function(x) {
return this.backend.decode(x);
}.bind(this));
},
/** @method */
add: function(object, directives) {
var key = this._getObjectKey(object, directives);
return Q.ninvoke(fs, 'writeFile', this.filename(key), this.backend.encode(object), {flag: 'wx'}).then(function() {
return object;
});
},
/** @method */
put: function(object, directives) {
var key = this._getObjectKey(object, directives);
return Q.ninvoke(fs, 'writeFile', this.filename(key), this.backend.encode(object), {flag: 'w'}).then(function() {
return object;
});
},
/** @method */
'delete': function(directives) {
var key = this._getObjectKey({}, directives),
d = Q.defer();
Q.ninvoke(fs, 'unlink', this.filename(key))
.then(function() {
d.resolve(1);
})
.fail(function() {
d.resolve(0);
});
return d.promise;
},
/** Execute RQL query */
query: function(query) {
var self = this;
return Q.ninvoke(fs, 'readdir', path.join(this.backend.options.path, this.name))
.then(function(files) {
return Q.all(files.map(function(f) {
return self.get(f);
}));
})
.then(function(vals) {
return _.query(vals, query);
});
},
/** Delete all items */
clear: function() {
var self = this;
return Q.ninvoke(fs, 'readdir', path.join(this.backend.options.path, this.name))
.then(function(files) {
return Q.all(files.map(function(f) {
return self.delete(f);
}));
})
.then(function() {
return true;
});
},
/** Get absolute filename for given key */
filename: function(file) {
return path.join(this.backend.options.path, this.name, ''+path.basename(file));
}
});
/** @module fs */
FsBackend.FsStore = FsStore;
module.exports = FsBackend;