mango-cms 0.2.27 → 0.2.30

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.
@@ -0,0 +1,11 @@
1
+ const subscribe = ({ io, collection, document, request, individual }) => {
2
+ let method = request.method
3
+ if (collection.subscribe && method != 'read' && individual) {
4
+ let roomId = request.member?.id
5
+ console.log('attempting to emit to', collection.name, roomId, `${collection.name}:${method}d`)
6
+ const subscription = io.of(collection.name);
7
+ subscription.to(roomId).emit(`${collection.name}:${method}d`, document);
8
+ }
9
+ }
10
+
11
+ export { subscribe }
@@ -25,7 +25,7 @@
25
25
  "dayjs": "^1.10.7",
26
26
  "express": "^4.18.1",
27
27
  "google-maps": "^4.3.3",
28
- "mango-cms": "^0.2.27",
28
+ "mango-cms": "^0.2.30",
29
29
  "mapbox-gl": "^2.7.0",
30
30
  "sweetalert2": "^11.4.0",
31
31
  "vite": "^6.2.2",
@@ -226,13 +226,49 @@ const Mango = collections.reduce((a, c) => {
226
226
 
227
227
  }
228
228
 
229
- let subscribe = (room, message, callback) => {
230
- message = message || c.name
229
+ let subscribe = ({target, triggers, room} = {}) => {
230
+
231
231
  let socket = io(`${api}/${c.name}`, { transports: ['websocket'] })
232
- let userId = window.localStorage.getItem('auth').split(':')[1]
232
+ let userId = window.localStorage.getItem('token').split(':')[1]
233
+
233
234
  room = room || userId
234
235
  socket.emit('subscribeToThread', room)
235
- if (callback) socket.on(message, callback)
236
+
237
+ triggers = triggers || {}
238
+ let defaultTriggers = {
239
+ created: (data) => {
240
+ if (Array.isArray(target)) target.push(data)
241
+ else Object.assign(target, data)
242
+ },
243
+ updated: (data) => {
244
+ if (Array.isArray(target)) {
245
+ const index = target.findIndex(t => t.id == data.id)
246
+ if (index !== -1) {
247
+ Object.assign(target[index], data)
248
+ }
249
+ }
250
+ else if (target.id == data.id) Object.assign(target, data)
251
+ },
252
+ deleted: (data) => {
253
+ if (Array.isArray(target)) {
254
+ const index = target.findIndex(t => t.id == data.id)
255
+ if (index !== -1) {
256
+ target.splice(index, 1) // Mutates the existing array
257
+ }
258
+ }
259
+ else if (target.id == data.id) {
260
+ // Clear the object properties while maintaining reactivity
261
+ Object.keys(target).forEach(key => delete target[key])
262
+ }
263
+ }
264
+ }
265
+
266
+ let combinedTriggers = { ...defaultTriggers, ...triggers }
267
+
268
+ for (let trigger of Object.keys(combinedTriggers)) {
269
+ socket.on(`${c.name}:${trigger}`, combinedTriggers[trigger])
270
+ }
271
+
236
272
  }
237
273
 
238
274
  a[c.name] = runQuery
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mango-cms",
3
- "version": "0.2.27",
3
+ "version": "0.2.30",
4
4
  "main": "./index.js",
5
5
  "exports": {
6
6
  ".": "./index.js",