mongo-realtime 2.0.2 → 2.0.3

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 (3) hide show
  1. package/README.md +6 -4
  2. package/index.js +33 -32
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -51,17 +51,19 @@ MongoRealtime.init({
51
51
  offSocket: (socket, reason) => {
52
52
  console.log(`Client disconnected: ${socket.id}, reason: ${reason}`);
53
53
  },
54
+ }).then(() => {
55
+ // It's better to start the server after the db connection is established
56
+ server.listen(3000, () => {
57
+ console.log("Server listening on port 3000");
58
+ });
54
59
  });
55
60
 
56
- server.listen(3000, () => {
57
- console.log("Server started on port 3000");
58
- });
59
61
  ```
60
62
 
61
63
  ## Breaking Changes since v2.x.x
62
64
 
63
65
  In older versions, streams receive all documents and send them to sockets. This can lead to performance issues with large collections.\
64
- From version 2.x.x, streams now require a limit (default 50 documents) from the client side to avoid overloading the server and network.\
66
+ From version 2.x.x, streams now require a limit (default 100 documents) from the client side to avoid overloading the server and network.\
65
67
  Receiving streamed docs now works like this:
66
68
 
67
69
  ```javascript
package/index.js CHANGED
@@ -63,7 +63,7 @@ class MongoRealtime {
63
63
 
64
64
  const expr = err ?? (match ? match[1].trim() : src);
65
65
 
66
- throw new Error(`MongoRealtime failed to check "${expr}"`);
66
+ throw new Error(`MongoRealtime expects "${expr}"`);
67
67
  }
68
68
  }
69
69
 
@@ -116,12 +116,15 @@ class MongoRealtime {
116
116
  * @param {bool} options.debug - Enable debug mode
117
117
  * @param {number} options.cacheDelay - Cache delay in minutes. Put 0 if no cache
118
118
  * @param {number} options.allowDbOperations - If true, you can use find and update operations.
119
+ * @param {mongoose} options.mongooseInstance - Running mongoose instance
120
+ *
119
121
  *
120
122
  */
121
123
  static async init({
122
124
  dbUri,
123
125
  dbOptions,
124
126
  server,
127
+ mongooseInstance,
125
128
  onDbConnect,
126
129
  onDbError,
127
130
  authentify,
@@ -193,8 +196,7 @@ class MongoRealtime {
193
196
  const coll = change.ns.coll;
194
197
  const colName = coll.toLowerCase();
195
198
  const id = change.documentKey?._id.toString();
196
- const doc = change.fullDocument??{_id:id};
197
-
199
+ const doc = change.fullDocument ?? { _id: id };
198
200
 
199
201
  this.#debugLog(`Collection '${colName}' changed`);
200
202
 
@@ -202,6 +204,29 @@ class MongoRealtime {
202
204
 
203
205
  const type = change.operationType;
204
206
 
207
+ const e_change = "db:change";
208
+ const e_change_type = `db:${type}`;
209
+ const e_change_col = `${e_change}:${colName}`;
210
+ const e_change_type_col = `${e_change_type}:${colName}`;
211
+
212
+ const events = [
213
+ e_change,
214
+ e_change_type,
215
+ e_change_col,
216
+ e_change_type_col,
217
+ ];
218
+
219
+ if (id) {
220
+ change.docId = id;
221
+ const e_change_doc = `${e_change_col}:${id}`;
222
+ const e_change_type_doc = `${e_change_type_col}:${id}`;
223
+ events.push(e_change_doc, e_change_type_doc);
224
+ }
225
+ for (let e of events) {
226
+ this.io.emit(e, change);
227
+ this.notifyListeners(e, change);
228
+ }
229
+
205
230
  for (const k in this.#streams) {
206
231
  const stream = this.#streams[k];
207
232
  if (stream.collection != coll) continue;
@@ -212,7 +237,6 @@ class MongoRealtime {
212
237
  if (change.operationType == "delete") data.removed.push(doc);
213
238
  else data.added.push(doc);
214
239
 
215
-
216
240
  this.io.emit(`realtime:${k}`, data);
217
241
  }
218
242
  });
@@ -223,40 +247,18 @@ class MongoRealtime {
223
247
  case "delete":
224
248
  delete this.#data[k].result[id];
225
249
  break;
226
- default:
250
+ default:
227
251
  doc._id = id;
228
252
  this.#data[k].result[id] = doc;
229
253
  }
230
254
  }
231
-
232
- const e_change = "db:change";
233
- const e_change_type = `db:${type}`;
234
- const e_change_col = `${e_change}:${colName}`;
235
- const e_change_type_col = `${e_change_type}:${colName}`;
236
-
237
- const events = [
238
- e_change,
239
- e_change_type,
240
- e_change_col,
241
- e_change_type_col,
242
- ];
243
-
244
- if (id) {
245
- change.docId = id;
246
- const e_change_doc = `${e_change_col}:${id}`;
247
- const e_change_type_doc = `${e_change_type_col}:${id}`;
248
- events.push(e_change_doc, e_change_type_doc);
249
- }
250
- for (let e of events) {
251
- this.io.emit(e, change);
252
- this.notifyListeners(e, change);
253
- }
254
255
  });
255
256
  });
256
257
 
257
258
  try {
258
259
  await mongoose.connect(dbUri, dbOptions);
259
260
  this.#log(`Connected to db '${mongoose.connection.name}'`, 1);
261
+ if (mongooseInstance) mongooseInstance.connection = mongoose.connection;
260
262
  onDbConnect?.call(this, mongoose.connection);
261
263
  } catch (error) {
262
264
  onDbError?.call(this, error);
@@ -303,8 +305,8 @@ class MongoRealtime {
303
305
 
304
306
  const stream = this.#streams[streamId];
305
307
  const coll = stream.collection;
306
-
307
- const default_limit = 50;
308
+
309
+ const default_limit = 100;
308
310
  limit ??= default_limit;
309
311
  try {
310
312
  limit = parseInt(limit);
@@ -315,7 +317,7 @@ class MongoRealtime {
315
317
  reverse = reverse == true;
316
318
  registerId ??= "";
317
319
  this.#debugLog(
318
- `Socket ${socket.id} registred for realtime '${coll}:${registerId}'. Limit ${limit}. Reversed ${reverse}`
320
+ `Socket '${socket.id}' registred for realtime '${coll}:${registerId}'. Limit ${limit}. Reversed ${reverse}`
319
321
  );
320
322
 
321
323
  let total;
@@ -326,7 +328,6 @@ class MongoRealtime {
326
328
  .collection(coll)
327
329
  .estimatedDocumentCount();
328
330
 
329
-
330
331
  const length = ids.length;
331
332
  const range = [length, Math.min(total, length + limit)];
332
333
  const now = new Date();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-realtime",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "main": "index.js",
5
5
  "scripts": {},
6
6
  "keywords": [