@jcbuisson/express-x 1.0.10 → 1.0.12
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 +50 -27
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -21,30 +21,50 @@ function expressX(app) {
|
|
|
21
21
|
*/
|
|
22
22
|
function createDatabaseService(name, { entity=name, client='prisma' }) {
|
|
23
23
|
return createService(name, {
|
|
24
|
-
create: (data) =>
|
|
25
|
-
data
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
24
|
+
create: (data) => {
|
|
25
|
+
if (isDebug) console.log('create', name, data)
|
|
26
|
+
return prisma[entity].create({
|
|
27
|
+
data,
|
|
28
|
+
})
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
get: (id) => {
|
|
32
|
+
if (isDebug) console.log('get', name, id)
|
|
33
|
+
return prisma[entity].findUnique({
|
|
34
|
+
where: {
|
|
35
|
+
id,
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
patch: (id, data) => {
|
|
41
|
+
if (isDebug) console.log('patch', name, id, data)
|
|
42
|
+
return prisma[entity].update({
|
|
43
|
+
where: {
|
|
44
|
+
id,
|
|
45
|
+
},
|
|
46
|
+
data,
|
|
47
|
+
})
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
remove: (id) => {
|
|
51
|
+
if (isDebug) console.log('remove', name, id)
|
|
52
|
+
return prisma[entity].delete({
|
|
53
|
+
where: {
|
|
54
|
+
id,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
find: (options) => {
|
|
60
|
+
if (isDebug) console.log('find', name, options)
|
|
61
|
+
return prisma[entity].findMany(options)
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
upsert: (options) => {
|
|
65
|
+
if (isDebug) console.log('upsert', name, options)
|
|
66
|
+
return prisma[entity].upsert(options)
|
|
67
|
+
},
|
|
48
68
|
})
|
|
49
69
|
}
|
|
50
70
|
|
|
@@ -59,7 +79,7 @@ function expressX(app) {
|
|
|
59
79
|
|
|
60
80
|
// `context` is the context of execution (transport type, connection, app)
|
|
61
81
|
// `args` is the list of arguments of the method
|
|
62
|
-
|
|
82
|
+
const hookedMethod = async (context, ...args) => {
|
|
63
83
|
context.args = args
|
|
64
84
|
|
|
65
85
|
// if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
|
|
@@ -85,8 +105,11 @@ function expressX(app) {
|
|
|
85
105
|
return result
|
|
86
106
|
}
|
|
87
107
|
|
|
108
|
+
// hooked version of method, used by client calls
|
|
109
|
+
service['__' + methodName] = hookedMethod
|
|
110
|
+
|
|
88
111
|
// hooked version of method: `create`, etc., to be called from backend with no context
|
|
89
|
-
service[methodName] =
|
|
112
|
+
service[methodName] = async (...args) => await hookedMethod({}, ...args)
|
|
90
113
|
|
|
91
114
|
// un-hooked version of method: `_create`, etc., to be called from backend with no context
|
|
92
115
|
service['_' + methodName] = method
|
|
@@ -216,7 +239,7 @@ function expressX(app) {
|
|
|
216
239
|
if (isDebug) console.log('service-event', name, action, channelName)
|
|
217
240
|
const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
218
241
|
for (const connection of connectionList) {
|
|
219
|
-
if (isDebug) console.log('emit to', connection.id)
|
|
242
|
+
if (isDebug) console.log('emit to', connection.id, name, action, result)
|
|
220
243
|
connection.socket.emit('service-event', {
|
|
221
244
|
name,
|
|
222
245
|
action,
|