mongo-realtime 1.0.1 → 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 (2) hide show
  1. package/README.md +33 -0
  2. 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-realtime",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {},
6
6
  "keywords": [