yaml-admin-api 0.0.75 → 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 +47 -22
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
|
|
@@ -253,19 +269,16 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
253
269
|
let aggregate = await makeApiGenerateAggregate(db, entity_name, yml_entity, yml, options)
|
|
254
270
|
|
|
255
271
|
if(aggregate?.length > 0) {
|
|
256
|
-
aggregate = [{$match: f}
|
|
257
|
-
|
|
258
|
-
const countResult = await db.collection(entity_name).aggregate([...aggregate, { $count: 'count' }]).toArray()
|
|
272
|
+
aggregate = [...aggregate, {$match: f}]
|
|
273
|
+
const countResult = await db.collection(collection_name).aggregate([...aggregate, { $count: 'count' }]).toArray()
|
|
259
274
|
count = countResult.length > 0 ? countResult[0].count : 0
|
|
260
275
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
.skip(parseInt(_start))
|
|
264
|
-
.limit(l).toArray()
|
|
276
|
+
aggregate = [...aggregate, { $sort: s }, { $skip: parseInt(_start) }, { $limit: l }]
|
|
277
|
+
list = await db.collection(collection_name).aggregate(aggregate).toArray()
|
|
265
278
|
} else
|
|
266
279
|
{
|
|
267
|
-
count = await db.collection(
|
|
268
|
-
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()
|
|
269
282
|
}
|
|
270
283
|
|
|
271
284
|
if(yml.debug)
|
|
@@ -337,7 +350,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
337
350
|
if (entityId) {
|
|
338
351
|
let f = {}
|
|
339
352
|
f[key_field.name] = entityId
|
|
340
|
-
let already = await db.collection(
|
|
353
|
+
let already = await db.collection(collection_name).findOne(f)
|
|
341
354
|
if (already)
|
|
342
355
|
throw new Error("duplicate key of [" + key_field.name + "] - [" + entityId + "]")
|
|
343
356
|
}
|
|
@@ -346,11 +359,14 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
346
359
|
entity['update_date'] = entity['create_date'] = new Date()
|
|
347
360
|
entity['create_admin_id'] = req.user.id
|
|
348
361
|
|
|
362
|
+
// default_filter 값을 entity에 자동 적용
|
|
363
|
+
Object.assign(entity, default_filter)
|
|
364
|
+
|
|
349
365
|
//Custom Create Start
|
|
350
366
|
|
|
351
367
|
//Custom Create End
|
|
352
368
|
|
|
353
|
-
var r = await db.collection(
|
|
369
|
+
var r = await db.collection(collection_name).insertOne(entity);
|
|
354
370
|
//Custom Create Tail Start
|
|
355
371
|
if(options?.listener?.entityCreated)
|
|
356
372
|
await options.listener.entityCreated(db, entity_name, entity)
|
|
@@ -380,6 +396,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
380
396
|
|
|
381
397
|
let f = {}
|
|
382
398
|
f[key_field.name] = entityId
|
|
399
|
+
Object.assign(f, default_filter)
|
|
383
400
|
|
|
384
401
|
for (let field of yml_entity.fields) {
|
|
385
402
|
if (['mp4', 'image', 'file'].includes(field.type)) {
|
|
@@ -391,7 +408,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
391
408
|
}
|
|
392
409
|
}
|
|
393
410
|
|
|
394
|
-
await db.collection(
|
|
411
|
+
await db.collection(collection_name).updateOne(f, { $set: entity });
|
|
395
412
|
|
|
396
413
|
//Custom Create Tail Start
|
|
397
414
|
if(options?.listener?.entityUpdated)
|
|
@@ -408,16 +425,17 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
408
425
|
app.get(`${api_prefix}/${entity_name}/:id`, auth.isAuthenticated, asyncErrorHandler(async (req, res) => {
|
|
409
426
|
let f = {}
|
|
410
427
|
f[key_field.name] = parseKey(req.params.id)
|
|
428
|
+
Object.assign(f, default_filter)
|
|
411
429
|
|
|
412
430
|
let aggregate = await makeApiGenerateAggregate(db, entity_name, yml_entity, yml, options)
|
|
413
431
|
|
|
414
432
|
let m
|
|
415
433
|
if(aggregate?.length > 0) {
|
|
416
434
|
aggregate = [{$match: f}, ...aggregate, { $limit: 1 }]
|
|
417
|
-
const result = await db.collection(
|
|
435
|
+
const result = await db.collection(collection_name).aggregate(aggregate).toArray()
|
|
418
436
|
m = result.length > 0 ? result[0] : null
|
|
419
437
|
} else {
|
|
420
|
-
m = await db.collection(
|
|
438
|
+
m = await db.collection(collection_name).findOne(f);
|
|
421
439
|
}
|
|
422
440
|
|
|
423
441
|
if (!m)
|
|
@@ -436,7 +454,8 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
436
454
|
app.delete(`${api_prefix}/${entity_name}/:id`, auth.isAuthenticated, asyncErrorHandler(async (req, res) =>{
|
|
437
455
|
let f = {}
|
|
438
456
|
f[key_field.name] = parseKey(req.params.id)
|
|
439
|
-
|
|
457
|
+
Object.assign(f, default_filter)
|
|
458
|
+
const entity = await db.collection(collection_name).findOne(f);
|
|
440
459
|
if (!entity)
|
|
441
460
|
return res.status(404).send('Not found');
|
|
442
461
|
|
|
@@ -451,9 +470,9 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
451
470
|
if (customDelete)
|
|
452
471
|
;
|
|
453
472
|
else if (softDelete)
|
|
454
|
-
await db.collection(
|
|
473
|
+
await db.collection(collection_name).updateOne(f, { $set: { remove: true } });
|
|
455
474
|
else
|
|
456
|
-
await db.collection(
|
|
475
|
+
await db.collection(collection_name).deleteOne(f);
|
|
457
476
|
|
|
458
477
|
if(options?.listener?.entityDeleted)
|
|
459
478
|
await options.listener.entityDeleted(db, entity_name, entity)
|
|
@@ -472,7 +491,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
472
491
|
//{ label: '상품', value: row => row.product_list?.map(m=>m.total_name).join(',') },
|
|
473
492
|
|
|
474
493
|
let f = req.body.filter || {}
|
|
475
|
-
const list = await db.collection(
|
|
494
|
+
const list = await db.collection(collection_name).find(f).project({
|
|
476
495
|
_id: false,
|
|
477
496
|
}).toArray();
|
|
478
497
|
|
|
@@ -567,7 +586,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
|
|
|
567
586
|
})
|
|
568
587
|
}
|
|
569
588
|
|
|
570
|
-
let result = await db.collection(
|
|
589
|
+
let result = await db.collection(collection_name).bulkWrite(bulk);
|
|
571
590
|
//result에서 update entity와 created entity list로 추출 해서 options?.listener?.entityCreated?.(entity_name, createdEntity)와 options?.listener?.entityUpdated?.(entity_name, updateEntity) 호출
|
|
572
591
|
try {
|
|
573
592
|
const upsertIndexToId = new Map()
|
|
@@ -707,6 +726,12 @@ const makeApiGenerateAggregate = async (db, entity_name, yml_entity, yml, option
|
|
|
707
726
|
{ $match: { $expr: { $eq: ['$' + match, '$$local_key'] } } }
|
|
708
727
|
]
|
|
709
728
|
|
|
729
|
+
// single일 때 max _id로 하나만 선택 ($match 바로 다음에)
|
|
730
|
+
if(single) {
|
|
731
|
+
innerPipeline.push({ $sort: { _id: -1 } })
|
|
732
|
+
innerPipeline.push({ $limit: 1 })
|
|
733
|
+
}
|
|
734
|
+
|
|
710
735
|
// projection 구성
|
|
711
736
|
const projection = { _id: 0, [match]: 1 }
|
|
712
737
|
if(field) {
|
|
@@ -726,8 +751,8 @@ const makeApiGenerateAggregate = async (db, entity_name, yml_entity, yml, option
|
|
|
726
751
|
})
|
|
727
752
|
}
|
|
728
753
|
|
|
729
|
-
// projection을 innerPipeline
|
|
730
|
-
innerPipeline.
|
|
754
|
+
// projection을 innerPipeline에 추가
|
|
755
|
+
innerPipeline.push({ $project: projection })
|
|
731
756
|
|
|
732
757
|
// 기본 $lookup 추가
|
|
733
758
|
aggregate.push({
|