@jcbuisson/express-x 1.0.18 → 1.0.20
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 +44 -14
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import http from 'http'
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
|
+
import { PrismaClient } from '@prisma/client'
|
|
4
5
|
|
|
5
6
|
/*
|
|
6
7
|
* Enhance `app` express application with Feathers-like services
|
|
@@ -19,7 +20,11 @@ function expressX(app, options={}) {
|
|
|
19
20
|
* create a service `name` based on Prisma table `entity`
|
|
20
21
|
*/
|
|
21
22
|
function createDatabaseService(name, { entity=name, client='prisma' }) {
|
|
22
|
-
|
|
23
|
+
let prisma = app.get('prisma')
|
|
24
|
+
if (!prisma) {
|
|
25
|
+
prisma = new PrismaClient()
|
|
26
|
+
app.set('prisma', prisma)
|
|
27
|
+
}
|
|
23
28
|
|
|
24
29
|
const service = createService(name, {
|
|
25
30
|
create: (data) => {
|
|
@@ -151,32 +156,57 @@ function expressX(app, options={}) {
|
|
|
151
156
|
|
|
152
157
|
app.post(path, async (req, res) => {
|
|
153
158
|
context.http.req = req
|
|
154
|
-
|
|
155
|
-
|
|
159
|
+
try {
|
|
160
|
+
const value = await service.__create(context, req.body)
|
|
161
|
+
res.json(value)
|
|
162
|
+
} catch(err) {
|
|
163
|
+
console.log('callErr', err)
|
|
164
|
+
res.status(500).send(err.toString())
|
|
165
|
+
}
|
|
156
166
|
})
|
|
157
167
|
|
|
158
168
|
app.get(path, async (req, res) => {
|
|
159
169
|
context.http.req = req
|
|
160
|
-
|
|
161
|
-
|
|
170
|
+
try {
|
|
171
|
+
const values = await service.__find(context, req.body)
|
|
172
|
+
res.json(values)
|
|
173
|
+
} catch(err) {
|
|
174
|
+
console.log('callErr', err)
|
|
175
|
+
res.status(500).send(err.toString())
|
|
176
|
+
}
|
|
162
177
|
})
|
|
163
178
|
|
|
164
179
|
app.get(`${path}/:id`, async (req, res) => {
|
|
165
180
|
context.http.req = req
|
|
166
|
-
|
|
167
|
-
|
|
181
|
+
try {
|
|
182
|
+
const value = await service.__get(context, parseInt(req.params.id))
|
|
183
|
+
res.json(value)
|
|
184
|
+
} catch(err) {
|
|
185
|
+
console.log('callErr', err)
|
|
186
|
+
res.status(500).send(err.toString())
|
|
187
|
+
}
|
|
168
188
|
})
|
|
169
189
|
|
|
170
190
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
171
191
|
context.http.req = req
|
|
172
|
-
|
|
173
|
-
|
|
192
|
+
try {
|
|
193
|
+
const value = await service.__patch(context, parseInt(req.params.id), req.body)
|
|
194
|
+
res.json(value)
|
|
195
|
+
} catch(err) {
|
|
196
|
+
console.log('callErr', err)
|
|
197
|
+
res.status(500).send(err.toString())
|
|
198
|
+
}
|
|
174
199
|
})
|
|
175
200
|
|
|
176
201
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
177
202
|
context.http.req = req
|
|
178
|
-
|
|
179
|
-
|
|
203
|
+
try {
|
|
204
|
+
const value = await service.__remove(context, parseInt(req.params.id))
|
|
205
|
+
res.json(value)
|
|
206
|
+
} catch(err) {
|
|
207
|
+
console.log('callErr', err)
|
|
208
|
+
res.status(500).send(err.toString())
|
|
209
|
+
}
|
|
180
210
|
})
|
|
181
211
|
|
|
182
212
|
if (options.debug) console.log(`added HTTP endpoints for service '${service.name}' at path '${path}'`)
|
|
@@ -256,11 +286,11 @@ function expressX(app, options={}) {
|
|
|
256
286
|
}
|
|
257
287
|
}
|
|
258
288
|
}
|
|
259
|
-
} catch(
|
|
260
|
-
console.log('callErr',
|
|
289
|
+
} catch(err) {
|
|
290
|
+
console.log('callErr', err)
|
|
261
291
|
io.emit('client-response', {
|
|
262
292
|
uid,
|
|
263
|
-
error:
|
|
293
|
+
error: err.toString(),
|
|
264
294
|
})
|
|
265
295
|
}
|
|
266
296
|
} else {
|