mongo-realtime 1.0.0 → 1.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 (3) hide show
  1. package/README.md +33 -0
  2. package/index.js +49 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -95,6 +95,37 @@ The package automatically emits six types of events for each database change:
95
95
  | `db:change:{collection}:{id}` | Specific document | `db:change:users:507f1f77bcf86cd799439011` |
96
96
  | `db:{type}:{collection}:{id}` | Type + document | `db:insert:users:507f1f77bcf86cd799439011` |
97
97
 
98
+ ### Event listeners
99
+
100
+ You can add serverside listeners to those db events to trigger specific actions on the server:
101
+
102
+ ```js
103
+ function sendNotification(change){
104
+ const userId = change.docId; // or change.documentKey._id
105
+ NotificationService.send(userId,"Welcome to DB");
106
+ }
107
+
108
+ MongoRealtime.listen("db:insert:users",sendNotification);
109
+ ```
110
+
111
+ #### Adding many callback to one event
112
+
113
+ ```js
114
+ MongoRealtime.listen("db:insert:users",anotherAction);
115
+ MongoRealtime.listen("db:insert:users",anotherAction2);
116
+ ```
117
+
118
+ #### Removing event listeners
119
+
120
+ ```js
121
+ MongoRealtime.removeListener("db:insert:users",sendNotification); // remove this specific action from this event
122
+
123
+ MongoRealtime.removeListener("db:insert:users"); // remove all actions from this event
124
+
125
+ MongoRealtime.removeAllListeners(); // remove all listeners
126
+
127
+ ```
128
+
98
129
  ### Event Payload Structure
99
130
 
100
131
  Each event contains the full MongoDB change object:
@@ -102,6 +133,8 @@ Each event contains the full MongoDB change object:
102
133
  ```javascript
103
134
  {
104
135
  "_id": {...},
136
+ "col":"users", // same as ns.coll
137
+ "docId":"...", // same as documentKey._id
105
138
  "operationType": "insert|update|delete|replace",
106
139
  "documentKey": { "_id": "..." },
107
140
  "ns": { "db": "mydb", "coll": "users" },
package/index.js CHANGED
@@ -4,6 +4,8 @@ class MongoRealtime {
4
4
  /** @type {import("socket.io").Server} */ static io;
5
5
  /** @type {import("mongoose").Connection} */ static connection;
6
6
  /** @type {[import("socket.io").Socket]} */ static sockets = [];
7
+ /** @type {Record<String, [(change:ChangeStreamDocument)=>void]>} */ static #listeners =
8
+ {};
7
9
 
8
10
  /**
9
11
  * Initializes the socket system.
@@ -71,6 +73,8 @@ class MongoRealtime {
71
73
 
72
74
  changeStream.on("change", (change) => {
73
75
  const colName = change.ns.coll.toLowerCase();
76
+ change.col = colName;
77
+
74
78
  const type = change.operationType;
75
79
  const id = change.documentKey?._id;
76
80
 
@@ -87,16 +91,61 @@ class MongoRealtime {
87
91
  ];
88
92
 
89
93
  if (id) {
94
+ change.docId = id;
90
95
  const e_change_doc = `${e_change_col}:${id}`;
91
96
  const e_change_type_doc = `${e_change_type_col}:${id}`;
92
97
  events.push(e_change_doc, e_change_type_doc);
93
98
  }
94
99
  for (let e of events) {
95
100
  this.io.emit(e, change);
101
+ this.notifyListeners(e, change);
96
102
  }
97
103
  });
98
104
  });
99
105
  }
106
+
107
+ /**
108
+ * Notify all event listeners
109
+ *
110
+ * @param {String} e - Name of the event
111
+ * @param {ChangeStreamDocument} change - Change Stream
112
+ */
113
+ static notifyListeners(e, change) {
114
+ if (this.#listeners[e]) {
115
+ for (let c of this.#listeners[e]) {
116
+ c(change);
117
+ }
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Subscribe to an event
123
+ *
124
+ * @param {String} key - Name of the event
125
+ * @param {(change:ChangeStreamDocument)=>void} cb - Callback
126
+ */
127
+ static listen(key, cb) {
128
+ if (!this.#listeners[key]) this.#listeners[key] = [];
129
+ this.#listeners[key].push(cb);
130
+ }
131
+
132
+ /**
133
+ * Remove one or all listeners of an event
134
+ *
135
+ * @param {String} key - Name of the event
136
+ * @param {(change:ChangeStreamDocument)=>void} cb - Callback
137
+ */
138
+ static removeListener(key, cb) {
139
+ if (cb) this.#listeners[key] = this.#listeners[key].filter((c) => c != cb);
140
+ else this.#listeners[key] = [];
141
+ }
142
+
143
+ /**
144
+ * Unsubscribe to all events
145
+ */
146
+ static removeAllListeners() {
147
+ this.#listeners = {};
148
+ }
100
149
  }
101
150
 
102
151
  module.exports = MongoRealtime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-realtime",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {},
6
6
  "keywords": [