2
2
Copyright (C) 2012 - 2016 Markus Kohlhase <[email protected] >
3
3
*/
4
4
5
- "use strict" ;
6
-
7
5
import async from 'async' ;
8
6
import fs from 'fs' ;
9
7
import path from 'path' ;
@@ -19,9 +17,9 @@ const getIDs = a => a.filter(isJSONFile).map(removeFileExtension);
19
17
20
18
const readIDsSync = d => getIDs ( fs . readdirSync ( d ) ) ;
21
19
22
- const readIDs = ( d , cb ) => fs . readdir ( d , ( err , ids ) => {
23
- cb ( err , getIDs ( ids ) ) ;
24
- } ) ;
20
+ const readIDs = ( d , cb ) => fs . readdir ( d , ( err , ids ) =>
21
+ cb ( err , getIDs ( ids ) )
22
+ ) ;
25
23
26
24
const getObjectFromFileSync = function ( id ) {
27
25
try {
@@ -49,10 +47,7 @@ const FILE_IS_WRITABLE = fs.constants ? fs.constants.W_OK : fs.W_OK;
49
47
50
48
const canWriteToFile = ( file , cb ) => {
51
49
fs . access ( file , FILE_EXISTS , ( err ) => {
52
- if ( err ) {
53
- return cb ( null ) ;
54
- }
55
-
50
+ if ( err ) return cb ( null ) ;
56
51
fs . access ( file , FILE_IS_WRITABLE , cb ) ;
57
52
} ) ;
58
53
} ;
@@ -68,8 +63,8 @@ const canWriteToFileSync = (file) => {
68
63
} ;
69
64
70
65
const saveObjectToFile = function ( o , file , cb ) {
71
- var json ;
72
66
const indent = this . _pretty ? 2 : void 0 ;
67
+ let json ;
73
68
try {
74
69
json = JSON . stringify ( o , null , indent ) ;
75
70
} catch ( error ) {
@@ -81,16 +76,13 @@ const saveObjectToFile = function(o, file, cb) {
81
76
}
82
77
83
78
const tmpFileName = file + uuid . v4 ( ) + ".tmp" ;
79
+
84
80
if ( cb != null ) {
85
81
canWriteToFile ( file , ( err ) => {
86
- if ( err ) {
87
- return cb ( err ) ;
88
- }
82
+ if ( err ) return cb ( err ) ;
89
83
90
84
fs . writeFile ( tmpFileName , json , 'utf8' , ( err ) => {
91
- if ( err ) {
92
- return cb ( err ) ;
93
- }
85
+ if ( err ) return cb ( err ) ;
94
86
95
87
fs . rename ( tmpFileName , file , cb ) ;
96
88
} ) ;
@@ -109,7 +101,7 @@ const saveObjectToFile = function(o, file, cb) {
109
101
const id2fileName = ( id , dir ) => path . join ( dir , id + ".json" ) ;
110
102
111
103
const save = function ( id , o , cb ) {
112
- var backup , k ;
104
+ let backup , k ;
113
105
if ( typeof id === "object" ) {
114
106
cb = o ;
115
107
o = id ;
@@ -152,28 +144,25 @@ const save = function(id, o, cb) {
152
144
} ;
153
145
} ) ( this ) ;
154
146
155
- if ( this . _memory ) {
156
- return done ( ) ;
147
+ if ( this . _memory ) return done ( ) ;
148
+
149
+ if ( cb != null ) {
150
+ saveObjectToFile . call ( this , data , file , done ) ;
157
151
} else {
158
- if ( cb != null ) {
159
- saveObjectToFile . call ( this , data , file , done ) ;
160
- } else {
161
- return done ( saveObjectToFile . call ( this , data , file ) ) ;
162
- }
152
+ return done ( saveObjectToFile . call ( this , data , file ) ) ;
163
153
}
164
154
} ;
165
155
166
156
const get = function ( id , cb ) {
167
- var err , o ;
168
- o = clone ( this . _cache [ id ] ) ;
157
+ let o = clone ( this . _cache [ id ] ) ;
169
158
if ( o != null ) {
170
159
return ( cb != null ? cb ( null , o ) : o ) ;
171
160
}
172
- const done = ( function ( _this ) {
173
- return function ( err , o ) {
174
- var e , item ;
161
+ const done = ( ( _this ) => {
162
+ return ( err , o ) => {
163
+ let e , item ;
175
164
if ( err ) {
176
- e = new Error ( "could not load data" ) ;
165
+ const e = new Error ( "could not load data" ) ;
177
166
if ( cb != null ) {
178
167
return cb ( e ) ;
179
168
} else {
@@ -197,25 +186,25 @@ const get = function(id, cb) {
197
186
}
198
187
} ;
199
188
} ) ( this ) ;
200
- if ( this . _memory ) {
201
- return done ( null , o ) ;
202
- }
203
- if ( cb != null ) {
204
- return getObjectFromFile . call ( this , id , done ) ;
205
- }
206
- err = ( o = getObjectFromFileSync . call ( this , id ) ) instanceof Error ;
189
+
190
+ if ( this . _memory ) return done ( null , o ) ;
191
+
192
+ if ( cb != null ) return getObjectFromFile . call ( this , id , done ) ;
193
+
194
+ const err = ( o = getObjectFromFileSync . call ( this , id ) ) instanceof Error ;
195
+
207
196
return done ( ( err ? o : void 0 ) , ( ! err ? o : void 0 ) ) ;
208
197
} ;
209
198
210
199
const remove = function ( id , cb ) {
211
- var cacheBackup , e , err , notInCache , o ;
200
+ let e , err , notInCache , o ;
212
201
const file = this . _getFileName ( id ) ;
213
- cacheBackup = this . _cache [ id ] ;
202
+ const cacheBackup = this . _cache [ id ] ;
214
203
if ( cacheBackup == null ) {
215
204
notInCache = new Error ( id + " does not exist" ) ;
216
205
}
217
- const done = ( function ( _this ) {
218
- return function ( err ) {
206
+ const done = ( ( _this ) => {
207
+ return ( err ) => {
219
208
if ( err ) {
220
209
_this . _cache [ id ] = cacheBackup ;
221
210
return ( cb != null ? cb ( err ) : err ) ;
@@ -224,58 +213,62 @@ const remove = function(id, cb) {
224
213
return typeof cb === "function" ? cb ( ) : void 0 ;
225
214
} ;
226
215
} ) ( this ) ;
216
+
227
217
if ( this . _single ) {
228
218
delete this . _cache [ id ] ;
229
219
if ( this . _memory || ( notInCache != null ) ) {
230
220
return done ( notInCache ) ;
231
221
}
222
+
232
223
if ( cb != null ) {
233
224
return saveObjectToFile . call ( this , this . _cache , file , done ) ;
234
225
}
226
+
235
227
err = ( o = saveObjectToFile . call ( this , this . _cache , file ) ) instanceof Error ;
236
228
return done ( ( err ? o : void 0 ) , ( ! err ? o : void 0 ) ) ;
237
- } else {
238
- if ( this . _memory ) {
239
- return done ( notInCache ) ;
240
- }
241
- if ( cb != null ) {
242
- return fs . unlink ( file , done ) ;
243
- }
244
- try {
245
- return done ( fs . unlinkSync ( file ) ) ;
246
- } catch ( error ) {
247
- e = error ;
248
- return done ( e ) ;
249
- }
229
+ }
230
+
231
+ if ( this . _memory ) return done ( notInCache ) ;
232
+
233
+ if ( cb != null ) return fs . unlink ( file , done ) ;
234
+
235
+ try {
236
+ return done ( fs . unlinkSync ( file ) ) ;
237
+ } catch ( error ) {
238
+ return done ( error ) ;
250
239
}
251
240
} ;
252
241
253
242
class Store {
254
243
255
- constructor ( name , opt ) {
256
- this . name = name != null ? name : 'store' ;
257
- if ( opt == null ) {
258
- opt = { } ;
259
- }
244
+ constructor ( name = 'store' , opt = { } ) {
245
+
246
+ this . name = name ;
260
247
this . _single = opt . single === true || opt . type === 'single' ;
261
248
this . _pretty = opt . pretty === true ;
262
249
this . _memory = opt . memory === true || opt . type === 'memory' ;
263
250
this . _saveId = opt . saveId ;
251
+
264
252
if ( isJSONFile ( this . name ) ) {
265
253
this . name = this . name . split ( ".json" ) [ 0 ] ;
266
254
this . _single = true ;
267
255
}
256
+
268
257
this . _dir = path . resolve ( this . name ) ;
258
+
269
259
if ( this . _single ) {
270
260
this . _dir = path . dirname ( this . _dir ) ;
271
261
}
262
+
272
263
this . _cache = { } ;
264
+
273
265
if ( ! this . _memory ) {
274
266
mkdirp . sync ( this . _dir ) ;
275
267
}
268
+
276
269
if ( this . _single ) {
277
- const fn = this . _getFileName ( ) ;
278
270
if ( ! this . _memory ) {
271
+ const fn = this . _getFileName ( ) ;
279
272
if ( ! fs . existsSync ( fn ) ) {
280
273
if ( fs . writeFileSync ( fn , "{}" , 'utf8' ) ) {
281
274
throw new Error ( "could not create database" ) ;
@@ -294,106 +287,99 @@ class Store {
294
287
}
295
288
}
296
289
297
-
298
-
299
290
save ( id , o , cb ) {
300
291
if ( cb == null ) {
301
- cb = function ( ) { } ;
292
+ cb = ( ) => { } ;
302
293
}
303
294
return save . call ( this , id , o , cb ) ;
304
- } ;
295
+ }
305
296
306
297
saveSync ( id , o ) {
307
298
return save . call ( this , id , o ) ;
308
- } ;
299
+ }
309
300
310
301
get ( id , cb ) {
311
302
if ( cb == null ) {
312
- cb = function ( ) { } ;
303
+ cb = ( ) => { } ;
313
304
}
314
- return get . call ( this , id , cb ) ;
315
- } ;
305
+ get . call ( this , id , cb ) ;
306
+ }
316
307
317
308
getSync ( id ) {
318
309
return get . call ( this , id ) ;
319
- } ;
310
+ }
320
311
321
312
delete ( id , cb ) {
322
- return remove . call ( this , id , cb ) ;
323
- } ;
313
+ remove . call ( this , id , cb ) ;
314
+ }
324
315
325
316
deleteSync ( id ) {
326
317
return remove . call ( this , id ) ;
327
- } ;
318
+ }
328
319
329
320
all ( cb ) {
330
- if ( cb == null ) {
331
- cb = function ( ) { } ;
332
- }
333
- if ( this . _memory ) {
334
- return cb ( null , this . _cache ) ;
335
- } else if ( this . _single ) {
321
+
322
+ if ( cb == null ) cb = ( ) => { } ;
323
+
324
+ if ( this . _memory ) return cb ( null , this . _cache ) ;
325
+
326
+ if ( this . _single ) {
336
327
return getObjectFromFile . call ( this , void 0 , cb ) ;
337
- } else {
338
- return readIDs ( this . _dir , ( function ( _this ) {
339
- return function ( err , ids ) {
340
- var all , id , loaders , that ;
341
- if ( typeof er !== "undefined" && er !== null ) {
342
- return cb ( err ) ;
343
- }
344
- that = _this ;
345
- all = { } ;
346
- loaders = ( function ( ) {
347
- var i , len , results ;
348
- results = [ ] ;
349
- for ( i = 0 , len = ids . length ; i < len ; i ++ ) {
350
- id = ids [ i ] ;
351
- results . push ( ( function ( id ) {
352
- return function ( cb ) {
353
- return that . get ( id , function ( err , o ) {
354
- if ( ! err ) {
355
- all [ id ] = o ;
356
- }
357
- return cb ( err ) ;
358
- } ) ;
359
- } ;
360
- } ) ( id ) ) ;
361
- }
362
- return results ;
363
- } ) ( ) ;
364
- return async . parallel ( loaders , function ( err ) {
365
- return cb ( err , all ) ;
366
- } ) ;
367
- } ;
368
- } ) ( this ) ) ;
369
328
}
370
- } ;
329
+ readIDs ( this . _dir , ( ( that ) => {
330
+ return ( err , ids ) => {
331
+ if ( typeof err !== "undefined" && err !== null ) {
332
+ return cb ( err ) ;
333
+ }
334
+
335
+ let all = { } ;
336
+
337
+ const loaders = ( ( ) => {
338
+ let i , len ;
339
+ let results = [ ] ;
340
+ for ( i = 0 , len = ids . length ; i < len ; i ++ ) {
341
+ const id = ids [ i ] ;
342
+ results . push ( ( ( id ) =>
343
+ ( cb ) => that . get ( id , ( err , o ) => {
344
+ if ( ! err ) {
345
+ all [ id ] = o ;
346
+ }
347
+ return cb ( err ) ;
348
+ } )
349
+ ) ( id ) ) ;
350
+ }
351
+ return results ;
352
+ } ) ( ) ;
353
+
354
+ async . parallel ( loaders , ( err ) => cb ( err , all ) ) ;
355
+
356
+ } ;
357
+ } ) ( this ) ) ;
358
+ }
371
359
372
360
allSync ( ) {
373
- var db , f , i , item , len , objects , ref ;
374
- if ( this . _memory ) {
375
- return this . _cache ;
376
- }
361
+
362
+ if ( this . _memory ) return this . _cache ;
363
+
377
364
if ( this . _single ) {
378
- db = getObjectFromFileSync . apply ( this ) ;
365
+ const db = getObjectFromFileSync . apply ( this ) ;
379
366
if ( typeof db !== "object" ) {
380
367
throw new Error ( "could not load database" ) ;
381
368
}
382
369
return db ;
383
- } else {
384
- objects = { } ;
385
- ref = readIDsSync ( this . _dir ) ;
386
- for ( i = 0 , len = ref . length ; i < len ; i ++ ) {
387
- f = ref [ i ] ;
388
- item = getObjectFromFileSync . call ( this , f ) ;
389
- if ( item != null ) {
390
- objects [ f ] = item ;
391
- } else {
392
- console . error ( "could not load '" + f + "'" ) ;
393
- }
394
- }
395
- return objects ;
396
370
}
371
+
372
+ let objects = { } ;
373
+ readIDsSync ( this . _dir ) . forEach ( ( f ) => {
374
+ const item = getObjectFromFileSync . call ( this , f ) ;
375
+ if ( item != null ) {
376
+ objects [ f ] = item ;
377
+ } else {
378
+ console . error ( "could not load '" + f + "'" ) ;
379
+ }
380
+ } ) ;
381
+
382
+ return objects ;
397
383
}
398
384
399
385
}
0 commit comments