yaml-admin-api 0.0.76 → 0.0.77
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/crud/entity-api-generator.js +37 -15
package/package.json
CHANGED
|
@@ -19,6 +19,19 @@ const asyncErrorHandler = (fn) => (req, res, next) => {
|
|
|
19
19
|
|
|
20
20
|
const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options }) => {
|
|
21
21
|
|
|
22
|
+
// entity 속성이 있으면 해당 값을 실제 collection 이름으로 사용
|
|
23
|
+
const collection_name = yml_entity.entity || entity_name
|
|
24
|
+
|
|
25
|
+
// filter 배열을 객체로 변환
|
|
26
|
+
let default_filter = {}
|
|
27
|
+
if (Array.isArray(yml_entity.filter)) {
|
|
28
|
+
yml_entity.filter.forEach(f => {
|
|
29
|
+
default_filter[f.name] = f.value
|
|
30
|
+
})
|
|
31
|
+
} else if (yml_entity.filter) {
|
|
32
|
+
default_filter = yml_entity.filter
|
|
33
|
+
}
|
|
34
|
+
|
|
22
35
|
const auth = withConfig({ db, jwt_secret: yml.login["jwt-secret"] });
|
|
23
36
|
const passwordEncoding = yml.login['password-encoding']
|
|
24
37
|
const api_host = yml["api-host"].uri;
|
|
@@ -177,7 +190,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
177
190
|
* @param {*} entity_name
|
|
178
191
|
*/
|
|
179
192
|
const recalcurateAutoGenerateIndex = async (db, entity_name) => {
|
|
180
|
-
const list = await db.collection(
|
|
193
|
+
const list = await db.collection(collection_name).find({})
|
|
181
194
|
.project({ [key_field.name]: 1, _id: 0 })
|
|
182
195
|
.sort({ [key_field.name]: -1 }).limit(1).toArray()
|
|
183
196
|
const counter = await db.collection('counters').findOne({ _id: entity_name })
|
|
@@ -241,6 +254,9 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
241
254
|
f['name'] = { $regex: ".*" + name + ".*" };
|
|
242
255
|
f.remove = { $ne: true }
|
|
243
256
|
|
|
257
|
+
// default_filter 적용
|
|
258
|
+
Object.assign(f, default_filter)
|
|
259
|
+
|
|
244
260
|
//Custom f list Start
|
|
245
261
|
|
|
246
262
|
//Custom f list End
|
|
@@ -254,15 +270,15 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
254
270
|
|
|
255
271
|
if(aggregate?.length > 0) {
|
|
256
272
|
aggregate = [...aggregate, {$match: f}]
|
|
257
|
-
const countResult = await db.collection(
|
|
273
|
+
const countResult = await db.collection(collection_name).aggregate([...aggregate, { $count: 'count' }]).toArray()
|
|
258
274
|
count = countResult.length > 0 ? countResult[0].count : 0
|
|
259
275
|
|
|
260
276
|
aggregate = [...aggregate, { $sort: s }, { $skip: parseInt(_start) }, { $limit: l }]
|
|
261
|
-
list = await db.collection(
|
|
277
|
+
list = await db.collection(collection_name).aggregate(aggregate).toArray()
|
|
262
278
|
} else
|
|
263
279
|
{
|
|
264
|
-
count = await db.collection(
|
|
265
|
-
list = await db.collection(
|
|
280
|
+
count = await db.collection(collection_name).find(f).project(projection).sort(s).count()
|
|
281
|
+
list = await db.collection(collection_name).find(f).project(projection).sort(s).skip(parseInt(_start)).limit(l).toArray()
|
|
266
282
|
}
|
|
267
283
|
|
|
268
284
|
if(yml.debug)
|
|
@@ -334,7 +350,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
334
350
|
if (entityId) {
|
|
335
351
|
let f = {}
|
|
336
352
|
f[key_field.name] = entityId
|
|
337
|
-
let already = await db.collection(
|
|
353
|
+
let already = await db.collection(collection_name).findOne(f)
|
|
338
354
|
if (already)
|
|
339
355
|
throw new Error("duplicate key of [" + key_field.name + "] - [" + entityId + "]")
|
|
340
356
|
}
|
|
@@ -343,11 +359,14 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
343
359
|
entity['update_date'] = entity['create_date'] = new Date()
|
|
344
360
|
entity['create_admin_id'] = req.user.id
|
|
345
361
|
|
|
362
|
+
// default_filter 값을 entity에 자동 적용
|
|
363
|
+
Object.assign(entity, default_filter)
|
|
364
|
+
|
|
346
365
|
//Custom Create Start
|
|
347
366
|
|
|
348
367
|
//Custom Create End
|
|
349
368
|
|
|
350
|
-
var r = await db.collection(
|
|
369
|
+
var r = await db.collection(collection_name).insertOne(entity);
|
|
351
370
|
//Custom Create Tail Start
|
|
352
371
|
if(options?.listener?.entityCreated)
|
|
353
372
|
await options.listener.entityCreated(db, entity_name, entity)
|
|
@@ -377,6 +396,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
377
396
|
|
|
378
397
|
let f = {}
|
|
379
398
|
f[key_field.name] = entityId
|
|
399
|
+
Object.assign(f, default_filter)
|
|
380
400
|
|
|
381
401
|
for (let field of yml_entity.fields) {
|
|
382
402
|
if (['mp4', 'image', 'file'].includes(field.type)) {
|
|
@@ -388,7 +408,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
388
408
|
}
|
|
389
409
|
}
|
|
390
410
|
|
|
391
|
-
await db.collection(
|
|
411
|
+
await db.collection(collection_name).updateOne(f, { $set: entity });
|
|
392
412
|
|
|
393
413
|
//Custom Create Tail Start
|
|
394
414
|
if(options?.listener?.entityUpdated)
|
|
@@ -405,16 +425,17 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
405
425
|
app.get(`${api_prefix}/${entity_name}/:id`, auth.isAuthenticated, asyncErrorHandler(async (req, res) => {
|
|
406
426
|
let f = {}
|
|
407
427
|
f[key_field.name] = parseKey(req.params.id)
|
|
428
|
+
Object.assign(f, default_filter)
|
|
408
429
|
|
|
409
430
|
let aggregate = await makeApiGenerateAggregate(db, entity_name, yml_entity, yml, options)
|
|
410
431
|
|
|
411
432
|
let m
|
|
412
433
|
if(aggregate?.length > 0) {
|
|
413
434
|
aggregate = [{$match: f}, ...aggregate, { $limit: 1 }]
|
|
414
|
-
const result = await db.collection(
|
|
435
|
+
const result = await db.collection(collection_name).aggregate(aggregate).toArray()
|
|
415
436
|
m = result.length > 0 ? result[0] : null
|
|
416
437
|
} else {
|
|
417
|
-
m = await db.collection(
|
|
438
|
+
m = await db.collection(collection_name).findOne(f);
|
|
418
439
|
}
|
|
419
440
|
|
|
420
441
|
if (!m)
|
|
@@ -433,7 +454,8 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
433
454
|
app.delete(`${api_prefix}/${entity_name}/:id`, auth.isAuthenticated, asyncErrorHandler(async (req, res) =>{
|
|
434
455
|
let f = {}
|
|
435
456
|
f[key_field.name] = parseKey(req.params.id)
|
|
436
|
-
|
|
457
|
+
Object.assign(f, default_filter)
|
|
458
|
+
const entity = await db.collection(collection_name).findOne(f);
|
|
437
459
|
if (!entity)
|
|
438
460
|
return res.status(404).send('Not found');
|
|
439
461
|
|
|
@@ -448,9 +470,9 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
448
470
|
if (customDelete)
|
|
449
471
|
;
|
|
450
472
|
else if (softDelete)
|
|
451
|
-
await db.collection(
|
|
473
|
+
await db.collection(collection_name).updateOne(f, { $set: { remove: true } });
|
|
452
474
|
else
|
|
453
|
-
await db.collection(
|
|
475
|
+
await db.collection(collection_name).deleteOne(f);
|
|
454
476
|
|
|
455
477
|
if(options?.listener?.entityDeleted)
|
|
456
478
|
await options.listener.entityDeleted(db, entity_name, entity)
|
|
@@ -469,7 +491,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
469
491
|
//{ label: '상품', value: row => row.product_list?.map(m=>m.total_name).join(',') },
|
|
470
492
|
|
|
471
493
|
let f = req.body.filter || {}
|
|
472
|
-
const list = await db.collection(
|
|
494
|
+
const list = await db.collection(collection_name).find(f).project({
|
|
473
495
|
_id: false,
|
|
474
496
|
}).toArray();
|
|
475
497
|
|
|
@@ -564,7 +586,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
564
586
|
})
|
|
565
587
|
}
|
|
566
588
|
|
|
567
|
-
let result = await db.collection(
|
|
589
|
+
let result = await db.collection(collection_name).bulkWrite(bulk);
|
|
568
590
|
//result에서 update entity와 created entity list로 추출 해서 options?.listener?.entityCreated?.(entity_name, createdEntity)와 options?.listener?.entityUpdated?.(entity_name, updateEntity) 호출
|
|
569
591
|
try {
|
|
570
592
|
const upsertIndexToId = new Map()
|