@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +50 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
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) => prisma[entity].create({
25
- data,
26
- }),
27
-
28
- get: (id) => prisma[entity].findUnique({
29
- where: {
30
- id,
31
- },
32
- }),
33
-
34
- patch: (id, data) => prisma[entity].update({
35
- where: {
36
- id,
37
- },
38
- data,
39
- }),
40
-
41
- remove: (id, data) => prisma[entity].delete({
42
- where: {
43
- id,
44
- },
45
- }),
46
-
47
- find: (options) => prisma[entity].findMany(options),
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
- service['__' + methodName] = async (context, ...args) => {
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] = method
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,