@jcbuisson/express-x 1.7.13 → 1.8.0
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 -19
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -3,6 +3,8 @@ import http from 'http'
|
|
|
3
3
|
import { Server } from 'socket.io'
|
|
4
4
|
import express from 'express'
|
|
5
5
|
|
|
6
|
+
import { getDMMF } from '@prisma/internals'
|
|
7
|
+
|
|
6
8
|
/*
|
|
7
9
|
* Enhance `app` express application with services and real-time features
|
|
8
10
|
*/
|
|
@@ -88,7 +90,7 @@ export function expressX(prisma, config) {
|
|
|
88
90
|
* create a service `name` with given `methods`
|
|
89
91
|
*/
|
|
90
92
|
function createService(name, methods) {
|
|
91
|
-
const service = { name }
|
|
93
|
+
const service = { _name: name }
|
|
92
94
|
|
|
93
95
|
for (const methodName in methods) {
|
|
94
96
|
const method = methods[methodName]
|
|
@@ -105,8 +107,8 @@ export function expressX(prisma, config) {
|
|
|
105
107
|
|
|
106
108
|
// call 'before' hooks, possibly modifying `context`
|
|
107
109
|
const beforeAppHooks = appHooks?.before || []
|
|
108
|
-
const beforeMethodHooks = service?.
|
|
109
|
-
const beforeAllHooks = service?.
|
|
110
|
+
const beforeMethodHooks = service?._hooks?.before && service._hooks.before[methodName] || []
|
|
111
|
+
const beforeAllHooks = service?._hooks?.before?.all || []
|
|
110
112
|
for (const hook of [...beforeAppHooks, ...beforeMethodHooks, ...beforeAllHooks]) {
|
|
111
113
|
await hook(context)
|
|
112
114
|
}
|
|
@@ -117,8 +119,8 @@ export function expressX(prisma, config) {
|
|
|
117
119
|
context.result = result
|
|
118
120
|
|
|
119
121
|
// call 'after' hooks, possibly modifying `context`
|
|
120
|
-
const afterMethodHooks = service?.
|
|
121
|
-
const afterAllHooks = service?.
|
|
122
|
+
const afterMethodHooks = service?._hooks?.after && service._hooks.after[methodName] || []
|
|
123
|
+
const afterAllHooks = service?._hooks?.after?.all || []
|
|
122
124
|
const afterAppHooks = appHooks?.after || []
|
|
123
125
|
for (const hook of [...afterMethodHooks, ...afterAllHooks, ...afterAppHooks]) {
|
|
124
126
|
await hook(context)
|
|
@@ -127,10 +129,10 @@ export function expressX(prisma, config) {
|
|
|
127
129
|
// publish event (websocket transport)
|
|
128
130
|
if (config.WS_TRANSPORT && service.publishFunction) {
|
|
129
131
|
const channelNames = await service.publishFunction(context)
|
|
130
|
-
app.log('verbose', `publish channels ${service.
|
|
132
|
+
app.log('verbose', `publish channels ${service._name} ${methodName} ${channelNames}`)
|
|
131
133
|
const connections = await app.prisma.Connection.findMany({})
|
|
132
134
|
for (const channelName of channelNames) {
|
|
133
|
-
app.log('verbose', `service-event ${service.
|
|
135
|
+
app.log('verbose', `service-event ${service._name} ${methodName} ${channelName}`)
|
|
134
136
|
const connectionList = connections.filter(connection => {
|
|
135
137
|
const channelNames = JSON.parse(connection.channelNames)
|
|
136
138
|
return channelNames.includes(channelName)
|
|
@@ -138,11 +140,11 @@ export function expressX(prisma, config) {
|
|
|
138
140
|
|
|
139
141
|
for (const connection of connectionList) {
|
|
140
142
|
const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
|
|
141
|
-
app.log('verbose', `emit to ${connection.id} ${service.
|
|
143
|
+
app.log('verbose', `emit to ${connection.id} ${service._name} ${methodName} ${trimmedResult}`)
|
|
142
144
|
const socket = getSocket(connection.id)
|
|
143
145
|
// emit service event
|
|
144
146
|
socket && socket.emit('service-event', {
|
|
145
|
-
name: service.
|
|
147
|
+
name: service._name,
|
|
146
148
|
action: methodName,
|
|
147
149
|
result,
|
|
148
150
|
})
|
|
@@ -153,11 +155,14 @@ export function expressX(prisma, config) {
|
|
|
153
155
|
return context.result
|
|
154
156
|
}
|
|
155
157
|
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
158
|
+
// hooked version of method to be used server-side
|
|
159
|
+
service[methodName] = (...args) => {
|
|
160
|
+
const context = {
|
|
161
|
+
caller: 'server'
|
|
162
|
+
}
|
|
163
|
+
const hookedMethod = service['__' + methodName]
|
|
164
|
+
return hookedMethod(context, ...args)
|
|
165
|
+
}
|
|
161
166
|
}
|
|
162
167
|
|
|
163
168
|
// attach pub/sub publish callback
|
|
@@ -167,7 +172,7 @@ export function expressX(prisma, config) {
|
|
|
167
172
|
|
|
168
173
|
// attach hooks
|
|
169
174
|
service.hooks = (hooks) => {
|
|
170
|
-
service.
|
|
175
|
+
service._hooks = hooks
|
|
171
176
|
}
|
|
172
177
|
|
|
173
178
|
// cache service in `services`
|
|
@@ -196,23 +201,25 @@ export function expressX(prisma, config) {
|
|
|
196
201
|
*/
|
|
197
202
|
async function addHttpRest(path, service) {
|
|
198
203
|
const context = {
|
|
204
|
+
caller: 'client',
|
|
199
205
|
app,
|
|
200
206
|
transport: 'http',
|
|
201
|
-
params: { name: service.
|
|
207
|
+
params: { name: service._name }
|
|
202
208
|
}
|
|
203
209
|
|
|
204
210
|
// introspect schema and return a map: field name => prisma type
|
|
205
211
|
function getTypesMap() {
|
|
206
212
|
// const dmmf = await service.prisma._getDmmf()
|
|
207
|
-
// const fieldDescriptions = dmmf.modelMap[service.
|
|
213
|
+
// const fieldDescriptions = dmmf.modelMap[service._name].fields
|
|
208
214
|
const dmmf = service.prisma._runtimeDataModel
|
|
209
|
-
const fieldDescriptions = dmmf.models[service.
|
|
215
|
+
const fieldDescriptions = dmmf.models[service._name].fields
|
|
210
216
|
return fieldDescriptions.reduce((accu, descr) => {
|
|
211
217
|
accu[descr.name] = descr.type
|
|
212
218
|
return accu
|
|
213
219
|
}, {})
|
|
214
220
|
}
|
|
215
221
|
|
|
222
|
+
|
|
216
223
|
app.post(path, async (req, res) => {
|
|
217
224
|
app.log('verbose', `http request POST ${req.url}`)
|
|
218
225
|
context.params.req = req
|
|
@@ -309,7 +316,7 @@ export function expressX(prisma, config) {
|
|
|
309
316
|
}
|
|
310
317
|
})
|
|
311
318
|
|
|
312
|
-
app.log('info', `added HTTP endpoints for service '${service.
|
|
319
|
+
app.log('info', `added HTTP endpoints for service '${service._name}' at path '${path}'`)
|
|
313
320
|
}
|
|
314
321
|
|
|
315
322
|
/*
|
|
@@ -382,6 +389,7 @@ export function expressX(prisma, config) {
|
|
|
382
389
|
const serviceMethod = service['__' + action]
|
|
383
390
|
if (serviceMethod) {
|
|
384
391
|
const context = {
|
|
392
|
+
caller: 'client',
|
|
385
393
|
app,
|
|
386
394
|
transport: 'ws',
|
|
387
395
|
params: { connectionId: connection.id, name, action, args },
|