yaml-admin-api 0.0.31 → 0.0.33

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.33",
4
4
  "license": "MIT",
5
5
  "description": "YAML Admin API package",
6
6
  "type": "commonjs",
@@ -164,13 +164,15 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
164
164
  * @param {*} entity_name
165
165
  */
166
166
  const recalcurateAutoGenerateIndex = async (db, entity_name) => {
167
- const list = await db.collection(entity_name).find({}).sort({ [key_field.name]: -1 }).limit(1).toArray()
168
- const counter = await db.collection('counters').findOne({ _id: key_field.name })
169
- if(list.length > 0) {
167
+ const list = await db.collection(entity_name).find({})
168
+ .project({ [key_field.name]: 1, _id: 0 })
169
+ .sort({ [key_field.name]: -1 }).limit(1).toArray()
170
+ const counter = await db.collection('counters').findOne({ _id: entity_name })
171
+ if(list.length > 0 && counter) {
170
172
  let seq = counter?.seq || 0
171
173
  let maxKey = list[0][key_field.name]
172
- if(maxKey <= seq)
173
- await db.collection('counters').updateOne({ _id: key_field.name }, { $set: { seq: maxKey + 1 } })
174
+ if(maxKey >= seq)
175
+ await db.collection('counters').updateOne({ _id: entity_name }, { $set: { seq: maxKey + 1 } })
174
176
  }
175
177
  }
176
178
 
@@ -223,14 +225,17 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
223
225
  //Custom f list End
224
226
 
225
227
  const projection = (key_field.name == '_id' ? {} : { _id: false })
228
+ if(yml.debug)
229
+ console.log('list', entity_name, f)
226
230
  var count = await db.collection(entity_name).find(f).project(projection).sort(s).count();
227
231
  let list = await db.collection(entity_name).find(f).project(projection).sort(s).skip(parseInt(_start)).limit(l).toArray()
228
232
  list.map(m => {
229
233
  m.id = getKeyFromEntity(m)
230
234
  })
231
- //Custom list Start
232
-
233
- //Custom list End
235
+
236
+ if(yml.debug)
237
+ console.log('list', entity_name, 'found', count)
238
+
234
239
  await addInfo(db, list)
235
240
 
236
241
  res.header('X-Total-Count', count);
@@ -594,19 +599,47 @@ const makeApiGenerateFields = async (db, entity_name, yml_entity, yml, options,
594
599
  limit = limit || 1000
595
600
 
596
601
  let match_from_list = data_list.map(m=>matchPathInObject(m, match_from))
597
-
598
602
  match_from_list = match_from_list.filter(m=>m)
599
603
  const f = { [match]: {$in:match_from_list} }
600
604
  const projection = {[match]:1}
601
605
 
606
+ const aggregate = [
607
+ { $match: { [match]: {$in:match_from_list} } },
608
+ ]
609
+
602
610
  if(field)
603
611
  projection[field] = 1
604
- else
612
+ else {
605
613
  fields.map(m=>{
606
614
  projection[m.name] = 1
607
615
  })
608
616
 
609
- const result = await db.collection(entity).find(f).project(projection).sort(sort).limit(limit).toArray()
617
+ fields.map(m=>{
618
+ if(m.type == 'reference') {
619
+ let project = { _id: 0 }
620
+ m.fields.map(f=>{
621
+ project[f.name] = 1
622
+ })
623
+
624
+ aggregate.push({ $lookup: {
625
+ from: m.reference_entity,
626
+ let: { local_key: '$'+m.reference_from },
627
+ pipeline: [
628
+ { $match: { $expr: { $eq: ["$"+m.reference_match, "$$local_key"] } } },
629
+ { $project: project }
630
+ ],
631
+ as: m.name
632
+ } })
633
+ if(m.single)
634
+ aggregate.push({ $unwind: `$${m.name}` })
635
+ }
636
+ })
637
+ }
638
+
639
+ const result = await db.collection(entity)
640
+ .aggregate(aggregate)
641
+ .project(projection)
642
+ .toArray()
610
643
  data_list.map(m=>{
611
644
  let found = result.filter(f=>matchPathInObject(f, match) === matchPathInObject(m, match_from))
612
645
  if(single) {