@jcbuisson/express-x 1.5.32 → 1.5.34

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/package.json +1 -1
  2. package/src/server.mjs +38 -53
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.32",
3
+ "version": "1.5.34",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -9,7 +9,6 @@ import express from 'express'
9
9
  export function expressX(prisma, options = {}) {
10
10
 
11
11
  const app = express()
12
- // app.set('prisma', prisma)
13
12
 
14
13
  if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
15
14
 
@@ -108,6 +107,38 @@ export function expressX(prisma, options = {}) {
108
107
  for (const hook of [...afterMethodHooks, ...afterAllHooks]) {
109
108
  await hook(context)
110
109
  }
110
+
111
+ // publish event
112
+ const publishFunc = service.publishCallback
113
+ if (publishFunc) {
114
+ const channelNames = await publishFunc(result, app)
115
+ app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
116
+ const connections = await app.prisma.Connection.findMany({})
117
+ console.log('connections', connections)
118
+ for (const channelName of channelNames) {
119
+ app.log('verbose', `service-event ${service.name} ${methodName} ${channelName}`)
120
+ const connectionList = connections.filter(connection => {
121
+ const channelNames = JSON.parse(connection.channelNames)
122
+ return channelNames.includes(channelName)
123
+ })
124
+
125
+ for (const connection of connectionList) {
126
+ const trimmedResult = JSON.stringify(result).slice(0, 300)
127
+ app.log('verbose', `emit to ${connection.id} ${service.name} ${methodName} ${trimmedResult}`)
128
+ const socket = cnx2Socket[connection.id]
129
+ console.log()
130
+ if (!socket) {
131
+ continue // SHOULD NOT HAPPEN
132
+ }
133
+ socket.emit('service-event', {
134
+ name: service.name,
135
+ action: methodName,
136
+ result,
137
+ })
138
+ }
139
+ }
140
+ }
141
+
111
142
  return context.result
112
143
  }
113
144
 
@@ -170,7 +201,6 @@ export function expressX(prisma, options = {}) {
170
201
  context.params.req = req
171
202
  try {
172
203
  const value = await service.__create(context, { data: req.body })
173
- publish(service, 'create', value)
174
204
  res.json(value)
175
205
  } catch(err) {
176
206
  app.log('error', err)
@@ -206,7 +236,6 @@ export function expressX(prisma, options = {}) {
206
236
  const values = await service.__findMany(context, {
207
237
  where: query,
208
238
  })
209
- publish(service, 'findMany', values)
210
239
  res.json(values)
211
240
  } catch(err) {
212
241
  app.log('error', err)
@@ -223,7 +252,6 @@ export function expressX(prisma, options = {}) {
223
252
  id: parseInt(req.params.id)
224
253
  }
225
254
  })
226
- publish(service, 'findUnique', value)
227
255
  res.json(value)
228
256
  } catch(err) {
229
257
  app.log('error', err)
@@ -241,7 +269,6 @@ export function expressX(prisma, options = {}) {
241
269
  },
242
270
  data: req.body,
243
271
  })
244
- publish(service, 'update', value)
245
272
  res.json(value)
246
273
  } catch(err) {
247
274
  app.log('error', err)
@@ -258,7 +285,6 @@ export function expressX(prisma, options = {}) {
258
285
  id: parseInt(req.params.id)
259
286
  }
260
287
  })
261
- publish(service, 'delete', value)
262
288
  res.json(value)
263
289
  } catch(err) {
264
290
  app.log('error', err)
@@ -288,6 +314,7 @@ export function expressX(prisma, options = {}) {
288
314
  cnx2Socket[connection.id] = socket
289
315
 
290
316
  // emit 'connection' event for app (expressjs extends EventEmitter)
317
+ console.log('EMIT CONNECTION')
291
318
  app.emit('connection', connection)
292
319
 
293
320
  // send 'connected' event to client
@@ -346,8 +373,6 @@ export function expressX(prisma, options = {}) {
346
373
  uid,
347
374
  result,
348
375
  })
349
- // pub/sub: send event on associated channels
350
- publish(service, action, result)
351
376
  } catch(err) {
352
377
  app.log('error', err.toString())
353
378
  io.emit('client-response', {
@@ -376,42 +401,9 @@ export function expressX(prisma, options = {}) {
376
401
  })
377
402
  })
378
403
  }
379
-
380
- // publish event on associated channels
381
- async function publish(service, action, result) {
382
- const publishFunc = service.publishCallback
383
- if (publishFunc) {
384
- const channelNames = await publishFunc(result, app)
385
- app.log('verbose', `publish channels ${service.name} ${action} ${channelNames}`)
386
- for (const channelName of channelNames) {
387
- app.log('verbose', `service-event ${service.name} ${action} ${channelName}`)
388
- const connectionList = await getChannelConnections(channelName)
389
- for (const connection of connectionList) {
390
- const trimmedResult = JSON.stringify(result).slice(0, 300)
391
- app.log('verbose', `emit to ${connection.id} ${service.name} ${action} ${trimmedResult}`)
392
- const socket = cnx2Socket[connection.id]
393
- if (!socket) {
394
- continue // SHOULD NOT HAPPEN
395
- }
396
- socket.emit('service-event', {
397
- name: service.name,
398
- action,
399
- result,
400
- })
401
- }
402
- }
403
- }
404
- }
405
-
406
- async function getChannelConnections(channelName) {
407
- const connections = await app.prisma.Connection.findMany({})
408
- return connections.filter(connection => {
409
- const channelNames = JSON.parse(connection.channelNames)
410
- return channelNames.includes(channelName)
411
- })
412
- }
413
404
 
414
- async function addChannelToConnection(connection, channelName) {
405
+ async function joinChannel(channelName, connection) {
406
+ app.log('verbose', `Joining channel ${channelName} ${connection.id}`)
415
407
  const channelNames = JSON.parse(connection.channelNames)
416
408
  if (!channelNames.includes(channelName)) channelNames.push(channelName)
417
409
  await app.prisma.Connection.update({
@@ -419,22 +411,15 @@ export function expressX(prisma, options = {}) {
419
411
  data: { channelNames: JSON.stringify(channelNames) },
420
412
  })
421
413
  }
422
-
423
- async function removeChannelFromConnection(connection, channelName) {
414
+
415
+ async function leaveChannel(channelName, connection) {
416
+ app.log('verbose', `Leaving channel ${channelName} ${connection.id}`)
424
417
  const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
425
418
  await app.prisma.Connection.update({
426
419
  where: { id },
427
420
  data: { channelNames: JSON.stringify(channelNames) },
428
421
  })
429
422
  }
430
-
431
- function joinChannel(channelName, connection) {
432
- addChannelToConnection(connection, channelName)
433
- }
434
-
435
- function leaveChannel(channelName, connection) {
436
- removeChannelFromConnection(connection, channelName)
437
- }
438
423
 
439
424
 
440
425
  // enhance `app` with objects and methods