@modular-rest/server 1.11.13 → 1.11.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/.nvmrc +1 -0
- package/.prettierrc.json +9 -0
- package/.releaserc.json +24 -0
- package/README.md +79 -94
- package/dist/application.d.ts +29 -0
- package/dist/application.js +217 -0
- package/dist/class/cms_trigger.d.ts +52 -0
- package/dist/class/cms_trigger.js +47 -0
- package/dist/class/collection_definition.d.ts +112 -0
- package/dist/class/collection_definition.js +87 -0
- package/dist/class/combinator.d.ts +43 -0
- package/dist/class/combinator.js +174 -0
- package/dist/class/database_trigger.d.ts +90 -0
- package/dist/class/database_trigger.js +64 -0
- package/dist/class/db_schemas.d.ts +25 -0
- package/dist/class/db_schemas.js +28 -0
- package/dist/class/directory.d.ts +20 -0
- package/dist/class/directory.js +87 -0
- package/dist/class/paginator.d.ts +31 -0
- package/dist/class/paginator.js +43 -0
- package/dist/class/reply.d.ts +29 -0
- package/dist/class/reply.js +44 -0
- package/dist/class/security.d.ts +186 -0
- package/dist/class/security.js +178 -0
- package/dist/class/trigger_operator.d.ts +92 -0
- package/dist/class/trigger_operator.js +99 -0
- package/dist/class/user.d.ts +81 -0
- package/dist/class/user.js +151 -0
- package/dist/class/validator.d.ts +19 -0
- package/dist/class/validator.js +101 -0
- package/dist/config.d.ts +113 -0
- package/dist/config.js +26 -0
- package/dist/defult-permissions.d.ts +2 -0
- package/dist/defult-permissions.js +31 -0
- package/dist/events.d.ts +23 -0
- package/dist/events.js +47 -0
- package/dist/helper/data_insertion.d.ts +38 -0
- package/dist/helper/data_insertion.js +110 -0
- package/dist/helper/presetup_services.d.ts +60 -0
- package/dist/helper/presetup_services.js +108 -0
- package/dist/index.d.ts +118 -0
- package/dist/index.js +79 -0
- package/dist/middlewares.d.ts +53 -0
- package/dist/middlewares.js +106 -0
- package/dist/play-test.d.ts +1 -0
- package/dist/play-test.js +9 -0
- package/dist/services/data_provider/router.d.ts +4 -0
- package/dist/services/data_provider/router.js +412 -0
- package/dist/services/data_provider/service.d.ts +132 -0
- package/dist/services/data_provider/service.js +253 -0
- package/dist/services/data_provider/typeCasters.d.ts +9 -0
- package/dist/services/data_provider/typeCasters.js +18 -0
- package/dist/services/file/db.d.ts +1 -0
- package/dist/services/file/db.js +31 -0
- package/dist/services/file/router.d.ts +4 -0
- package/dist/services/file/router.js +115 -0
- package/dist/services/file/service.d.ts +204 -0
- package/dist/services/file/service.js +341 -0
- package/dist/services/functions/router.d.ts +4 -0
- package/dist/services/functions/router.js +68 -0
- package/dist/services/functions/service.d.ts +132 -0
- package/dist/services/functions/service.js +159 -0
- package/dist/services/jwt/router.d.ts +4 -0
- package/dist/services/jwt/router.js +99 -0
- package/dist/services/jwt/service.d.ts +97 -0
- package/dist/services/jwt/service.js +135 -0
- package/dist/services/user_manager/db.d.ts +1 -0
- package/dist/services/user_manager/db.js +75 -0
- package/dist/services/user_manager/permissionManager.d.ts +19 -0
- package/dist/services/user_manager/permissionManager.js +42 -0
- package/dist/services/user_manager/router.d.ts +4 -0
- package/dist/services/user_manager/router.js +195 -0
- package/dist/services/user_manager/service.d.ts +317 -0
- package/dist/services/user_manager/service.js +632 -0
- package/docs/.keep +0 -0
- package/docs/system-access-type.md +26 -0
- package/package.json +59 -46
- package/src/application.ts +206 -0
- package/src/class/cms_trigger.ts +62 -0
- package/src/class/collection_definition.ts +134 -0
- package/src/class/combinator.ts +176 -0
- package/src/class/database_trigger.ts +105 -0
- package/src/class/db_schemas.ts +44 -0
- package/src/class/{directory.js → directory.ts} +40 -18
- package/src/class/paginator.ts +51 -0
- package/src/class/reply.ts +59 -0
- package/src/class/security.ts +250 -0
- package/src/class/trigger_operator.ts +142 -0
- package/src/class/user.ts +199 -0
- package/src/class/validator.ts +123 -0
- package/src/config.ts +122 -0
- package/src/defult-permissions.ts +31 -0
- package/src/events.ts +59 -0
- package/src/helper/data_insertion.ts +94 -0
- package/src/helper/presetup_services.ts +96 -0
- package/src/index.ts +146 -0
- package/src/middlewares.ts +75 -0
- package/src/play-test.ts +8 -0
- package/src/services/data_provider/router.ts +484 -0
- package/src/services/data_provider/service.ts +306 -0
- package/src/services/data_provider/typeCasters.ts +15 -0
- package/src/services/file/db.ts +29 -0
- package/src/services/file/router.ts +88 -0
- package/src/services/file/service.ts +387 -0
- package/src/services/functions/router.ts +35 -0
- package/src/services/functions/service.ts +203 -0
- package/src/services/jwt/router.ts +73 -0
- package/src/services/jwt/service.ts +139 -0
- package/src/services/user_manager/db.ts +87 -0
- package/src/services/user_manager/permissionManager.ts +49 -0
- package/src/services/user_manager/router.ts +193 -0
- package/src/services/user_manager/service.ts +703 -0
- package/tsconfig.json +16 -9
- package/typedoc.mjs +41 -0
- package/LICENSE +0 -21
- package/package-lock.json +0 -1373
- package/src/application.js +0 -239
- package/src/class/cms_trigger.js +0 -20
- package/src/class/collection_definition.js +0 -33
- package/src/class/combinator.js +0 -133
- package/src/class/database_trigger.js +0 -20
- package/src/class/db_schemas.js +0 -18
- package/src/class/paginator.js +0 -31
- package/src/class/reply.js +0 -37
- package/src/class/security.js +0 -141
- package/src/class/trigger_operator.js +0 -39
- package/src/class/user.js +0 -112
- package/src/class/validator.js +0 -91
- package/src/config.js +0 -67
- package/src/events.js +0 -15
- package/src/helper/data_insertion.js +0 -64
- package/src/helper/presetup_services.js +0 -31
- package/src/index.js +0 -66
- package/src/middlewares.js +0 -44
- package/src/services/data_provider/router.js +0 -552
- package/src/services/data_provider/service.js +0 -262
- package/src/services/data_provider/typeCasters.js +0 -10
- package/src/services/file/db.js +0 -29
- package/src/services/file/router.js +0 -92
- package/src/services/file/service.js +0 -231
- package/src/services/functions/router.js +0 -37
- package/src/services/functions/service.js +0 -74
- package/src/services/jwt/router.js +0 -82
- package/src/services/jwt/service.js +0 -37
- package/src/services/user_manager/db.js +0 -83
- package/src/services/user_manager/permissionManager.js +0 -43
- package/src/services/user_manager/router.js +0 -176
- package/src/services/user_manager/service.js +0 -377
- package/types/application.d.ts +0 -97
- package/types/class/cms_trigger.d.ts +0 -24
- package/types/class/collection_definition.d.ts +0 -36
- package/types/class/combinator.d.ts +0 -30
- package/types/class/database_trigger.d.ts +0 -28
- package/types/class/db_schemas.d.ts +0 -2
- package/types/class/directory.d.ts +0 -2
- package/types/class/paginator.d.ts +0 -8
- package/types/class/reply.d.ts +0 -8
- package/types/class/security.d.ts +0 -109
- package/types/class/trigger_operator.d.ts +0 -19
- package/types/class/user.d.ts +0 -24
- package/types/class/validator.d.ts +0 -9
- package/types/config.d.ts +0 -101
- package/types/events.d.ts +0 -7
- package/types/helper/data_insertion.d.ts +0 -4
- package/types/helper/presetup_services.d.ts +0 -5
- package/types/index.d.ts +0 -72
- package/types/middlewares.d.ts +0 -10
- package/types/services/data_provider/router.d.ts +0 -3
- package/types/services/data_provider/service.d.ts +0 -40
- package/types/services/data_provider/typeCasters.d.ts +0 -3
- package/types/services/file/db.d.ts +0 -3
- package/types/services/file/router.d.ts +0 -3
- package/types/services/file/service.d.ts +0 -81
- package/types/services/functions/router.d.ts +0 -3
- package/types/services/functions/service.d.ts +0 -23
- package/types/services/jwt/router.d.ts +0 -3
- package/types/services/jwt/service.d.ts +0 -10
- package/types/services/user_manager/db.d.ts +0 -3
- package/types/services/user_manager/permissionManager.d.ts +0 -3
- package/types/services/user_manager/router.d.ts +0 -3
- package/types/services/user_manager/service.d.ts +0 -131
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { AccessTypes } from '../../class/security';
|
|
2
|
+
import Router from 'koa-router';
|
|
3
|
+
import { validateObject } from '../../class/validator';
|
|
4
|
+
import { create as reply } from '../../class/reply';
|
|
5
|
+
import nestedProperty from 'nested-property';
|
|
6
|
+
import * as service from './service';
|
|
7
|
+
import * as middleware from '../../middlewares';
|
|
8
|
+
import { Context, Next } from 'koa';
|
|
9
|
+
import mongoose from 'mongoose';
|
|
10
|
+
|
|
11
|
+
const name = 'data-provider';
|
|
12
|
+
|
|
13
|
+
const dataProvider = new Router();
|
|
14
|
+
|
|
15
|
+
dataProvider.use('/', middleware.auth, async (ctx: Context, next: Next) => {
|
|
16
|
+
const body = ctx.request.body;
|
|
17
|
+
const bodyValidated = validateObject(body, 'database collection');
|
|
18
|
+
|
|
19
|
+
// fields validation
|
|
20
|
+
if (!bodyValidated.isValid) {
|
|
21
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidated.requires })));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// type caster
|
|
25
|
+
if (body.types && body.hasOwnProperty(body.bodyKey || '.')) {
|
|
26
|
+
const bodyKey = body.bodyKey;
|
|
27
|
+
for (const key in body.types) {
|
|
28
|
+
if (body.types.hasOwnProperty(key) && typeof body.types[key] == 'object') {
|
|
29
|
+
const typeDetail = body.types[key];
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const value = nestedProperty.get(body[bodyKey], typeDetail.path);
|
|
33
|
+
const newProperty =
|
|
34
|
+
service.TypeCasters[typeDetail.type as keyof typeof service.TypeCasters](value);
|
|
35
|
+
nestedProperty.set(body[bodyKey], typeDetail.path, newProperty);
|
|
36
|
+
console.log('newProperty', newProperty, JSON.stringify(body[bodyKey]));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.log('type caster error', e);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await next();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
dataProvider.post('/find', async (ctx: Context) => {
|
|
48
|
+
const body = ctx.request.body;
|
|
49
|
+
const bodyValidate = validateObject(body, 'database collection query');
|
|
50
|
+
|
|
51
|
+
// fields validation
|
|
52
|
+
if (!bodyValidate.isValid) {
|
|
53
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// access validation
|
|
57
|
+
const hasAccess = service.checkAccess(
|
|
58
|
+
body.database,
|
|
59
|
+
body.collection,
|
|
60
|
+
AccessTypes.read,
|
|
61
|
+
body.query,
|
|
62
|
+
ctx.state.user
|
|
63
|
+
);
|
|
64
|
+
if (!hasAccess) {
|
|
65
|
+
console.log(body);
|
|
66
|
+
console.log(ctx.state.user.permission);
|
|
67
|
+
ctx.throw(403, 'access denied');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// collection validation
|
|
71
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
72
|
+
if (collection == null) {
|
|
73
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// operate on db
|
|
77
|
+
let queryRequest = collection.find(body.query, body.projection);
|
|
78
|
+
|
|
79
|
+
if (body.options) {
|
|
80
|
+
queryRequest = service.performAdditionalOptionsToQueryObject(queryRequest, body.options);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (body.populates) {
|
|
84
|
+
try {
|
|
85
|
+
queryRequest = service.performPopulateToQueryObject(queryRequest, body.populates);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
ctx.status = 412;
|
|
88
|
+
ctx.body = err;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await queryRequest
|
|
93
|
+
.exec()
|
|
94
|
+
.then(async docs => {
|
|
95
|
+
// Call trigger
|
|
96
|
+
service.triggers.call('find', body.database, body.collection, {
|
|
97
|
+
query: body.query,
|
|
98
|
+
queryResult: docs,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
ctx.body = { data: docs };
|
|
102
|
+
})
|
|
103
|
+
.catch(err => {
|
|
104
|
+
ctx.status = err.status || 500;
|
|
105
|
+
ctx.body = err.message;
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
dataProvider.post('/find-one', async (ctx: Context) => {
|
|
110
|
+
const body = ctx.request.body;
|
|
111
|
+
const bodyValidate = validateObject(body, 'database collection query');
|
|
112
|
+
|
|
113
|
+
// fields validation
|
|
114
|
+
if (!bodyValidate.isValid) {
|
|
115
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// access validation
|
|
119
|
+
const hasAccess = service.checkAccess(
|
|
120
|
+
body.database,
|
|
121
|
+
body.collection,
|
|
122
|
+
AccessTypes.read,
|
|
123
|
+
body.query,
|
|
124
|
+
ctx.state.user
|
|
125
|
+
);
|
|
126
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
127
|
+
|
|
128
|
+
// collection validation
|
|
129
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
130
|
+
if (collection == null) {
|
|
131
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// operate on db
|
|
135
|
+
let queryRequest = collection.findOne(body.query, body.projection, body.options);
|
|
136
|
+
|
|
137
|
+
if (body.options) {
|
|
138
|
+
queryRequest = service.performAdditionalOptionsToQueryObject(queryRequest, body.options);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (body.populates) {
|
|
142
|
+
try {
|
|
143
|
+
queryRequest = service.performPopulateToQueryObject(queryRequest, body.populates);
|
|
144
|
+
} catch (err) {
|
|
145
|
+
ctx.status = 412;
|
|
146
|
+
ctx.body = err;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// operate on db
|
|
151
|
+
await queryRequest
|
|
152
|
+
.exec()
|
|
153
|
+
.then(async doc => {
|
|
154
|
+
// Call trigger
|
|
155
|
+
service.triggers.call('find-one', body.database, body.collection, {
|
|
156
|
+
query: body.query,
|
|
157
|
+
queryResult: doc,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
ctx.body = { data: doc };
|
|
161
|
+
})
|
|
162
|
+
.catch(err => {
|
|
163
|
+
ctx.status = err.status || 500;
|
|
164
|
+
ctx.body = err.message;
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
dataProvider.post('/count', async (ctx: Context) => {
|
|
169
|
+
const body = ctx.request.body;
|
|
170
|
+
const bodyValidate = validateObject(body, 'database collection query');
|
|
171
|
+
|
|
172
|
+
// fields validation
|
|
173
|
+
if (!bodyValidate.isValid) {
|
|
174
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// access validation
|
|
178
|
+
const hasAccess = service.checkAccess(
|
|
179
|
+
body.database,
|
|
180
|
+
body.collection,
|
|
181
|
+
AccessTypes.read,
|
|
182
|
+
body.query,
|
|
183
|
+
ctx.state.user
|
|
184
|
+
);
|
|
185
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
186
|
+
|
|
187
|
+
// collection validation
|
|
188
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
189
|
+
if (collection == null) {
|
|
190
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
await collection
|
|
194
|
+
.countDocuments(body.query)
|
|
195
|
+
.exec()
|
|
196
|
+
.then(count => {
|
|
197
|
+
// Call trigger
|
|
198
|
+
service.triggers.call('count', body.database, body.collection, {
|
|
199
|
+
query: body.query,
|
|
200
|
+
queryResult: count,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
ctx.body = { data: count };
|
|
204
|
+
})
|
|
205
|
+
.catch(err => {
|
|
206
|
+
ctx.status = err.status || 500;
|
|
207
|
+
ctx.body = err.message;
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
dataProvider.post('/update-one', async (ctx: Context) => {
|
|
212
|
+
const body = ctx.request.body;
|
|
213
|
+
const bodyValidate = validateObject(body, 'database collection query update');
|
|
214
|
+
|
|
215
|
+
// fields validation
|
|
216
|
+
if (!bodyValidate.isValid) {
|
|
217
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// access validation
|
|
221
|
+
const hasAccess = service.checkAccess(
|
|
222
|
+
body.database,
|
|
223
|
+
body.collection,
|
|
224
|
+
AccessTypes.write,
|
|
225
|
+
body.query,
|
|
226
|
+
ctx.state.user
|
|
227
|
+
);
|
|
228
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
229
|
+
|
|
230
|
+
// collection validation
|
|
231
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
232
|
+
if (collection == null) {
|
|
233
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// get removing doc as output for triggers
|
|
237
|
+
const output: any = await collection.findOne(body.query).exec().then();
|
|
238
|
+
|
|
239
|
+
// operate on db
|
|
240
|
+
await collection
|
|
241
|
+
.updateOne(body.query, body.update, body.options)
|
|
242
|
+
.exec()
|
|
243
|
+
.then(writeOpResult => {
|
|
244
|
+
// Call trigger
|
|
245
|
+
service.triggers.call('update-one', body.database, body.collection, {
|
|
246
|
+
query: body.query,
|
|
247
|
+
update: body.update,
|
|
248
|
+
queryResult: writeOpResult,
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
ctx.body = { data: writeOpResult };
|
|
252
|
+
})
|
|
253
|
+
.catch(err => {
|
|
254
|
+
ctx.status = err.status || 500;
|
|
255
|
+
ctx.body = err.message;
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
dataProvider.post('/insert-one', async (ctx: Context) => {
|
|
260
|
+
const body = ctx.request.body;
|
|
261
|
+
const bodyValidate = validateObject(body, 'database collection doc');
|
|
262
|
+
|
|
263
|
+
// fields validation
|
|
264
|
+
if (!bodyValidate.isValid) {
|
|
265
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// access validation
|
|
269
|
+
const hasAccess = service.checkAccess(
|
|
270
|
+
body.database,
|
|
271
|
+
body.collection,
|
|
272
|
+
AccessTypes.write,
|
|
273
|
+
body.doc,
|
|
274
|
+
ctx.state.user
|
|
275
|
+
);
|
|
276
|
+
if (!hasAccess) {
|
|
277
|
+
console.log(body);
|
|
278
|
+
console.log(ctx.state.user.permission);
|
|
279
|
+
ctx.throw(403, 'access denied');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// collection validation
|
|
283
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
284
|
+
if (collection == null) {
|
|
285
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// operate on db
|
|
289
|
+
await new collection(body.doc)
|
|
290
|
+
.save()
|
|
291
|
+
.then(async newDoc => {
|
|
292
|
+
// Call trigger
|
|
293
|
+
service.triggers.call('insert-one', body.database, body.collection, {
|
|
294
|
+
doc: body.doc,
|
|
295
|
+
queryResult: newDoc,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
ctx.body = { data: newDoc };
|
|
299
|
+
})
|
|
300
|
+
.catch(err => {
|
|
301
|
+
ctx.status = err.status || 500;
|
|
302
|
+
ctx.body = err.message;
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
dataProvider.post('/remove-one', async (ctx: Context) => {
|
|
307
|
+
const body = ctx.request.body;
|
|
308
|
+
const bodyValidate = validateObject(body, 'database collection query');
|
|
309
|
+
|
|
310
|
+
// fields validation
|
|
311
|
+
if (!bodyValidate.isValid) {
|
|
312
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// access validation
|
|
316
|
+
const hasAccess = service.checkAccess(
|
|
317
|
+
body.database,
|
|
318
|
+
body.collection,
|
|
319
|
+
AccessTypes.write,
|
|
320
|
+
body.query,
|
|
321
|
+
ctx.state.user
|
|
322
|
+
);
|
|
323
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
324
|
+
|
|
325
|
+
// collection validation
|
|
326
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
327
|
+
if (collection == null) {
|
|
328
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// get removing doc as output for triggers
|
|
332
|
+
const output: any = await collection.findOne(body.query).exec().then();
|
|
333
|
+
|
|
334
|
+
// operate on db
|
|
335
|
+
await collection
|
|
336
|
+
.deleteOne(body.query)
|
|
337
|
+
.exec()
|
|
338
|
+
.then(async (result: any) => {
|
|
339
|
+
// Call trigger
|
|
340
|
+
service.triggers.call('remove-one', body.database, body.collection, {
|
|
341
|
+
query: body.query,
|
|
342
|
+
queryResult: result,
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
ctx.body = { data: result };
|
|
346
|
+
})
|
|
347
|
+
.catch((err: Error) => {
|
|
348
|
+
ctx.status = (err as any).status || 500;
|
|
349
|
+
ctx.body = err.message;
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
dataProvider.post('/aggregate', async (ctx: Context) => {
|
|
354
|
+
const body = ctx.request.body;
|
|
355
|
+
const bodyValidate = validateObject(body, 'database collection accessQuery');
|
|
356
|
+
|
|
357
|
+
// fields validation
|
|
358
|
+
if (!bodyValidate.isValid) {
|
|
359
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// access validation
|
|
363
|
+
const hasAccess = service.checkAccess(
|
|
364
|
+
body.database,
|
|
365
|
+
body.collection,
|
|
366
|
+
AccessTypes.read,
|
|
367
|
+
body.accessQuery,
|
|
368
|
+
ctx.state.user
|
|
369
|
+
);
|
|
370
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
371
|
+
|
|
372
|
+
// collection validation
|
|
373
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
374
|
+
if (collection == null) {
|
|
375
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// operate on db
|
|
379
|
+
await collection
|
|
380
|
+
.aggregate(body.pipelines)
|
|
381
|
+
.exec()
|
|
382
|
+
.then(async (result: any) => {
|
|
383
|
+
// Call trigger
|
|
384
|
+
service.triggers.call('aggregate', body.database, body.collection, {
|
|
385
|
+
pipelines: body.pipelines,
|
|
386
|
+
queryResult: result,
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
ctx.body = { data: result };
|
|
390
|
+
})
|
|
391
|
+
.catch((err: any) => {
|
|
392
|
+
ctx.status = err.status || 500;
|
|
393
|
+
ctx.body = err.message;
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
dataProvider.post('/findByIds', async (ctx: Context, next: Next) => {
|
|
398
|
+
const body = ctx.request.body;
|
|
399
|
+
const bodyValidate = validateObject(body, 'database collection ids');
|
|
400
|
+
|
|
401
|
+
// fields validation
|
|
402
|
+
if (!bodyValidate.isValid) {
|
|
403
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: bodyValidate.requires })));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// access validation
|
|
407
|
+
const hasAccess = service.checkAccess(
|
|
408
|
+
body.database,
|
|
409
|
+
body.collection,
|
|
410
|
+
AccessTypes.read,
|
|
411
|
+
body.accessQuery || {},
|
|
412
|
+
ctx.state.user
|
|
413
|
+
);
|
|
414
|
+
if (!hasAccess) ctx.throw(403, 'access denied');
|
|
415
|
+
|
|
416
|
+
// collection validation
|
|
417
|
+
const collection = service.getCollection(body.database, body.collection);
|
|
418
|
+
if (collection == null) {
|
|
419
|
+
ctx.throw(412, JSON.stringify(reply('e', { error: 'wrong database or collection' })));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const or: Array<{ _id: any }> = [];
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
body.ids.forEach((id: any) => {
|
|
426
|
+
const castedid = service.getAsID(id);
|
|
427
|
+
or.push({ _id: castedid });
|
|
428
|
+
});
|
|
429
|
+
} catch (e) {
|
|
430
|
+
console.log('ids.forEach', e);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const pipelines = [
|
|
434
|
+
{
|
|
435
|
+
$match: { $or: or },
|
|
436
|
+
},
|
|
437
|
+
// {
|
|
438
|
+
// $sort: body.sort || { _id: 1 }
|
|
439
|
+
// }
|
|
440
|
+
];
|
|
441
|
+
|
|
442
|
+
// operate on db
|
|
443
|
+
await collection
|
|
444
|
+
.aggregate(pipelines)
|
|
445
|
+
.exec()
|
|
446
|
+
.then(async (result: any[]) => {
|
|
447
|
+
ctx.state = { data: result };
|
|
448
|
+
await next();
|
|
449
|
+
})
|
|
450
|
+
.catch((err: Error) => {
|
|
451
|
+
ctx.status = (err as any).status || 500;
|
|
452
|
+
ctx.body = err.message;
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
// Final middleware for converting mongoose documents to JSON
|
|
457
|
+
dataProvider.use('/', async (ctx: Context, next: Next) => {
|
|
458
|
+
// this event is responsible to covert whole mongoose doc to json form
|
|
459
|
+
// including getters, public properties
|
|
460
|
+
// each mongoose doc must have a "toJson" method being defined on its own Schema.
|
|
461
|
+
|
|
462
|
+
const state = ctx.state;
|
|
463
|
+
// let result;
|
|
464
|
+
|
|
465
|
+
// // array
|
|
466
|
+
// if(!isNaN(state.length)) {
|
|
467
|
+
// result = [];
|
|
468
|
+
|
|
469
|
+
// for (let index = 0; index < state.length; index++) {
|
|
470
|
+
// const element = state[index];
|
|
471
|
+
// if(element.hasOwnProperty('toJson'))
|
|
472
|
+
// result.push(element.toJson());
|
|
473
|
+
// else result.push(element);
|
|
474
|
+
// }
|
|
475
|
+
// }
|
|
476
|
+
// // object
|
|
477
|
+
// else {
|
|
478
|
+
// result = state.toJson();
|
|
479
|
+
// }
|
|
480
|
+
|
|
481
|
+
ctx.body = state;
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
export { name, dataProvider as main };
|