-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
268 lines (248 loc) · 7.4 KB
/
Copy pathbase.js
File metadata and controls
268 lines (248 loc) · 7.4 KB
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
/* eslint class-methods-use-this:0, no-unused-vars:0, no-param-reassign: 0 */
import EventEmitter from 'events';
import BulkAPI from 'bulkapi';
/**
* An instance of [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}.
* @typedef {Promise} Promise
*/
const undoOperation = function undoOperation(undos, evnt, noId, coll, id, cont, prevData) {
if (!Object.hasOwnProperty.call(undos, coll)) {
undos[coll] = {};
}
if (!Object.hasOwnProperty.call(undos[coll], id)) {
undos[coll][id] = {
data: prevData || cont,
call: evnt,
_id: noId ? undefined : id,
};
}
};
/**
* @module BaseStore
*/
/**
* The Store class
* @class
*/
class Store extends EventEmitter {
/**
* Create an instance of Store class
* @param {Object} config - the application config
*/
constructor({ url, prefix, defaultPageSize = 10 }) {
super();
this.dburl = url;
this.defaultPageSize = defaultPageSize;
this.dbprefix = prefix || 'app_';
this.bulkOp = new BulkAPI((req, res) => {
let ar = req.body;
if (!(Array.isArray(ar))) {
ar = [ar];
}
this[req.method].call(this, ...(ar.concat([{ prevData: 1 }])))
.then((dt) => {
res.end(dt);
})
.catch((er) => {
res.statusCode = 400;
res.end(er);
});
}, undefined, undefined, function success(data) {
this.end(null, data);
}, function fail(err) {
this.end(err);
});
}
/**
* Emitting the db event
* @param {String} event - the event string
* @param {Object} content - the content of emitted document
*/
emitDbEvent(event, content) {
const evs = event.split(':');
this.emit(evs[0], ...(evs.slice(1).concat(content)));
this.emit(evs.slice(0, 2).join(':'), ...(evs.slice(2).concat(content)));
this.emit(event, content);
}
/**
* resolving the collection name with the prefix
getCollection(name) {
// to extend
}
*/
/**
* get a previous a document based on options.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the doc id
* @param {object} options - the options to list the collection
* @return {Promise} promise - return a promise
*/
async getPrevDoc(coll, _id, options) {
let prevData;
if (options && options.prevData) {
prevData = await this.read(coll, _id);
if (!prevData) throw new Error(`Record not exists with id ${_id}`);
}
return prevData;
}
/**
* connecting to db
async connectDb() {
// to extend
}
*/
/**
* generate a random id
* @return {string} text - return random string id
static GenId() {
// to extend
}
*/
/**
* Performs a set of transactions, as bulk operationo. Either all or none.
* Currently not suitable for concurrent transactions.
* @param {Object} req - the transactions
* @param {String} req.url - then collection name
* @param {String} req.method - the method to call
* @param {*} req.body - the array of parameters
* @return {Object[]} the results
*/
async bulk(req) {
const undos = {};
this.on('update', undoOperation.bind(this, undos, 'write', false));
this.on('write', undoOperation.bind(this, undos, 'del', false));
this.on('delete', undoOperation.bind(this, undos, 'write', true));
return new Promise((res, rej) => {
this.bulkOp.callbulk({
url: 'POST',
method: 'bulk',
body: req,
}, {
end: (er, dt) => {
if (er) {
const ar = [];
Object.keys(undos).forEach((cl) => {
Object.keys(undos[cl]).forEach((id) => {
ar.push(this[undos[cl][id].call](cl, undos[cl][id]._id, undos[cl][id].data));
});
});
Promise.all(ar).then().then(() => {
rej(er);
});
} else {
res(dt);
}
},
});
});
}
/**
* list all the entries
* @param {string} coll - the collection, that should be read
* @param {object} filter - the filter to apply
* @param {object} options - the options to list the collection
* @return {Promise} promise - return a promise
async list(coll, filter, options) {
// to extend
}
*/
/**
* list all the entries
* @param {string} coll - the collection, that should be read
* @param {object} filter - the filter to apply
* @param {object} options - the options to list the collection
* @return {Promise} promise - return a promise
*/
async listAll(coll, filter, options) {
const result = { total: 0 };
const firstResult = await this.list(coll, filter, Object.assign({}, options, {
defaultPageSize: undefined,
count: true,
}));
result.records = new Array(firstResult.total);
result.total = firstResult.total;
Object.assign(result.records, firstResult.records);
const restOfResults = [];
for (let point = result.records.length; point < result.total; point += this.defaultPageSize) {
restOfResults.push(this.list(coll, filter, Object.assign({}, options, {
defaultPageSize: undefined,
skip: point,
count: false,
})));
}
(await Promise.all(restOfResults)).forEach((res, ind) => {
let point = (ind + 1) * this.defaultPageSize;
res.records.forEach((rc) => {
result.records[point] = rc;
point += 1;
});
});
if (result.records.length !== result.total) {
result.warning = 'Records count were changed while fetching the records.';
}
return result;
}
/**
* find one entry
* @param {string} coll - the collection, that should be read
* @param {object} filter - the filter to apply
* @param {object} options - the options to list the collection
* @return {Promise} promise - return a promise
*/
async findOne(coll, filter, options = {}) {
return (await this.list(coll, filter, Object.assign(options, { limit: 1 }))).records.shift();
}
/**
* list all the collections.
* @return {Promise} promise - return a promise
listcolls() {
// to extend
}
*/
/**
* read a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the document id, that should be read
* @param {object} fields - the fields to retrieve
* @return {Promise} promise - return a promise
async read(coll, _id, fields) {
// to extend
}
*/
/**
* create or update a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the doc id at which, that should be created/updated
* @param {object} data - the content in to write
* @return {Promise} promise - return a promise
async write(coll, _id, data) {
// to extend
}
*/
/**
* delete a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the doc id at which, that should be deleted
* @return {Promise} promise - return a promise
async del(coll, _id) {
// to extend
}
*/
/**
* create a collection.
* @param {string} coll - the new collection, that should be created
* @return {Promise} promise - return a promise
mkcoll() { // eslint-disable-line
// do nothing
}
*/
/**
* remove a collection.
* @param {string} coll - the collection name that should be deleted
* @return {Promise} promise - return a promise
rmcoll(coll) {
// to extend
}
*/
}
export default Store;