yaml-admin-api 0.0.84 → 0.0.85

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.84",
3
+ "version": "0.0.85",
4
4
  "license": "MIT",
5
5
  "description": "YAML Admin API package",
6
6
  "type": "commonjs",
@@ -107,7 +107,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
107
107
  a.push({ $limit: limit })
108
108
 
109
109
  //debug
110
- if(yml.debug)
110
+ if(chart.debug)
111
111
  console.log('chart', chart.label, entity_x, JSON.stringify(a, null, 2))
112
112
 
113
113
  const list = await db.collection(entity_x).aggregate(a).toArray();
@@ -243,7 +243,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
243
243
  a.push({ $limit: limit })
244
244
 
245
245
  //debug
246
- if(yml.debug)
246
+ if(chart.debug)
247
247
  console.log('chart', chart.label, entity_x, JSON.stringify(a, null, 2))
248
248
 
249
249
  const list = await db.collection(entity_x).aggregate(a).toArray();
@@ -324,6 +324,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
324
324
  })
325
325
  }
326
326
  }
327
+
327
328
  /**
328
329
  * 'lock==true' or 'lock!=true' to mongodb match format like {lock:true} , {lock:{$ne:true}}
329
330
  * @param {*} expression
@@ -409,10 +410,11 @@ function evaluateIfToMatch(expression) {
409
410
  }
410
411
  }
411
412
 
413
+
412
414
  function getPath(obj, path) {
413
415
  return path.split('.').reduce((acc, k) => (acc && acc[k] !== undefined ? acc[k] : undefined), obj);
414
416
  }
415
417
 
416
418
  module.exports = {
417
- generateChartApi
419
+ generateChartApi,
418
420
  }
@@ -123,6 +123,11 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
123
123
  return null
124
124
  if(value?.startsWith('$')) {
125
125
  const [op, val] = value.split(' ')
126
+
127
+ if (type == 'array') {
128
+ return { __isExpr: true, op, val: parseInt(val), field: field.name }
129
+ }
130
+
126
131
  if(op == '$lte')
127
132
  return { $lte: parseFloat(val) }
128
133
  else if(op == '$gte')
@@ -267,7 +272,13 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
267
272
  f[field.name] = { $in: q.map(v => parseValueByType(v, field)) };
268
273
  } else {
269
274
  if (search?.exact != false || field.type == 'integer') {
270
- f[field.name] = parseValueByType(q, field)
275
+ const v = parseValueByType(q, field)
276
+ if(v && v.__isExpr) {
277
+ if(!f.$expr) f.$expr = { $and: [] }
278
+ f.$expr.$and.push({ [v.op]: [{ $size: `$${v.field}` }, v.val] })
279
+ } else {
280
+ f[field.name] = v
281
+ }
271
282
  } else
272
283
  f[field.name] = { $regex: ".*" + q + ".*" };
273
284
  }
@@ -303,7 +314,7 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
303
314
 
304
315
  const projection = (key_field.name == '_id' ? {} : { _id: false })
305
316
  let list, count;
306
- let aggregate = await makeApiGenerateAggregate(db, entity_name, yml_entity, yml, options)
317
+ let aggregate = await makeApiGenerateAggregate(yml_entity.api_generate)
307
318
 
308
319
  if(aggregate?.length > 0) {
309
320
  aggregate = [...aggregate, {$match: f}]
@@ -311,17 +322,17 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
311
322
  count = countResult.length > 0 ? countResult[0].count : 0
312
323
 
313
324
  aggregate = [...aggregate, { $sort: s }, { $skip: parseInt(_start) }, { $limit: l }]
314
- if(yml.debug)
315
- console.log('list', entity_name, JSON.stringify(aggregate, null, 2))
325
+ if(yml.debug || yml_entity.debug)
326
+ console.log('list', entity_name, JSON.stringify(aggregate))
316
327
  list = await db.collection(collection_name).aggregate(aggregate).toArray()
317
328
  } else {
318
- if(yml.debug)
329
+ if(yml.debug || yml_entity.debug)
319
330
  console.log('list', entity_name, f)
320
331
  count = await db.collection(collection_name).find(f).project(projection).sort(s).count()
321
332
  list = await db.collection(collection_name).find(f).project(projection).sort(s).skip(parseInt(_start)).limit(l).toArray()
322
333
  }
323
334
 
324
- if(yml.debug)
335
+ if(yml.debug || yml_entity.debug)
325
336
  console.log('list', entity_name, 'count', count, 'list length', list.length)
326
337
 
327
338
  list.map(m => {
@@ -344,12 +355,12 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
344
355
  f[key_field.name] = parseKey(req.params.id)
345
356
  Object.assign(f, default_filter)
346
357
 
347
- let aggregate = await makeApiGenerateAggregate(db, entity_name, yml_entity, yml, options)
358
+ let aggregate = await makeApiGenerateAggregate(yml_entity.api_generate)
348
359
 
349
360
  let m
350
361
  if(aggregate?.length > 0) {
351
362
  aggregate = [{$match: f}, ...aggregate, { $limit: 1 }]
352
- if(yml.debug && entity_name == 'ils')
363
+ if(yml.debug || yml_entity.debug)
353
364
  console.log('list', entity_name, JSON.stringify(aggregate, null, 2))
354
365
  const result = await db.collection(collection_name).aggregate(aggregate).toArray()
355
366
  m = result.length > 0 ? result[0] : null
@@ -710,8 +721,7 @@ const matchPathInObject = (obj, path) => {
710
721
  return r
711
722
  }
712
723
 
713
- const makeApiGenerateAggregate = async (db, entity_name, yml_entity, yml, options) => {
714
- const apiGenerate = yml_entity.api_generate
724
+ const makeApiGenerateAggregate = async (apiGenerate) => {
715
725
  if(!apiGenerate)
716
726
  return;
717
727