@jcbuisson/express-x 1.0.13 → 1.0.14
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 +17 -2
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
|
/*
|
|
@@ -139,36 +141,48 @@ 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
|
transport: 'http',
|
|
147
|
+
name: service.name,
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
app.post(path, async (req, res) => {
|
|
151
|
+
context.action = 'create'
|
|
152
|
+
context.args = [req.body]
|
|
149
153
|
const value = await service.__create(context, req.body)
|
|
150
154
|
res.json(value)
|
|
151
155
|
})
|
|
152
156
|
|
|
153
157
|
app.get(path, async (req, res) => {
|
|
158
|
+
context.action = 'find'
|
|
159
|
+
context.args = [req.body]
|
|
154
160
|
const values = await service.__find(context, req.body)
|
|
155
161
|
res.json(values)
|
|
156
162
|
})
|
|
157
163
|
|
|
158
164
|
app.get(`${path}/:id`, async (req, res) => {
|
|
165
|
+
context.action = 'get'
|
|
166
|
+
context.args = [req.params.id]
|
|
159
167
|
const value = await service.__get(context, parseInt(req.params.id))
|
|
160
168
|
res.json(value)
|
|
161
169
|
})
|
|
162
170
|
|
|
163
171
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
172
|
+
context.action = 'patch'
|
|
173
|
+
context.args = [req.params.id, req.body]
|
|
164
174
|
const value = await service.__patch(context, parseInt(req.params.id), req.body)
|
|
165
175
|
res.json(value)
|
|
166
176
|
})
|
|
167
177
|
|
|
168
178
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
179
|
+
context.action = 'remove'
|
|
180
|
+
context.args = [req.params.id]
|
|
169
181
|
const value = await service.__remove(context, parseInt(req.params.id))
|
|
170
182
|
res.json(value)
|
|
171
183
|
})
|
|
184
|
+
|
|
185
|
+
if (options.debug) console.log(`added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
172
186
|
}
|
|
173
187
|
|
|
174
188
|
/*
|
|
@@ -217,6 +231,7 @@ function expressX(app, options) {
|
|
|
217
231
|
connection,
|
|
218
232
|
name,
|
|
219
233
|
action,
|
|
234
|
+
args,
|
|
220
235
|
}
|
|
221
236
|
const result = await serviceMethod(context, ...args)
|
|
222
237
|
|