mongo-realtime 2.0.1 → 2.0.2

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/index.js +21 -21
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -192,14 +192,15 @@ class MongoRealtime {
192
192
  changeStream.on("change", async (change) => {
193
193
  const coll = change.ns.coll;
194
194
  const colName = coll.toLowerCase();
195
- const doc = change.fullDocument;
195
+ const id = change.documentKey?._id.toString();
196
+ const doc = change.fullDocument??{_id:id};
197
+
196
198
 
197
199
  this.#debugLog(`Collection '${colName}' changed`);
198
200
 
199
201
  change.col = colName;
200
202
 
201
203
  const type = change.operationType;
202
- const id = change.documentKey?._id.toString();
203
204
 
204
205
  for (const k in this.#streams) {
205
206
  const stream = this.#streams[k];
@@ -207,24 +208,23 @@ class MongoRealtime {
207
208
 
208
209
  Promise.resolve(stream.filter(doc)).then((ok) => {
209
210
  if (ok) {
210
- this.io.emit(`realtime:${k}`, {
211
- results: [doc],
212
- coll: coll,
213
- count: 1,
214
- total: 1,
215
- remaining: 0,
216
- });
211
+ const data = { added: [], removed: [] };
212
+ if (change.operationType == "delete") data.removed.push(doc);
213
+ else data.added.push(doc);
214
+
215
+
216
+ this.io.emit(`realtime:${k}`, data);
217
217
  }
218
218
  });
219
219
  }
220
220
  for (let k in this.#data) {
221
221
  if (!k.startsWith(`${coll}-`) || !this.#data[k].result[id]) continue;
222
- doc._id = doc._id.toString();
223
222
  switch (change.operationType) {
224
223
  case "delete":
225
224
  delete this.#data[k].result[id];
226
225
  break;
227
- default:
226
+ default:
227
+ doc._id = id;
228
228
  this.#data[k].result[id] = doc;
229
229
  }
230
230
  }
@@ -326,6 +326,7 @@ class MongoRealtime {
326
326
  .collection(coll)
327
327
  .estimatedDocumentCount();
328
328
 
329
+
329
330
  const length = ids.length;
330
331
  const range = [length, Math.min(total, length + limit)];
331
332
  const now = new Date();
@@ -390,11 +391,8 @@ class MongoRealtime {
390
391
  .map((item) => item.doc);
391
392
 
392
393
  const data = {
393
- results: filtered,
394
- coll,
395
- count: filtered.length,
396
- total,
397
- remaining: total - ids.length,
394
+ added: filtered,
395
+ removed: [],
398
396
  };
399
397
 
400
398
  socket.emit(`realtime:${streamId}:${registerId}`, data);
@@ -457,12 +455,14 @@ class MongoRealtime {
457
455
  { coll, query, limit, sortBy, project, one, skip, id, update },
458
456
  ack
459
457
  ) => {
460
-
461
458
  if (!coll || !notEmpty(update)) return ack(0);
462
459
  const c = this.connection.db.collection(coll);
463
460
 
464
- if (id) {
465
- ack((await c.updateOne({ _id: toObjectId(id) }, update)).modifiedCount);
461
+ if (id) {
462
+ ack(
463
+ (await c.updateOne({ _id: toObjectId(id) }, update))
464
+ .modifiedCount
465
+ );
466
466
  return;
467
467
  }
468
468
 
@@ -481,11 +481,11 @@ class MongoRealtime {
481
481
  };
482
482
 
483
483
  if (one) {
484
- ack((await c.updateOne(query,update, options)).modifiedCount);
484
+ ack((await c.updateOne(query, update, options)).modifiedCount);
485
485
  return;
486
486
  }
487
487
 
488
- let cursor = await c.updateMany(query,update, options);
488
+ let cursor = await c.updateMany(query, update, options);
489
489
  ack(cursor.modifiedCount);
490
490
  }
491
491
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-realtime",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {},
6
6
  "keywords": [