yaml-admin-api 0.0.31 → 0.0.32

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml-admin-api",
3
- "version": "0.0.31",
3
+ "version": "0.0.32",
4
4
  "license": "MIT",
5
5
  "description": "YAML Admin API package",
6
6
  "type": "commonjs",
@@ -594,19 +594,47 @@ const makeApiGenerateFields = async (db, entity_name, yml_entity, yml, options,
594
594
  limit = limit || 1000
595
595
 
596
596
  let match_from_list = data_list.map(m=>matchPathInObject(m, match_from))
597
-
598
597
  match_from_list = match_from_list.filter(m=>m)
599
598
  const f = { [match]: {$in:match_from_list} }
600
599
  const projection = {[match]:1}
601
600
 
601
+ const aggregate = [
602
+ { $match: { [match]: {$in:match_from_list} } },
603
+ ]
604
+
602
605
  if(field)
603
606
  projection[field] = 1
604
- else
607
+ else {
605
608
  fields.map(m=>{
606
609
  projection[m.name] = 1
607
610
  })
608
611
 
609
- const result = await db.collection(entity).find(f).project(projection).sort(sort).limit(limit).toArray()
612
+ fields.map(m=>{
613
+ if(m.type == 'reference') {
614
+ let project = { _id: 0 }
615
+ m.fields.map(f=>{
616
+ project[f.name] = 1
617
+ })
618
+
619
+ aggregate.push({ $lookup: {
620
+ from: m.reference_entity,
621
+ let: { local_key: '$'+m.reference_from },
622
+ pipeline: [
623
+ { $match: { $expr: { $eq: ["$"+m.reference_match, "$$local_key"] } } },
624
+ { $project: project }
625
+ ],
626
+ as: m.name
627
+ } })
628
+ if(m.single)
629
+ aggregate.push({ $unwind: `$${m.name}` })
630
+ }
631
+ })
632
+ }
633
+
634
+ const result = await db.collection(entity)
635
+ .aggregate(aggregate)
636
+ .project(projection)
637
+ .toArray()
610
638
  data_list.map(m=>{
611
639
  let found = result.filter(f=>matchPathInObject(f, match) === matchPathInObject(m, match_from))
612
640
  if(single) {