@jcbuisson/express-x 1.0.2 → 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 +2 -3
- package/src/{expressX.js → index.js} +45 -45
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jcbuisson/express-x",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "src/
|
|
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(
|
|
23
|
-
return createService({
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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(
|
|
54
|
+
function createService(name, methods) {
|
|
60
55
|
const service = { name }
|
|
61
56
|
|
|
62
57
|
for (const methodName in methods) {
|
|
@@ -64,9 +59,12 @@ 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
|
|
|
65
|
+
// if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
|
|
66
|
+
// and the client will get a rejected promise
|
|
67
|
+
|
|
70
68
|
// call 'before' hooks, modifying `context.args`
|
|
71
69
|
const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
|
|
72
70
|
const beforeAllHooks = service?.hooks?.before?.all || []
|
|
@@ -86,11 +84,11 @@ function expressX(app) {
|
|
|
86
84
|
}
|
|
87
85
|
return result
|
|
88
86
|
}
|
|
89
|
-
}
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
|
94
92
|
service['_' + methodName] = method
|
|
95
93
|
}
|
|
96
94
|
|
|
@@ -109,8 +107,9 @@ function expressX(app) {
|
|
|
109
107
|
return service
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
//
|
|
110
|
+
// `app.service(name)` starts here!
|
|
113
111
|
function service(name) {
|
|
112
|
+
// get service from `services` cache
|
|
114
113
|
if (name in services) return services[name]
|
|
115
114
|
throw Error(`there is no service named '${name}'`)
|
|
116
115
|
}
|
|
@@ -130,27 +129,27 @@ function expressX(app) {
|
|
|
130
129
|
}
|
|
131
130
|
|
|
132
131
|
app.post(path, async (req, res) => {
|
|
133
|
-
const value = await service.
|
|
132
|
+
const value = await service.__create(context, req.body)
|
|
134
133
|
res.json(value)
|
|
135
134
|
})
|
|
136
135
|
|
|
137
136
|
app.get(path, async (req, res) => {
|
|
138
|
-
const values = await service.
|
|
137
|
+
const values = await service.__find(context, req.body)
|
|
139
138
|
res.json(values)
|
|
140
139
|
})
|
|
141
140
|
|
|
142
141
|
app.get(`${path}/:id`, async (req, res) => {
|
|
143
|
-
const value = await service.
|
|
142
|
+
const value = await service.__get(context, parseInt(req.params.id))
|
|
144
143
|
res.json(value)
|
|
145
144
|
})
|
|
146
145
|
|
|
147
146
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
148
|
-
const value = await service.
|
|
147
|
+
const value = await service.__patch(context, parseInt(req.params.id), req.body)
|
|
149
148
|
res.json(value)
|
|
150
149
|
})
|
|
151
150
|
|
|
152
151
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
153
|
-
const value = await service.
|
|
152
|
+
const value = await service.__remove(context, parseInt(req.params.id))
|
|
154
153
|
res.json(value)
|
|
155
154
|
})
|
|
156
155
|
}
|
|
@@ -192,12 +191,14 @@ function expressX(app) {
|
|
|
192
191
|
if (name in services) {
|
|
193
192
|
const service = services[name]
|
|
194
193
|
try {
|
|
195
|
-
const serviceMethod = service[action]
|
|
194
|
+
const serviceMethod = service['__' + action]
|
|
196
195
|
|
|
197
196
|
const context = {
|
|
198
197
|
app,
|
|
199
198
|
transport: 'ws',
|
|
200
199
|
connection,
|
|
200
|
+
name,
|
|
201
|
+
action,
|
|
201
202
|
}
|
|
202
203
|
const result = await serviceMethod(context, ...args)
|
|
203
204
|
|
|
@@ -224,7 +225,6 @@ function expressX(app) {
|
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
227
|
} catch(error) {
|
|
227
|
-
console.log('error', error)
|
|
228
228
|
io.emit('client-response', {
|
|
229
229
|
uid,
|
|
230
230
|
error,
|