@jcbuisson/express-x 1.0.13 → 1.0.15
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 +14 -9
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -18,7 +18,7 @@ function expressX(app, options) {
|
|
|
18
18
|
function createDatabaseService(name, { entity=name, client='prisma' }) {
|
|
19
19
|
const prisma = app.get('prisma')
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const service = createService(name, {
|
|
22
22
|
create: (data) => {
|
|
23
23
|
if (options.debug) console.log('create', name, data)
|
|
24
24
|
return prisma[entity].create({
|
|
@@ -63,6 +63,8 @@ function expressX(app, options) {
|
|
|
63
63
|
return prisma[entity].upsert(options)
|
|
64
64
|
},
|
|
65
65
|
})
|
|
66
|
+
if (options.debug) console.log(`created service '${name}' over table '${entity}'`)
|
|
67
|
+
return service
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
/*
|
|
@@ -79,8 +81,8 @@ function expressX(app, options) {
|
|
|
79
81
|
service['__' + methodName] = async (context, ...args) => {
|
|
80
82
|
context.args = args
|
|
81
83
|
|
|
82
|
-
// if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
|
|
83
|
-
// and the client will get a rejected promise
|
|
84
|
+
// if a hook or the method throws an error, it will be caught by `socket.on('client-request'` (ws)
|
|
85
|
+
// or by express (http) and the client will get a rejected promise
|
|
84
86
|
|
|
85
87
|
// call 'before' hooks, modifying `context.args`
|
|
86
88
|
const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
|
|
@@ -139,36 +141,42 @@ function expressX(app, options) {
|
|
|
139
141
|
* add an HTTP REST endpoint at `path`, based on `service`
|
|
140
142
|
*/
|
|
141
143
|
function addHttpRestRoutes(path, service) {
|
|
142
|
-
|
|
143
144
|
const context = {
|
|
144
145
|
app,
|
|
145
|
-
|
|
146
|
+
http: { name: service.name }
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
app.post(path, async (req, res) => {
|
|
150
|
+
context.http.req = req
|
|
149
151
|
const value = await service.__create(context, req.body)
|
|
150
152
|
res.json(value)
|
|
151
153
|
})
|
|
152
154
|
|
|
153
155
|
app.get(path, async (req, res) => {
|
|
156
|
+
context.http.req = req
|
|
154
157
|
const values = await service.__find(context, req.body)
|
|
155
158
|
res.json(values)
|
|
156
159
|
})
|
|
157
160
|
|
|
158
161
|
app.get(`${path}/:id`, async (req, res) => {
|
|
162
|
+
context.http.req = req
|
|
159
163
|
const value = await service.__get(context, parseInt(req.params.id))
|
|
160
164
|
res.json(value)
|
|
161
165
|
})
|
|
162
166
|
|
|
163
167
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
168
|
+
context.http.req = req
|
|
164
169
|
const value = await service.__patch(context, parseInt(req.params.id), req.body)
|
|
165
170
|
res.json(value)
|
|
166
171
|
})
|
|
167
172
|
|
|
168
173
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
174
|
+
context.http.req = req
|
|
169
175
|
const value = await service.__remove(context, parseInt(req.params.id))
|
|
170
176
|
res.json(value)
|
|
171
177
|
})
|
|
178
|
+
|
|
179
|
+
if (options.debug) console.log(`added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
172
180
|
}
|
|
173
181
|
|
|
174
182
|
/*
|
|
@@ -213,10 +221,7 @@ function expressX(app, options) {
|
|
|
213
221
|
if (serviceMethod) {
|
|
214
222
|
const context = {
|
|
215
223
|
app,
|
|
216
|
-
|
|
217
|
-
connection,
|
|
218
|
-
name,
|
|
219
|
-
action,
|
|
224
|
+
ws: { connection, name, action, args },
|
|
220
225
|
}
|
|
221
226
|
const result = await serviceMethod(context, ...args)
|
|
222
227
|
|