@jcbuisson/express-x 1.3.2 → 1.3.4
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 +27 -21
- package/prisma/dev.db +0 -0
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -18,6 +18,12 @@ export function expressX(options = {}) {
|
|
|
18
18
|
|
|
19
19
|
let lastConnectionId = 1
|
|
20
20
|
|
|
21
|
+
// logging function - a winston logger must be configured first
|
|
22
|
+
app.log = (severity, message) => {
|
|
23
|
+
const logger = app.get('logger')
|
|
24
|
+
if (logger) logger.log(severity, message)
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
/*
|
|
22
28
|
* create a service `name` based on Prisma table `entity`
|
|
23
29
|
*/
|
|
@@ -37,7 +43,7 @@ export function expressX(options = {}) {
|
|
|
37
43
|
service.prisma = prisma
|
|
38
44
|
service.entity = prismaOptions.entity
|
|
39
45
|
|
|
40
|
-
|
|
46
|
+
app.log('info', `created service '${name}' over table '${prismaOptions.entity}'`)
|
|
41
47
|
return service
|
|
42
48
|
}
|
|
43
49
|
|
|
@@ -68,7 +74,7 @@ export function expressX(options = {}) {
|
|
|
68
74
|
|
|
69
75
|
// call method
|
|
70
76
|
const result = await method(...context.args)
|
|
71
|
-
|
|
77
|
+
app.log('debug', `result ${result}`)
|
|
72
78
|
|
|
73
79
|
// call 'after' hooks
|
|
74
80
|
const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
|
|
@@ -146,20 +152,20 @@ export function expressX(options = {}) {
|
|
|
146
152
|
|
|
147
153
|
|
|
148
154
|
app.post(path, async (req, res) => {
|
|
149
|
-
|
|
155
|
+
app.log('verbose', `http request POST ${req.url}`)
|
|
150
156
|
context.http.req = req
|
|
151
157
|
try {
|
|
152
158
|
const value = await service.__create(context, { data: req.body })
|
|
153
159
|
publish(service, 'create', value)
|
|
154
160
|
res.json(value)
|
|
155
161
|
} catch(err) {
|
|
156
|
-
|
|
162
|
+
app.log('error', err)
|
|
157
163
|
res.status(500).send(err.toString())
|
|
158
164
|
}
|
|
159
165
|
})
|
|
160
166
|
|
|
161
167
|
app.get(path, async (req, res) => {
|
|
162
|
-
|
|
168
|
+
app.log('verbose', `http request GET ${req.url}`)
|
|
163
169
|
context.http.req = req
|
|
164
170
|
const query = { ...req.query }
|
|
165
171
|
try {
|
|
@@ -189,13 +195,13 @@ export function expressX(options = {}) {
|
|
|
189
195
|
publish(service, 'findMany', values)
|
|
190
196
|
res.json(values)
|
|
191
197
|
} catch(err) {
|
|
192
|
-
|
|
198
|
+
app.log('error', err)
|
|
193
199
|
res.status(500).send(err.toString())
|
|
194
200
|
}
|
|
195
201
|
})
|
|
196
202
|
|
|
197
203
|
app.get(`${path}/:id`, async (req, res) => {
|
|
198
|
-
|
|
204
|
+
app.log('verbose', `http request GET ${req.url}`)
|
|
199
205
|
context.http.req = req
|
|
200
206
|
try {
|
|
201
207
|
const value = await service.__findUnique(context, {
|
|
@@ -206,13 +212,13 @@ export function expressX(options = {}) {
|
|
|
206
212
|
publish(service, 'findUnique', value)
|
|
207
213
|
res.json(value)
|
|
208
214
|
} catch(err) {
|
|
209
|
-
|
|
215
|
+
app.log('error', err)
|
|
210
216
|
res.status(500).send(err.toString())
|
|
211
217
|
}
|
|
212
218
|
})
|
|
213
219
|
|
|
214
220
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
215
|
-
|
|
221
|
+
app.log('verbose', `http request PATCH ${req.url}`)
|
|
216
222
|
context.http.req = req
|
|
217
223
|
try {
|
|
218
224
|
const value = await service.__update(context, {
|
|
@@ -224,13 +230,13 @@ export function expressX(options = {}) {
|
|
|
224
230
|
publish(service, 'update', value)
|
|
225
231
|
res.json(value)
|
|
226
232
|
} catch(err) {
|
|
227
|
-
|
|
233
|
+
app.log('error', err)
|
|
228
234
|
res.status(500).send(err.toString())
|
|
229
235
|
}
|
|
230
236
|
})
|
|
231
237
|
|
|
232
238
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
233
|
-
|
|
239
|
+
app.log('verbose', `http request DELETE ${req.url}`)
|
|
234
240
|
context.http.req = req
|
|
235
241
|
try {
|
|
236
242
|
const value = await service.__delete(context, {
|
|
@@ -241,12 +247,12 @@ export function expressX(options = {}) {
|
|
|
241
247
|
publish(service, 'delete', value)
|
|
242
248
|
res.json(value)
|
|
243
249
|
} catch(err) {
|
|
244
|
-
|
|
250
|
+
app.log('error', err)
|
|
245
251
|
res.status(500).send(err.toString())
|
|
246
252
|
}
|
|
247
253
|
})
|
|
248
254
|
|
|
249
|
-
|
|
255
|
+
app.log('info', `added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
250
256
|
}
|
|
251
257
|
|
|
252
258
|
/*
|
|
@@ -261,7 +267,7 @@ export function expressX(options = {}) {
|
|
|
261
267
|
const io = new Server(server)
|
|
262
268
|
|
|
263
269
|
io.on('connection', function(socket) {
|
|
264
|
-
|
|
270
|
+
app.log('verbose', 'Client connected to the WebSocket')
|
|
265
271
|
const connection = {
|
|
266
272
|
id: lastConnectionId++,
|
|
267
273
|
socket,
|
|
@@ -269,7 +275,7 @@ export function expressX(options = {}) {
|
|
|
269
275
|
}
|
|
270
276
|
// store connection in cache
|
|
271
277
|
connections[connection.id] = connection
|
|
272
|
-
|
|
278
|
+
app.log('verbose', `active connections ${Object.keys(connections)}`)
|
|
273
279
|
|
|
274
280
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
275
281
|
app.emit('connection', connection)
|
|
@@ -278,7 +284,7 @@ export function expressX(options = {}) {
|
|
|
278
284
|
socket.emit('connected', connection.id)
|
|
279
285
|
|
|
280
286
|
socket.on('disconnect', () => {
|
|
281
|
-
|
|
287
|
+
app.log('verbose', `Client disconnected ${connection.id}`)
|
|
282
288
|
delete connections[connection.id]
|
|
283
289
|
})
|
|
284
290
|
|
|
@@ -288,7 +294,7 @@ export function expressX(options = {}) {
|
|
|
288
294
|
* Emit in return a 'client-response' message
|
|
289
295
|
*/
|
|
290
296
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
291
|
-
|
|
297
|
+
app.log('verbose', `client-request ${uid} ${name} ${action} ${args}`)
|
|
292
298
|
if (name in services) {
|
|
293
299
|
const service = services[name]
|
|
294
300
|
try {
|
|
@@ -308,7 +314,7 @@ export function expressX(options = {}) {
|
|
|
308
314
|
// pub/sub: send event on associated channels
|
|
309
315
|
publish(service, action, result)
|
|
310
316
|
} catch(err) {
|
|
311
|
-
|
|
317
|
+
app.log('error', err)
|
|
312
318
|
io.emit('client-response', {
|
|
313
319
|
uid,
|
|
314
320
|
error: err.toString(),
|
|
@@ -341,12 +347,12 @@ export function expressX(options = {}) {
|
|
|
341
347
|
const publishFunc = service.publishCallback
|
|
342
348
|
if (publishFunc) {
|
|
343
349
|
const channelNames = await publishFunc(result, app)
|
|
344
|
-
|
|
350
|
+
app.log('verbose', `publish channels ${service.name} ${action} ${channelNames}`)
|
|
345
351
|
for (const channelName of channelNames) {
|
|
346
|
-
|
|
352
|
+
app.log('verbose', `service-event ${service.name} ${action} ${channelName}`)
|
|
347
353
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
348
354
|
for (const connection of connectionList) {
|
|
349
|
-
|
|
355
|
+
app.log('verbose', `emit to ${connection.id} ${service.name} ${action} ${result}`)
|
|
350
356
|
connection.socket.emit('service-event', {
|
|
351
357
|
name: service.name,
|
|
352
358
|
action,
|
package/prisma/dev.db
DELETED
|
Binary file
|