【问题标题】:how do i reduce the complexity of following code using nodejs and prisma我如何使用 nodejs 和 prisma 降低以下代码的复杂性
【发布时间】:2022-12-06 13:43:06
【问题描述】:

要计算用户一年中花费的总金额,我需要包含总金额的数组。

它工作正常,但它非常复杂,因为我必须每天、每月和每周都这样做

我如何降低复杂性。

let query = '';
        graph_data = '';
        months_for_year = [];
        let startofyear = MOMENT().startOf('year');

        for (let index = 0; index <= 11; index++) {
          const add1month = MOMENT(startofyear)
            .add(index, 'month')
            .format('YYYY-MM-DD' + 'T' + 'HH:mm:SS' + '.000+00:00');
          months_for_year.push(add1month);
        }
        console.log(',,,', months_for_year);
        let year = [];
        for (let i = 0; i < months_for_year.length; i++) {
          let d = await PRISMA.orders.findMany({
            where: {
              created_at: {
                gte: months_for_year[i],
                lte: months_for_year[i + 1],
              },
            },
            select: { actual_amount: true },
          });
          let total = 0;
          d.forEach((el) => {
            total += el.actual_amount;
            d.push(total);
          });
          year.push(d);
        }
        graph_data_of_year = year.map((el) => {
          if (el.length === 0) {
            return 0;
          } else {
            return el.pop();
          }
        });
        console.log(graph_data_of_year);

【问题讨论】:

  • 按日期分组/聚合是not directly supported by prisma。如果你可以使它特定于数据库,你使用的是什么类型的 pf 数据库?
  • 我正在使用 mongodb

标签: java node.js typescript prisma


【解决方案1】:

有几种方法可以降低此代码的复杂性。一种方法是使用更有效的方法来计算用户一年内花费的总金额。与其遍历一年中的每个月然后查询数据库以获取该月的订单,不如查询一次数据库以获取全年的订单,然后使用一个循环来计算总支出。这会更有效,因为它将数据库查询的数量从 12 减少到 1。

这是一个如何做到这一点的例子:

let year = MOMENT().year();
let startofyear = MOMENT().startOf('year');
let endofyear = MOMENT().endOf('year');

let orders = await PRISMA.orders.findMany({
  where: {
    created_at: {
      gte: startofyear,
      lte: endofyear
    }
  },
  select: { actual_amount: true }
});

let total = 0;

for (let i = 0; i < orders.length; i++) {
  total += orders[i].actual_amount;
}

console.log(total); // total amount spent by the user in the year

更新:

要从单个结果重新创建分组数据,您可以遍历订单并使用片刻从中提取月份的库创建时间每个订单的时间戳。然后,您可以使用此信息按月对订单进行分组,并计算每个月的总支出。

这是一个如何做到这一点的例子:

let year = MOMENT().year();
let startofyear = MOMENT().startOf('year');
let endofyear = MOMENT().endOf('year');

let orders = await PRISMA.orders.findMany({
  where: {
    created_at: {
      gte: startofyear,
      lte: endofyear
    }
  },
  select: { actual_amount: true }
});

let data = {};

for (let i = 0; i < orders.length; i++) {
  let month = MOMENT(orders[i].created_at).month();
  if (!data[month]) {
    data[month] = 0;
  }
  data[month] += orders[i].actual_amount;
}

console.log(data); // object containing the total amount spent by the user in each month of the year

【讨论】:

  • op 将如何从单个结果中重新创建分组数据?
  • 我更新了答案,这应该有效
猜你喜欢
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多