-
Notifications
You must be signed in to change notification settings - Fork 11
/
jsonfile-store.js
321 lines (274 loc) · 8.15 KB
/
jsonfile-store.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'use strict'
var Fs = require('fs')
var Path = require('path')
var _ = require('lodash')
var error = require('eraro')({ package: 'seneca-jsonfile-store' })
var name = 'jsonfile-store'
module.exports = jsonfile_store
Object.defineProperty(module.exports, 'name', { value: 'jsonfile-store' })
function jsonfile_store(options) {
var seneca = this
seneca.depends('jsonfile-store', ['entity'])
options = seneca.util.deepextend(
{
must_merge: false,
// TODO: use seneca.export once it allows for null values
generate_id: seneca.root.private$.exports['entity/generate_id']
},
options
)
options.folder = Path.normalize(options.folder || '.')
var isodate_re = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/
var filename_re = /\.json$/
// FIX: this is a bit silly, refactor out
function good(args, err, cb) {
if (err) {
cb(error('entity_error', { store: name, error: err, args: args }))
return false
} else return true
}
/**
* Return if the folder exists, create otherwise.
*/
function ensurefolder(folder, cb) {
Fs.stat(folder, function(err, stat) {
if (!err && stat.isDirectory()) {
return cb()
}
Fs.mkdir(folder, function(err) {
if (err && err.code === 'EEXIST') {
err = null
}
cb(err)
})
})
}
function makefolderpath(ent) {
var canon = ent.canon$({ object: true })
var base = canon.base
var name = canon.name
var entfolder = (base ? base + '_' : '') + name
var folderpath = Path.join(options.folder, entfolder)
return folderpath
}
function do_load(args, qent, filepath, cb) {
Fs.readFile(filepath, function(err, jsonstr) {
if (err && 'ENOENT' === err.code) {
return cb(null, null)
} else if (good(args, err, cb)) {
// TODO: handle JSON parse error
var data = JSON.parse(jsonstr, function(key, val) {
if (_.isString(val)) {
if (val.match(isodate_re)) {
return new Date(val)
} else return val
} else return val
})
var fent = qent.make$(data)
cb(null, fent)
}
})
}
function do_list(args, qent, q, cb) {
// used by load, list, remove
var entlist = []
var folderpath = makefolderpath(qent)
ensurefolder(folderpath, function(err) {
if (good(args, err, cb)) {
Fs.readdir(folderpath, function(err, filelist) {
if (good(args, err, cb)) {
nextfile(0)
}
function nextfile(i) {
var filename = filelist[i]
if (filename) {
if (filename.match(filename_re)) {
var filepath = Path.join(folderpath, filename)
do_load(args, qent, filepath, function(err, fent) {
if (good(args, err, cb)) {
// match query
for (var p in q) {
if (!~p.indexOf('$') && q[p] !== fent[p]) {
return nextfile(i + 1)
}
}
entlist.push(fent)
nextfile(i + 1)
}
})
} else nextfile(i + 1)
} else cb(null, entlist)
}
})
}
})
}
function do_remove(args, q, ent, cb) {
var folderpath = makefolderpath(ent)
ensurefolder(folderpath, function(err) {
if (good(args, err, cb)) {
var filepath = Path.join(folderpath, ent.id + '.json')
var filepath_DELETE = Path.join(folderpath, ent.id + '.json.DELETE')
Fs.rename(filepath, filepath_DELETE, function(err) {
if (good(args, err, cb)) {
cb()
}
})
}
})
}
var store = {
name: name,
save: function(args, cb) {
var ent = args.ent
var create = !ent.id
if (null != ent.id$) {
var id = ent.id$
delete ent.id$
do_save(id)
} else if (create) {
id = options.generate_id ? options.generate_id() : void 0
if (undefined !== id) {
return do_save(id)
} else {
seneca.act({ role: 'basic', cmd: 'generate_id' }, function(err, id) {
if (err) return cb(err)
do_save(id)
})
}
} else do_save()
function do_save(id) {
if (id) {
ent.id = id
}
var folderpath = makefolderpath(ent)
ensurefolder(folderpath, function(err) {
function handledate_replacer(key, val) {
if (_.isDate(val)) {
return val.toISOString()
} else return val
}
if (good(args, err, cb)) {
var filepath = Path.join(folderpath, ent.id + '.json')
var entdata = ent.data$()
var jsonstr = JSON.stringify(entdata, handledate_replacer)
if (options.must_merge) {
return cb(error('store-merge-unsupported', { args: args }))
}
Fs.writeFile(filepath, jsonstr, function(err) {
if (good(args, err, cb)) {
seneca.log.debug(
args.actid$,
'save/' + (create ? 'insert' : 'update'),
ent,
desc
)
cb(null, ent)
}
})
}
})
}
},
load: function(args, cb) {
var qent = args.qent
var q = args.q
if (q.id) {
var folderpath = makefolderpath(qent)
ensurefolder(folderpath, function(err) {
if (good(args, err, cb)) {
var filepath = Path.join(folderpath, q.id + '.json')
do_load(args, qent, filepath, function(err, fent) {
if (good(args, err, cb)) {
seneca.log.debug(args.actid$, 'load', q, fent, desc)
cb(null, fent)
}
})
}
})
} else {
do_list(args, qent, q, function(err, entlist) {
if (good(args, err, cb)) {
cb(null, entlist[0])
}
})
}
},
list: function(args, cb) {
var qent = args.qent
var q = args.q
do_list(args, qent, q, cb)
},
remove: function(args, cb) {
var qent = args.qent
var q = args.q
var all = q.all$ // default false
var load = _.isUndefined(q.load$) ? true : q.load$ // default true
if (all) {
do_list(args, qent, q, function(err, entlist) {
function next_remove(i) {
var ent = entlist[i]
if (ent) {
do_remove(args, q, ent, function(err) {
if (good(args, err, cb)) {
next_remove(i + 1)
}
})
} else return cb()
}
if (good(args, err, cb)) {
next_remove(0)
}
})
} else {
q.limit$ = 1
do_list(args, qent, q, function(err, list) {
if (good(args, err, cb)) {
var fent = list[0]
if (fent) {
do_remove(args, q, fent, function(err) {
if (good(args, err, cb)) {
cb(null, load ? fent : null)
}
})
} else cb(null, null)
}
})
}
},
close: function(args, done) {
done()
},
native: function(args, done) {
done(null, options.folder)
}
}
var storedesc = seneca.store.init(seneca, options, store)
var tag = storedesc.tag
var desc = storedesc.desc
seneca.add({ init: store.name, tag: tag }, function(args, done) {
Fs.exists(options.folder, function(exists) {
if (!exists) {
return done(
error('folder-not-found', { folder: options.folder, store: desc })
)
}
var markerfile = Path.join(options.folder, 'seneca.txt')
Fs.writeFile(markerfile, 'This is a jsonfile-store folder.', function(
err
) {
if (err) {
return done(
error('folder-not-writable', {
folder: options.folder,
store: desc,
error: err
})
)
}
return done()
})
})
})
return { name: store.name, tag: tag }
}