@jcbuisson/express-x 1.0.3 → 1.0.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 CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
- "main": "src/expressX.js",
5
+ "main": "src/index.js",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git@gitlab.com:buisson31/express-x.git"
@@ -17,7 +17,6 @@
17
17
  "@prisma/client": "^4.10.1",
18
18
  "config": "^3.3.9",
19
19
  "express": "^4.18.2",
20
- "jsonwebtoken": "^9.0.0",
21
20
  "socket.io": "^4.6.0"
22
21
  },
23
22
  "devDependencies": {
@@ -19,44 +19,39 @@ function expressX(app) {
19
19
  /*
20
20
  * create a service `name` based on Prisma table `entity`
21
21
  */
22
- function createDatabaseService({ name, entity=name, client='prisma' }) {
23
- return createService({
24
- name,
25
- entity,
26
-
27
- methods: {
28
- create: (data) => prisma[entity].create({
29
- data,
30
- }),
31
-
32
- get: (id) => prisma[entity].findUnique({
33
- where: {
34
- id,
35
- },
36
- }),
37
-
38
- patch: (id, data) => prisma[entity].update({
39
- where: {
40
- id,
41
- },
42
- data,
43
- }),
44
-
45
- remove: (id, data) => prisma[entity].delete({
46
- where: {
47
- id,
48
- },
49
- }),
50
-
51
- find: (options) => prisma[entity].findMany(options),
52
- }
22
+ function createDatabaseService(name, { entity=name, client='prisma' }) {
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),
53
48
  })
54
49
  }
55
50
 
56
51
  /*
57
52
  * create a service `name` with given `methods`
58
53
  */
59
- function createService({ name, methods }) {
54
+ function createService(name, methods) {
60
55
  const service = { name }
61
56
 
62
57
  for (const methodName in methods) {
@@ -64,7 +59,7 @@ function expressX(app) {
64
59
 
65
60
  // `context` is the context of execution (transport type, connection, app)
66
61
  // `args` is the list of arguments of the method
67
- service[methodName] = async (context, ...args) => {
62
+ service['__' + methodName] = async (context, ...args) => {
68
63
  context.args = args
69
64
 
70
65
  // if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
@@ -89,11 +84,11 @@ function expressX(app) {
89
84
  }
90
85
  return result
91
86
  }
92
- }
93
87
 
94
- // un-hooked versions of methods: `_create`, etc.
95
- for (const methodName in methods) {
96
- const method = methods[methodName]
88
+ // hooked version of method: `create`, etc., to be called from backend with no context
89
+ service[methodName] = method
90
+
91
+ // un-hooked version of method: `_create`, etc., to be called from backend with no context
97
92
  service['_' + methodName] = method
98
93
  }
99
94
 
@@ -112,8 +107,9 @@ function expressX(app) {
112
107
  return service
113
108
  }
114
109
 
115
- // get service from `services` cache
110
+ // `app.service(name)` starts here!
116
111
  function service(name) {
112
+ // get service from `services` cache
117
113
  if (name in services) return services[name]
118
114
  throw Error(`there is no service named '${name}'`)
119
115
  }
@@ -133,27 +129,27 @@ function expressX(app) {
133
129
  }
134
130
 
135
131
  app.post(path, async (req, res) => {
136
- const value = await service.create(context, req.body)
132
+ const value = await service.__create(context, req.body)
137
133
  res.json(value)
138
134
  })
139
135
 
140
136
  app.get(path, async (req, res) => {
141
- const values = await service.find(context, req.body)
137
+ const values = await service.__find(context, req.body)
142
138
  res.json(values)
143
139
  })
144
140
 
145
141
  app.get(`${path}/:id`, async (req, res) => {
146
- const value = await service.get(context, parseInt(req.params.id))
142
+ const value = await service.__get(context, parseInt(req.params.id))
147
143
  res.json(value)
148
144
  })
149
145
 
150
146
  app.patch(`${path}/:id`, async (req, res) => {
151
- const value = await service.patch(context, parseInt(req.params.id), req.body)
147
+ const value = await service.__patch(context, parseInt(req.params.id), req.body)
152
148
  res.json(value)
153
149
  })
154
150
 
155
151
  app.delete(`${path}/:id`, async (req, res) => {
156
- const value = await service.remove(context, parseInt(req.params.id))
152
+ const value = await service.__remove(context, parseInt(req.params.id))
157
153
  res.json(value)
158
154
  })
159
155
  }
@@ -195,12 +191,14 @@ function expressX(app) {
195
191
  if (name in services) {
196
192
  const service = services[name]
197
193
  try {
198
- const serviceMethod = service[action]
194
+ const serviceMethod = service['__' + action]
199
195
 
200
196
  const context = {
201
197
  app,
202
198
  transport: 'ws',
203
199
  connection,
200
+ name,
201
+ action,
204
202
  }
205
203
  const result = await serviceMethod(context, ...args)
206
204