@jcbuisson/express-x 1.5.31 → 1.5.33

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.31",
3
+ "version": "1.5.33",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -108,6 +108,36 @@ export function expressX(prisma, options = {}) {
108
108
  for (const hook of [...afterMethodHooks, ...afterAllHooks]) {
109
109
  await hook(context)
110
110
  }
111
+
112
+ // publish event
113
+ const publishFunc = service.publishCallback
114
+ if (publishFunc) {
115
+ const channelNames = await publishFunc(result, app)
116
+ app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
117
+ for (const channelName of channelNames) {
118
+ app.log('verbose', `service-event ${service.name} ${methodName} ${channelName}`)
119
+ const connections = await app.prisma.Connection.findMany({})
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
+ if (!socket) {
130
+ continue // SHOULD NOT HAPPEN
131
+ }
132
+ socket.emit('service-event', {
133
+ name: service.name,
134
+ action: methodName,
135
+ result,
136
+ })
137
+ }
138
+ }
139
+ }
140
+
111
141
  return context.result
112
142
  }
113
143
 
@@ -170,7 +200,6 @@ export function expressX(prisma, options = {}) {
170
200
  context.params.req = req
171
201
  try {
172
202
  const value = await service.__create(context, { data: req.body })
173
- publish(service, 'create', value)
174
203
  res.json(value)
175
204
  } catch(err) {
176
205
  app.log('error', err)
@@ -206,7 +235,6 @@ export function expressX(prisma, options = {}) {
206
235
  const values = await service.__findMany(context, {
207
236
  where: query,
208
237
  })
209
- publish(service, 'findMany', values)
210
238
  res.json(values)
211
239
  } catch(err) {
212
240
  app.log('error', err)
@@ -223,7 +251,6 @@ export function expressX(prisma, options = {}) {
223
251
  id: parseInt(req.params.id)
224
252
  }
225
253
  })
226
- publish(service, 'findUnique', value)
227
254
  res.json(value)
228
255
  } catch(err) {
229
256
  app.log('error', err)
@@ -241,7 +268,6 @@ export function expressX(prisma, options = {}) {
241
268
  },
242
269
  data: req.body,
243
270
  })
244
- publish(service, 'update', value)
245
271
  res.json(value)
246
272
  } catch(err) {
247
273
  app.log('error', err)
@@ -258,7 +284,6 @@ export function expressX(prisma, options = {}) {
258
284
  id: parseInt(req.params.id)
259
285
  }
260
286
  })
261
- publish(service, 'delete', value)
262
287
  res.json(value)
263
288
  } catch(err) {
264
289
  app.log('error', err)
@@ -340,13 +365,12 @@ export function expressX(prisma, options = {}) {
340
365
 
341
366
  try {
342
367
  const result = await serviceMethod(context, ...args)
343
- app.log('verbose', `client-response ${uid} ${JSON.stringify(result)}`)
368
+ const trimmedResult = JSON.stringify(result).slice(0, 300)
369
+ app.log('verbose', `client-response ${uid} ${trimmedResult}`)
344
370
  socket.emit('client-response', {
345
371
  uid,
346
372
  result,
347
373
  })
348
- // pub/sub: send event on associated channels
349
- publish(service, action, result)
350
374
  } catch(err) {
351
375
  app.log('error', err.toString())
352
376
  io.emit('client-response', {
@@ -375,41 +399,8 @@ export function expressX(prisma, options = {}) {
375
399
  })
376
400
  })
377
401
  }
378
-
379
- // publish event on associated channels
380
- async function publish(service, action, result) {
381
- const publishFunc = service.publishCallback
382
- if (publishFunc) {
383
- const channelNames = await publishFunc(result, app)
384
- app.log('verbose', `publish channels ${service.name} ${action} ${channelNames}`)
385
- for (const channelName of channelNames) {
386
- app.log('verbose', `service-event ${service.name} ${action} ${channelName}`)
387
- const connectionList = await getChannelConnections(channelName)
388
- for (const connection of connectionList) {
389
- app.log('verbose', `emit to ${connection.id} ${service.name} ${action} ${result}`)
390
- const socket = cnx2Socket[connection.id]
391
- if (!socket) {
392
- continue // SHOULD NOT HAPPEN
393
- }
394
- socket.emit('service-event', {
395
- name: service.name,
396
- action,
397
- result,
398
- })
399
- }
400
- }
401
- }
402
- }
403
-
404
- async function getChannelConnections(channelName) {
405
- const connections = await app.prisma.Connection.findMany({})
406
- return connections.filter(connection => {
407
- const channelNames = JSON.parse(connection.channelNames)
408
- return channelNames.includes(channelName)
409
- })
410
- }
411
402
 
412
- async function addChannelToConnection(connection, channelName) {
403
+ async function joinChannel(channelName, connection) {
413
404
  const channelNames = JSON.parse(connection.channelNames)
414
405
  if (!channelNames.includes(channelName)) channelNames.push(channelName)
415
406
  await app.prisma.Connection.update({
@@ -417,22 +408,14 @@ export function expressX(prisma, options = {}) {
417
408
  data: { channelNames: JSON.stringify(channelNames) },
418
409
  })
419
410
  }
420
-
421
- async function removeChannelFromConnection(connection, channelName) {
411
+
412
+ async function leaveChannel(channelName, connection) {
422
413
  const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
423
414
  await app.prisma.Connection.update({
424
415
  where: { id },
425
416
  data: { channelNames: JSON.stringify(channelNames) },
426
417
  })
427
418
  }
428
-
429
- function joinChannel(channelName, connection) {
430
- addChannelToConnection(connection, channelName)
431
- }
432
-
433
- function leaveChannel(channelName, connection) {
434
- removeChannelFromConnection(connection, channelName)
435
- }
436
419
 
437
420
 
438
421
  // enhance `app` with objects and methods
@@ -1,7 +1,6 @@
1
1
 
2
2
  import bodyParser from 'body-parser'
3
3
  import axios from 'axios'
4
- import io from 'socket.io-client'
5
4
  import { PrismaClient } from '@prisma/client'
6
5
 
7
6
 
@@ -10,13 +9,7 @@ import { strict as assert } from 'node:assert'
10
9
 
11
10
  import { expressX } from '../src/index.mjs'
12
11
 
13
- const prisma = new PrismaClient({
14
- datasources: {
15
- db: {
16
- url: "file:./dev.db",
17
- },
18
- },
19
- })
12
+ const prisma = new PrismaClient()
20
13
 
21
14
  // `app` is a regular express application, enhanced with services and real-time features
22
15
  const app = expressX(prisma)