-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-crud.js
147 lines (132 loc) · 4.5 KB
/
json-crud.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
'use strict';
var fs = require('fs');
var path = require('path');
var skemer = require('skemer');
var Promise = require('promise');
var merge = require('merge');
/*require('promise/lib/rejection-tracking').enable(
{allRejections: true}
);*/
var JsonFileDB = require('./lib/JsonFileDB.js');
var JsonFolderDB = require('./lib/JsonFolderDB.js');
var optionsSchema = require('./lib/options.json');
/**
* Generator function for creating a JSON DB. The database is equivalent to a
* a single table or collection. The generator returns a promise that will
* resolve to the JSON databaes if everything is ok.
*
* @param {String} [file] Path to file/folder to contain the JSON database
* @param {Object} [options] Object containing the options for the database.
* @param {String} [options.path] Path to file/folder to contain JSON database
* if not given as first argument
* @param {String} [options.id] Field of data objects to be used as the key
* @param {Boolean} [options.cacheKeys] Cache the keys of the objects in the
* database
* @param {Boolean} [options.cacheValues] Cache the objects in the database
*
* @returns {Promise<JsonDBInstance>} A promise that will resolve to a JsonDB
* instance if everything checks out
*/
function jsonDB() {
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
var file, options, schema;
if (args.length === 0) {
return reject(new Error('No path or options given'));
}
// Check if we have a file
if (typeof args[0] === 'string' || args[0] === false) {
file = args.shift();
schema = optionsSchema;
} else {
// Make file required
schema = merge(true, optionsSchema);
schema.type.path.required = true;
}
// Check for options
try {
options = skemer.validateNew({
schema: schema,
parameter: 'options'
}, args[0] || {});
} catch (err) {
return reject(err);
}
if (typeof file === 'undefined') {
file = options.path;
}
if (file === false) {
JsonFileDB(false, options).then(function(instance) {
resolve(instance);
});
} else {
// Absolutise the file name with the CWD
file = path.resolve(process.cwd(), file);
// Check file/folder
fs.access(file, fs.R_OK | fs.W_OK, function(err) {
if (err) {
switch (err.code) {
case 'ENOENT': // File does not exist
// Determine if should be a folder based on if it ends with .json
if (file.endsWith('.json')) {
// Try creating the file
fs.writeFile(file, '{}', function(werr) {
if (werr) {
werr.message = 'Error trying to make file ' + file + ': '
+ werr.message,
reject(werr);
}
JsonFileDB(file, options).then(function(instance) {
resolve(instance);
});
});
} else {
// Try creating the folder
fs.mkdir(file, function(merr) {
if (merr) {
merr.message = 'Error trying to make folder ' + file + ': '
+ merr.message,
reject(merr);
}
JsonFolderDB(file, options).then(function(instance) {
resolve(instance);
});
});
}
break;
// @TODO Add permission error handling
default:
// Unknown error?
//err.message = 'Error trying to access database ' + file + ': '
// + err.message;
reject(err);
break;
}
} else {
// Create stat so can find out what file is
fs.stat(file, function(serr, stats) {
if (serr) {
reject(serr);
}
try {
if (stats.isDirectory()) {
// Return JsonFolderDB
JsonFolderDB(file, options).then(function(instance) {
resolve(instance);
});
} else if (stats.isFile()) {
// Return JsonFileDB
JsonFileDB(file, options).then(function(instance) {
resolve(instance);
});
}
} catch(ferr) {
reject(ferr);
}
});
}
});
}
});
}
module.exports = jsonDB;