mongoplusplus 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mongoplus.js +77 -111
- package/package.json +1 -1
package/mongoplus.js
CHANGED
|
@@ -5,18 +5,18 @@ class Mongoplus {
|
|
|
5
5
|
this.mongoURI = mongoURI;
|
|
6
6
|
this.allConnections = [];
|
|
7
7
|
this.currentIndex = 0;
|
|
8
|
-
if(this.mongoURI.filter((uri)=>uri.startsWith("readonly")).length==this.mongoURI.length){
|
|
8
|
+
if (this.mongoURI.filter((uri) => uri.startsWith("readonly")).length == this.mongoURI.length) {
|
|
9
9
|
throw new Error('Some of your URIs must be writable. If it is a mistake remove the `readonly:` flag from your urls')
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
}
|
|
13
|
-
static readonlydbs=[]
|
|
14
|
-
static readonlymodels=[] // Define currentIndex to keep track of the current URI
|
|
13
|
+
static readonlydbs = []
|
|
14
|
+
static readonlymodels = [] // Define currentIndex to keep track of the current URI
|
|
15
15
|
Schema(schema) {
|
|
16
16
|
return mongoose.Schema(schema)
|
|
17
17
|
}
|
|
18
|
-
addIndex(schema,indextype){
|
|
19
|
-
return schema.index(indextype)
|
|
18
|
+
addIndex(schema, indextype) {
|
|
19
|
+
return schema.index(indextype)
|
|
20
20
|
}
|
|
21
21
|
getNextMongoURI() {
|
|
22
22
|
const uri = this.mongoURI[this.currentIndex];
|
|
@@ -26,16 +26,16 @@ return schema.index(indextype)
|
|
|
26
26
|
|
|
27
27
|
connectToAll() {
|
|
28
28
|
for (let i = 0; i < this.mongoURI.length; i++) {
|
|
29
|
-
|
|
30
|
-
const uri = this.mongoURI[i].replaceAll("readonly:",'');
|
|
29
|
+
|
|
30
|
+
const uri = this.mongoURI[i].replaceAll("readonly:", '');
|
|
31
31
|
const con = mongoose.createConnection(uri, {
|
|
32
32
|
useNewUrlParser: true,
|
|
33
33
|
useUnifiedTopology: true,
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
this.allConnections.push(con);
|
|
37
|
-
if(this.mongoURI[i].startsWith('readonly:')){
|
|
38
|
-
|
|
37
|
+
if (this.mongoURI[i].startsWith('readonly:')) {
|
|
38
|
+
|
|
39
39
|
Mongoplus.readonlydbs.push(con)
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -50,7 +50,7 @@ return schema.index(indextype)
|
|
|
50
50
|
required: true
|
|
51
51
|
} `)
|
|
52
52
|
}
|
|
53
|
-
if(this.allConnections.length<=0){
|
|
53
|
+
if (this.allConnections.length <= 0) {
|
|
54
54
|
throw new Error(`[!]Error : All connections should be made first use the code
|
|
55
55
|
(async () => {await mongodb.connectToAll();})(); to init connections here mongodb is the class init variable`)
|
|
56
56
|
}
|
|
@@ -59,28 +59,28 @@ return schema.index(indextype)
|
|
|
59
59
|
//console.groupCollapsed("====>",Mongoplus.readonlydbs);
|
|
60
60
|
for (let i = 0; i < allConnections.length; i++) {
|
|
61
61
|
const mongooseConnection = allConnections[i];
|
|
62
|
-
var currentm=mongooseConnection.model(name, schema)
|
|
62
|
+
var currentm = mongooseConnection.model(name, schema)
|
|
63
63
|
model.push(currentm);
|
|
64
64
|
//console.count(Mongoplus.readonlydbs[i]);
|
|
65
|
-
if(Mongoplus.readonlydbs.includes(allConnections[i])){
|
|
66
|
-
|
|
65
|
+
if (Mongoplus.readonlydbs.includes(allConnections[i])) {
|
|
66
|
+
|
|
67
67
|
Mongoplus.readonlymodels.push(currentm)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
console.log("REadonly ",Mongoplus.readonlymodels)
|
|
71
|
-
return new MongoModel(model,schema,Mongoplus.readonlymodels);
|
|
70
|
+
console.log("REadonly ", Mongoplus.readonlymodels)
|
|
71
|
+
return new MongoModel(model, schema, Mongoplus.readonlymodels);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
class MongoModel {
|
|
76
|
-
constructor(model,s,readonlydbs) {
|
|
76
|
+
constructor(model, s, readonlydbs) {
|
|
77
77
|
if (!Array.isArray(model)) {
|
|
78
78
|
throw new Error('Model should be an array');
|
|
79
79
|
}
|
|
80
80
|
this.model = model;
|
|
81
|
-
this.readonlydbs=readonlydbs
|
|
82
|
-
this.s=s
|
|
83
|
-
|
|
81
|
+
this.readonlydbs = readonlydbs
|
|
82
|
+
this.s = s
|
|
83
|
+
|
|
84
84
|
|
|
85
85
|
}
|
|
86
86
|
static currentIndex = 0
|
|
@@ -104,24 +104,24 @@ class MongoModel {
|
|
|
104
104
|
async writeInAllDatabase(data) {
|
|
105
105
|
data["dbIndex"] = -1
|
|
106
106
|
const dynamicComputationPromises = [];
|
|
107
|
-
modellist=this.model
|
|
107
|
+
modellist = this.model
|
|
108
108
|
//this.readonlydbs.forEach((i)=>{modellist.splice(i,1,null)})
|
|
109
|
-
|
|
109
|
+
|
|
110
110
|
for (let i = 0; i < this.model.length; i++) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
if (Mongoplus.readonlymodels.includes(this.model[i])) continue;
|
|
112
|
+
var x = new this.model[i](data)
|
|
113
|
+
|
|
114
|
+
dynamicComputationPromises.push(await x.save());
|
|
115
|
+
|
|
116
|
+
|
|
117
117
|
}
|
|
118
|
-
|
|
118
|
+
|
|
119
119
|
return [].concat(dynamicComputationPromises);
|
|
120
120
|
|
|
121
121
|
}
|
|
122
122
|
//==================
|
|
123
123
|
async UpdateOneInAllDatabase(filter, update) {
|
|
124
|
-
|
|
124
|
+
|
|
125
125
|
const dynamicComputationPromises = [];
|
|
126
126
|
this.model.forEach((modelRef) => {
|
|
127
127
|
|
|
@@ -132,7 +132,7 @@ class MongoModel {
|
|
|
132
132
|
}
|
|
133
133
|
//==================
|
|
134
134
|
async UpdateByIdInAllDatabase(id, update) {
|
|
135
|
-
|
|
135
|
+
|
|
136
136
|
const dynamicComputationPromises = [];
|
|
137
137
|
this.model.forEach((modelRef) => {
|
|
138
138
|
|
|
@@ -142,7 +142,7 @@ class MongoModel {
|
|
|
142
142
|
|
|
143
143
|
}
|
|
144
144
|
async findByIdInAllDatabaseAndDelete(id) {
|
|
145
|
-
|
|
145
|
+
|
|
146
146
|
const dynamicComputationPromises = [];
|
|
147
147
|
this.model.forEach((modelRef) => {
|
|
148
148
|
|
|
@@ -152,7 +152,7 @@ class MongoModel {
|
|
|
152
152
|
|
|
153
153
|
}
|
|
154
154
|
async findOneInAllDatabaseAndDelete(filter) {
|
|
155
|
-
|
|
155
|
+
|
|
156
156
|
const dynamicComputationPromises = [];
|
|
157
157
|
this.model.forEach((modelRef) => {
|
|
158
158
|
|
|
@@ -163,18 +163,18 @@ class MongoModel {
|
|
|
163
163
|
}
|
|
164
164
|
//=======================
|
|
165
165
|
async write(data) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
const currentModel = this.model[MongoModel.currentIndex];
|
|
169
|
+
data["dbIndex"] = MongoModel.currentIndex;
|
|
170
|
+
MongoModel.currentIndex = (MongoModel.currentIndex + 1) % this.model.length;
|
|
171
|
+
if (Mongoplus.readonlymodels.includes(currentModel)) {
|
|
172
|
+
this.write(data)
|
|
173
|
+
//("This model is readonly");
|
|
174
|
+
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
178
|
try {
|
|
179
179
|
|
|
180
180
|
let dataToWrite = new currentModel(data)
|
|
@@ -188,7 +188,7 @@ class MongoModel {
|
|
|
188
188
|
|
|
189
189
|
async findOne(dbIndex, filter, chain = {}) {
|
|
190
190
|
var currentModel = this.model[dbIndex]
|
|
191
|
-
|
|
191
|
+
|
|
192
192
|
if (chain.skip && chain.limit && chain.sort) {
|
|
193
193
|
currentModel.findOne(filter).skip(chain.skip).limit(chain.limit).sort(chain.sort)
|
|
194
194
|
} else if (chain.skip && chain.limit) {
|
|
@@ -211,67 +211,39 @@ class MongoModel {
|
|
|
211
211
|
|
|
212
212
|
async find(dbIndex, filter, chain = {}) {
|
|
213
213
|
var currentModel = this.model[dbIndex]
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
currentModel.find(filter).skip(chain.skip).limit(chain.limit).sort(chain.sort)
|
|
217
|
-
} else if (chain.skip && chain.limit) {
|
|
218
|
-
return currentModel.find(filter).skip(chain.skip).limit(chain.limit)
|
|
219
|
-
}
|
|
220
|
-
else if (chain.skip) {
|
|
221
|
-
return currentModel.find(filter).skip(chain.skip)
|
|
222
|
-
}
|
|
214
|
+
// Start with the base query
|
|
215
|
+
let query = currentModel.find(filter);
|
|
223
216
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
217
|
+
// Dynamically apply chain options if they exist
|
|
218
|
+
for (const [key, value] of Object.entries(chain)) {
|
|
219
|
+
if (query[key]) {
|
|
220
|
+
query = query[key](value);
|
|
221
|
+
}
|
|
228
222
|
}
|
|
229
223
|
|
|
224
|
+
return query;
|
|
225
|
+
|
|
226
|
+
|
|
230
227
|
|
|
231
228
|
}
|
|
232
229
|
//=======================
|
|
233
230
|
async findById(dbIndex, filter, chain = {}) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (chain.skip && chain.limit && chain.sort) {
|
|
237
|
-
currentModel.findById(filter).skip(chain.skip).limit(chain.limit).sort(chain.sort)
|
|
238
|
-
} else if (chain.skip && chain.limit) {
|
|
239
|
-
return currentModel.findById(filter).skip(chain.skip).limit(chain.limit)
|
|
240
|
-
}
|
|
241
|
-
else if (chain.skip) {
|
|
242
|
-
return currentModel.findById(filter).skip(chain.skip)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
else if (chain.limit) {
|
|
246
|
-
return currentModel.findById(filter).limit(chain.limit)
|
|
247
|
-
} else {
|
|
248
|
-
return currentModel.findById(filter);
|
|
249
|
-
}
|
|
231
|
+
const currentModel = this.model[dbIndex];
|
|
250
232
|
|
|
233
|
+
// Start with the base query
|
|
234
|
+
let query = currentModel.findById(filter);
|
|
251
235
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (chain.skip && chain.limit && chain.sort) {
|
|
259
|
-
currentModel.findById(filter).skip(chain.skip).limit(chain.limit).sort(chain.sort)
|
|
260
|
-
} else if (chain.skip && chain.limit) {
|
|
261
|
-
return currentModel.findById(filter).skip(chain.skip).limit(chain.limit)
|
|
262
|
-
}
|
|
263
|
-
else if (chain.skip) {
|
|
264
|
-
return currentModel.findById(filter).skip(chain.skip)
|
|
236
|
+
// Dynamically apply chain options if they exist
|
|
237
|
+
for (const [key, value] of Object.entries(chain)) {
|
|
238
|
+
if (query[key]) {
|
|
239
|
+
query = query[key](value);
|
|
240
|
+
}
|
|
265
241
|
}
|
|
266
242
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
} else {
|
|
270
|
-
return currentModel.findById(filter);
|
|
271
|
-
}
|
|
243
|
+
return query;
|
|
244
|
+
}
|
|
272
245
|
|
|
273
246
|
|
|
274
|
-
}
|
|
275
247
|
//====================
|
|
276
248
|
async findByIdAndUpdate(dbIndex, id, update) {
|
|
277
249
|
var currentModel = this.model[dbIndex]
|
|
@@ -296,7 +268,7 @@ class MongoModel {
|
|
|
296
268
|
async watch(dbIndex) {
|
|
297
269
|
return this.model[dbIndex].watch()
|
|
298
270
|
}
|
|
299
|
-
//================
|
|
271
|
+
//================
|
|
300
272
|
|
|
301
273
|
|
|
302
274
|
|
|
@@ -316,25 +288,19 @@ class MongoModel {
|
|
|
316
288
|
const results = await Promise.all(
|
|
317
289
|
computationPairs.map(async pair => {
|
|
318
290
|
var chain = pair.chain;
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
291
|
+
var query = pair.fn(...pair.params);
|
|
292
|
+
// Start with the base query
|
|
293
|
+
|
|
294
|
+
// Dynamically apply chain options if they exist
|
|
295
|
+
for (const [key, value] of Object.entries(chain)) {
|
|
296
|
+
if (query[key]) {
|
|
297
|
+
query = query[key](value);
|
|
298
|
+
}
|
|
323
299
|
}
|
|
324
|
-
else if (chain.skip) {
|
|
325
|
-
return pair.fn(...pair.params).skip(chain.skip)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
else if (chain.limit) {
|
|
329
|
-
return pair.fn(...pair.params).limit(chain.limit)
|
|
330
|
-
} else {
|
|
331
|
-
return pair.fn(...pair.params);
|
|
332
|
-
}
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
|
|
336
300
|
|
|
301
|
+
return query;
|
|
337
302
|
|
|
303
|
+
})
|
|
338
304
|
);
|
|
339
305
|
|
|
340
306
|
const endTime = performance.now();
|