core icon indicating copy to clipboard operation
core copied to clipboard

Group operation

Open stombre opened this issue 5 years ago • 0 comments

Average, sum...

Search for an API;

const result = await Invoices.query()
  .groupBy(Invoices.userId)
  .define('totalDebt').asSumOf(Invoices.amount)
  .define('nbInvoicse').asCountOf(Invoices)
  .find();

Hard scenario, how to do two stage grouping pipeline?

const result = await Invoices.query()
  .stage((query, Invoices) => {
    query.groupBy(query.year(Invoices.createdAt)).as('year')
      .groupBy(query.month(invoicse.createdAt)).as('month')
      .define('averageDebt').asAverageOf(Invoices.amount);
  })
  .stage((query, InvoicesAggregatedByMonthAndYear) => {
    query.groupBy(query.year(InvoicesAggregatedByMonthAndYear.year))
      .define('averageDebt').asAverageOf(InvoicesAggregatedByMonthAndYear.averageDebt))
  });

Need to introduce field defined on Model? sub field;

const result = await Invoices.query()
  .groupBy(Invoices.FIELDS.userId)
  .define('totalDebt').asSumOf(Invoices.FIELDS.amount)
  .define('nbInvoicse').asCountOf(Invoices)
  .find();
  • query.groupBy: Apply a groupBy operation
  • query.define: Create a property in the result. When define something? it's not a model instance anymore? alternate operation than find? apply? generate? fetch?
  • query.stage; multiple group by (support for mongoDb aggregate)
  • define.asSumOf
  • define.asAverageOf
  • define.asCountOf
  • define.asMaxOf
  • define.asMinOf

How to have access to this in every context?

  • query.year (apply a year on a date?)

  • query.month (apply a month on a date?)

  • query.day (apply a day on a date?)

  • [ ] query.define

  • [ ] DEFINE.as

  • [ ] Schema.FIELDS

  • [ ] DEFINE.asSumOf

  • [ ] DEFINE.asAverageOf

  • [ ] DEFINE.asCountOf

  • [ ] DEFINE.asMaxOf

  • [ ] DEFINE.asMinOf

  • [ ] query.groupBy

  • [ ] query.stage

stombre avatar Sep 05 '20 13:09 stombre