@jcbuisson/express-x 1.0.12 → 1.0.13
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/index.mjs +21 -31
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
|
|
2
2
|
import http from 'http'
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
|
-
import { PrismaClient } from '@prisma/client'
|
|
5
4
|
|
|
6
5
|
/*
|
|
7
6
|
* Enhance `app` express application with Feathers-like services
|
|
8
7
|
*/
|
|
9
|
-
function expressX(app) {
|
|
10
|
-
const prisma = new PrismaClient()
|
|
11
|
-
app.set('prisma', prisma) // ?? BOF
|
|
8
|
+
function expressX(app, options) {
|
|
12
9
|
|
|
13
10
|
const services = {}
|
|
14
11
|
const connections = {}
|
|
15
12
|
|
|
16
13
|
let lastConnectionId = 1
|
|
17
|
-
let isDebug = false
|
|
18
14
|
|
|
19
15
|
/*
|
|
20
16
|
* create a service `name` based on Prisma table `entity`
|
|
21
17
|
*/
|
|
22
18
|
function createDatabaseService(name, { entity=name, client='prisma' }) {
|
|
19
|
+
const prisma = app.get('prisma')
|
|
20
|
+
|
|
23
21
|
return createService(name, {
|
|
24
22
|
create: (data) => {
|
|
25
|
-
if (
|
|
23
|
+
if (options.debug) console.log('create', name, data)
|
|
26
24
|
return prisma[entity].create({
|
|
27
25
|
data,
|
|
28
26
|
})
|
|
29
27
|
},
|
|
30
28
|
|
|
31
29
|
get: (id) => {
|
|
32
|
-
if (
|
|
30
|
+
if (options.debug) console.log('get', name, id)
|
|
33
31
|
return prisma[entity].findUnique({
|
|
34
32
|
where: {
|
|
35
33
|
id,
|
|
@@ -38,7 +36,7 @@ function expressX(app) {
|
|
|
38
36
|
},
|
|
39
37
|
|
|
40
38
|
patch: (id, data) => {
|
|
41
|
-
if (
|
|
39
|
+
if (options.debug) console.log('patch', name, id, data)
|
|
42
40
|
return prisma[entity].update({
|
|
43
41
|
where: {
|
|
44
42
|
id,
|
|
@@ -48,21 +46,20 @@ function expressX(app) {
|
|
|
48
46
|
},
|
|
49
47
|
|
|
50
48
|
remove: (id) => {
|
|
51
|
-
if (
|
|
49
|
+
if (options.debug) console.log('remove', name, id)
|
|
52
50
|
return prisma[entity].delete({
|
|
53
51
|
where: {
|
|
54
52
|
id,
|
|
55
53
|
},
|
|
56
|
-
})
|
|
57
|
-
},
|
|
54
|
+
})},
|
|
58
55
|
|
|
59
56
|
find: (options) => {
|
|
60
|
-
if (
|
|
57
|
+
if (options.debug) console.log('find', name, options)
|
|
61
58
|
return prisma[entity].findMany(options)
|
|
62
59
|
},
|
|
63
60
|
|
|
64
61
|
upsert: (options) => {
|
|
65
|
-
if (
|
|
62
|
+
if (options.debug) console.log('upsert', name, options)
|
|
66
63
|
return prisma[entity].upsert(options)
|
|
67
64
|
},
|
|
68
65
|
})
|
|
@@ -79,7 +76,7 @@ function expressX(app) {
|
|
|
79
76
|
|
|
80
77
|
// `context` is the context of execution (transport type, connection, app)
|
|
81
78
|
// `args` is the list of arguments of the method
|
|
82
|
-
|
|
79
|
+
service['__' + methodName] = async (context, ...args) => {
|
|
83
80
|
context.args = args
|
|
84
81
|
|
|
85
82
|
// if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
|
|
@@ -94,7 +91,7 @@ function expressX(app) {
|
|
|
94
91
|
|
|
95
92
|
// call method
|
|
96
93
|
const result = await method(...context.args)
|
|
97
|
-
// if (
|
|
94
|
+
// if (options.debug) console.log('result', result)
|
|
98
95
|
|
|
99
96
|
// call 'after' hooks
|
|
100
97
|
const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
|
|
@@ -105,11 +102,8 @@ function expressX(app) {
|
|
|
105
102
|
return result
|
|
106
103
|
}
|
|
107
104
|
|
|
108
|
-
// hooked version of method, used by client calls
|
|
109
|
-
service['__' + methodName] = hookedMethod
|
|
110
|
-
|
|
111
105
|
// hooked version of method: `create`, etc., to be called from backend with no context
|
|
112
|
-
service[methodName] =
|
|
106
|
+
service[methodName] = method
|
|
113
107
|
|
|
114
108
|
// un-hooked version of method: `_create`, etc., to be called from backend with no context
|
|
115
109
|
service['_' + methodName] = method
|
|
@@ -184,7 +178,7 @@ function expressX(app) {
|
|
|
184
178
|
const io = new Server(server)
|
|
185
179
|
|
|
186
180
|
io.on('connection', function(socket) {
|
|
187
|
-
if (
|
|
181
|
+
if (options.debug) console.log('Client connected to the WebSocket')
|
|
188
182
|
const connection = {
|
|
189
183
|
id: lastConnectionId++,
|
|
190
184
|
socket,
|
|
@@ -192,7 +186,7 @@ function expressX(app) {
|
|
|
192
186
|
}
|
|
193
187
|
// store connection in cache
|
|
194
188
|
connections[connection.id] = connection
|
|
195
|
-
if (
|
|
189
|
+
if (options.debug) console.log('active connections', Object.keys(connections))
|
|
196
190
|
|
|
197
191
|
// emit 'connection' event for app (expressjs extends EventEmitter)
|
|
198
192
|
app.emit('connection', connection)
|
|
@@ -201,7 +195,7 @@ function expressX(app) {
|
|
|
201
195
|
socket.emit('connected', connection.id)
|
|
202
196
|
|
|
203
197
|
socket.on('disconnect', () => {
|
|
204
|
-
if (
|
|
198
|
+
if (options.debug) console.log('Client disconnected', connection.id)
|
|
205
199
|
delete connections[connection.id]
|
|
206
200
|
})
|
|
207
201
|
|
|
@@ -211,7 +205,7 @@ function expressX(app) {
|
|
|
211
205
|
* Emit in return a 'client-response' message
|
|
212
206
|
*/
|
|
213
207
|
socket.on('client-request', async ({ uid, name, action, args }) => {
|
|
214
|
-
if (
|
|
208
|
+
if (options.debug) console.log("client-request", uid, name, action, args)
|
|
215
209
|
if (name in services) {
|
|
216
210
|
const service = services[name]
|
|
217
211
|
try {
|
|
@@ -234,12 +228,12 @@ function expressX(app) {
|
|
|
234
228
|
const publishFunc = service.publishCallback
|
|
235
229
|
if (publishFunc) {
|
|
236
230
|
const channelNames = await publishFunc(result, app)
|
|
237
|
-
if (
|
|
231
|
+
if (options.debug) console.log('publish channels', name, action, channelNames)
|
|
238
232
|
for (const channelName of channelNames) {
|
|
239
|
-
if (
|
|
233
|
+
if (options.debug) console.log('service-event', name, action, channelName)
|
|
240
234
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
241
235
|
for (const connection of connectionList) {
|
|
242
|
-
if (
|
|
236
|
+
if (options.debug) console.log('emit to', connection.id, name, action, result)
|
|
243
237
|
connection.socket.emit('service-event', {
|
|
244
238
|
name,
|
|
245
239
|
action,
|
|
@@ -278,12 +272,9 @@ function expressX(app) {
|
|
|
278
272
|
connection.channelNames.delete(channelName)
|
|
279
273
|
}
|
|
280
274
|
|
|
281
|
-
function setDebug(isOn) {
|
|
282
|
-
isDebug = isOn
|
|
283
|
-
}
|
|
284
|
-
|
|
285
275
|
// enhance `app` with objects and methods
|
|
286
276
|
Object.assign(app, {
|
|
277
|
+
options,
|
|
287
278
|
createDatabaseService,
|
|
288
279
|
createService,
|
|
289
280
|
service,
|
|
@@ -292,7 +283,6 @@ function expressX(app) {
|
|
|
292
283
|
server,
|
|
293
284
|
joinChannel,
|
|
294
285
|
leaveChannel,
|
|
295
|
-
setDebug,
|
|
296
286
|
})
|
|
297
287
|
return app
|
|
298
288
|
}
|