mongoplusplus 1.0.3 → 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.
Files changed (2) hide show
  1. package/mongoplus.js +71 -80
  2. 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
- 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
-
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
- 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
-
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,42 +211,39 @@ class MongoModel {
211
211
 
212
212
  async find(dbIndex, filter, chain = {}) {
213
213
  var currentModel = this.model[dbIndex]
214
-
215
- if (chain.skip && chain.limit && chain.sort) {
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
- else if (chain.limit) {
225
- return currentModel.find(filter).limit(chain.limit)
226
- } else {
227
- return currentModel.find(filter);
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
231
  const currentModel = this.model[dbIndex];
235
-
232
+
236
233
  // Start with the base query
237
234
  let query = currentModel.findById(filter);
238
-
235
+
239
236
  // Dynamically apply chain options if they exist
240
237
  for (const [key, value] of Object.entries(chain)) {
241
238
  if (query[key]) {
242
239
  query = query[key](value);
243
240
  }
244
241
  }
245
-
242
+
246
243
  return query;
247
244
  }
248
245
 
249
-
246
+
250
247
  //====================
251
248
  async findByIdAndUpdate(dbIndex, id, update) {
252
249
  var currentModel = this.model[dbIndex]
@@ -271,7 +268,7 @@ class MongoModel {
271
268
  async watch(dbIndex) {
272
269
  return this.model[dbIndex].watch()
273
270
  }
274
- //================
271
+ //================
275
272
 
276
273
 
277
274
 
@@ -291,25 +288,19 @@ class MongoModel {
291
288
  const results = await Promise.all(
292
289
  computationPairs.map(async pair => {
293
290
  var chain = pair.chain;
294
- if (chain.skip && chain.limit && chain.sort) {
295
- pair.fn(...pair.params).skip(chain.skip).limit(chain.limit).sort(chain.sort)
296
- } else if (chain.skip && chain.limit) {
297
- return pair.fn(...pair.params).skip(chain.skip).limit(chain.limit)
298
- }
299
- else if (chain.skip) {
300
- return pair.fn(...pair.params).skip(chain.skip)
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
+ }
301
299
  }
302
300
 
303
- else if (chain.limit) {
304
- return pair.fn(...pair.params).limit(chain.limit)
305
- } else {
306
- return pair.fn(...pair.params);
307
- }
308
- })
309
-
310
-
311
-
301
+ return query;
312
302
 
303
+ })
313
304
  );
314
305
 
315
306
  const endTime = performance.now();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongoplusplus",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "load balancing of read and write operations across multiple MongoDB servers ",
5
5
  "main": "mongoplus.js",
6
6
  "scripts": {