@jcbuisson/express-x 1.5.32 → 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 +1 -1
- package/src/server.mjs +33 -52
package/package.json
CHANGED
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)
|
|
@@ -346,8 +371,6 @@ export function expressX(prisma, options = {}) {
|
|
|
346
371
|
uid,
|
|
347
372
|
result,
|
|
348
373
|
})
|
|
349
|
-
// pub/sub: send event on associated channels
|
|
350
|
-
publish(service, action, result)
|
|
351
374
|
} catch(err) {
|
|
352
375
|
app.log('error', err.toString())
|
|
353
376
|
io.emit('client-response', {
|
|
@@ -376,42 +399,8 @@ export function expressX(prisma, options = {}) {
|
|
|
376
399
|
})
|
|
377
400
|
})
|
|
378
401
|
}
|
|
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
402
|
|
|
414
|
-
async function
|
|
403
|
+
async function joinChannel(channelName, connection) {
|
|
415
404
|
const channelNames = JSON.parse(connection.channelNames)
|
|
416
405
|
if (!channelNames.includes(channelName)) channelNames.push(channelName)
|
|
417
406
|
await app.prisma.Connection.update({
|
|
@@ -419,22 +408,14 @@ export function expressX(prisma, options = {}) {
|
|
|
419
408
|
data: { channelNames: JSON.stringify(channelNames) },
|
|
420
409
|
})
|
|
421
410
|
}
|
|
422
|
-
|
|
423
|
-
async function
|
|
411
|
+
|
|
412
|
+
async function leaveChannel(channelName, connection) {
|
|
424
413
|
const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
|
|
425
414
|
await app.prisma.Connection.update({
|
|
426
415
|
where: { id },
|
|
427
416
|
data: { channelNames: JSON.stringify(channelNames) },
|
|
428
417
|
})
|
|
429
418
|
}
|
|
430
|
-
|
|
431
|
-
function joinChannel(channelName, connection) {
|
|
432
|
-
addChannelToConnection(connection, channelName)
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
function leaveChannel(channelName, connection) {
|
|
436
|
-
removeChannelFromConnection(connection, channelName)
|
|
437
|
-
}
|
|
438
419
|
|
|
439
420
|
|
|
440
421
|
// enhance `app` with objects and methods
|