@jcbuisson/express-x 1.0.19 → 1.0.21
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 +41 -14
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -19,7 +19,7 @@ function expressX(app, options={}) {
|
|
|
19
19
|
/*
|
|
20
20
|
* create a service `name` based on Prisma table `entity`
|
|
21
21
|
*/
|
|
22
|
-
function createDatabaseService(name, { entity=name
|
|
22
|
+
function createDatabaseService(name, { entity=name }) {
|
|
23
23
|
let prisma = app.get('prisma')
|
|
24
24
|
if (!prisma) {
|
|
25
25
|
prisma = new PrismaClient()
|
|
@@ -71,6 +71,8 @@ function expressX(app, options={}) {
|
|
|
71
71
|
return prisma[entity].upsert(options)
|
|
72
72
|
},
|
|
73
73
|
})
|
|
74
|
+
service.entity = entity
|
|
75
|
+
|
|
74
76
|
if (options.debug) console.log(`created service '${name}' over table '${entity}'`)
|
|
75
77
|
return service
|
|
76
78
|
}
|
|
@@ -156,32 +158,57 @@ function expressX(app, options={}) {
|
|
|
156
158
|
|
|
157
159
|
app.post(path, async (req, res) => {
|
|
158
160
|
context.http.req = req
|
|
159
|
-
|
|
160
|
-
|
|
161
|
+
try {
|
|
162
|
+
const value = await service.__create(context, req.body)
|
|
163
|
+
res.json(value)
|
|
164
|
+
} catch(err) {
|
|
165
|
+
console.log('callErr', err)
|
|
166
|
+
res.status(500).send(err.toString())
|
|
167
|
+
}
|
|
161
168
|
})
|
|
162
169
|
|
|
163
170
|
app.get(path, async (req, res) => {
|
|
164
171
|
context.http.req = req
|
|
165
|
-
|
|
166
|
-
|
|
172
|
+
try {
|
|
173
|
+
const values = await service.__find(context, req.body)
|
|
174
|
+
res.json(values)
|
|
175
|
+
} catch(err) {
|
|
176
|
+
console.log('callErr', err)
|
|
177
|
+
res.status(500).send(err.toString())
|
|
178
|
+
}
|
|
167
179
|
})
|
|
168
180
|
|
|
169
181
|
app.get(`${path}/:id`, async (req, res) => {
|
|
170
182
|
context.http.req = req
|
|
171
|
-
|
|
172
|
-
|
|
183
|
+
try {
|
|
184
|
+
const value = await service.__get(context, parseInt(req.params.id))
|
|
185
|
+
res.json(value)
|
|
186
|
+
} catch(err) {
|
|
187
|
+
console.log('callErr', err)
|
|
188
|
+
res.status(500).send(err.toString())
|
|
189
|
+
}
|
|
173
190
|
})
|
|
174
191
|
|
|
175
192
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
176
193
|
context.http.req = req
|
|
177
|
-
|
|
178
|
-
|
|
194
|
+
try {
|
|
195
|
+
const value = await service.__patch(context, parseInt(req.params.id), req.body)
|
|
196
|
+
res.json(value)
|
|
197
|
+
} catch(err) {
|
|
198
|
+
console.log('callErr', err)
|
|
199
|
+
res.status(500).send(err.toString())
|
|
200
|
+
}
|
|
179
201
|
})
|
|
180
202
|
|
|
181
203
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
182
204
|
context.http.req = req
|
|
183
|
-
|
|
184
|
-
|
|
205
|
+
try {
|
|
206
|
+
const value = await service.__remove(context, parseInt(req.params.id))
|
|
207
|
+
res.json(value)
|
|
208
|
+
} catch(err) {
|
|
209
|
+
console.log('callErr', err)
|
|
210
|
+
res.status(500).send(err.toString())
|
|
211
|
+
}
|
|
185
212
|
})
|
|
186
213
|
|
|
187
214
|
if (options.debug) console.log(`added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
@@ -261,11 +288,11 @@ function expressX(app, options={}) {
|
|
|
261
288
|
}
|
|
262
289
|
}
|
|
263
290
|
}
|
|
264
|
-
} catch(
|
|
265
|
-
console.log('callErr',
|
|
291
|
+
} catch(err) {
|
|
292
|
+
console.log('callErr', err)
|
|
266
293
|
io.emit('client-response', {
|
|
267
294
|
uid,
|
|
268
|
-
error:
|
|
295
|
+
error: err.toString(),
|
|
269
296
|
})
|
|
270
297
|
}
|
|
271
298
|
} else {
|