繁体   English   中英

如何计算 prisma io 中的记录?

[英]how to count records in prisma io?

我正在执行查询,但帐户的结果添加了字母“n”,我不明白为什么当我在 mysql 控制台中执行查询时,它会正确显示。

const client = await prisma.$queryRaw`SELECT idClient, COUNT(*) as totalCount FROM sales GROUP BY idClient`;
console.log(client)

在此处输入图像描述

执行相同的查询,但在 heidiSQL 中。 在此处输入图像描述

带有n后缀的数字表示 JavaScript MDN中的 BigInt 类型。 这可能是由于 v4.0.0 中进行了一些queryRaw更改。 Integer 结果现在作为 BigInt 返回。 You must change your code to handle the new type 请参阅升级指南的这一部分

示例:给定这个 Schema 文件

model Customers {
  id  Int  @id @default(autoincrement())
  customerName String
  country String
}

和这个脚本文件

async function main() {

  await prisma.customers.createMany({
    data: [
      {
        country: 'USA',
        customerName: 'John Doe',
      },
      {
        country: 'Germany',
        customerName: 'Jane Doe',
      },
      {
        country: 'Canada',
        customerName: 'Adams Doe',
      },
    ],
  });

  const clients = await prisma.customers.groupBy({
    by: ['country'],
    _count: true,
  });
  console.log("Using normal client query with groupBy ")
  console.log(clients);

  const clientsWithRawQuery =
    await prisma.$queryRaw`SELECT Country, COUNT(country) as totalCount
FROM Customers GROUP BY Country`;
  console.log("\n Using queryRaw")
  console.log(clientsWithRawQuery);

  console.log(
    'Before Conversion: Typeof Count:',
    typeof clientsWithRawQuery[0].totalCount
  );

  clientsWithRawQuery.forEach((countryObject) => {
    countryObject.totalCount = Number(countryObject.totalCount);
  });

  console.log(
    'After Conversion: Typeof Count:',
    typeof clientsWithRawQuery[0].totalCount
  );
  console.log('\n', clientsWithRawQuery)
}

output 是

Using normal client query with groupBy 
[
  { _count: 2, country: 'Canada' },
  { _count: 2, country: 'Germany' },
  { _count: 2, country: 'USA' }
]

 Using queryRaw
[
  { Country: 'Canada', totalCount: 2n },
  { Country: 'Germany', totalCount: 2n },
  { Country: 'USA', totalCount: 2n }
]
Before Conversion: Typeof Count: bigint
After Conversion: Typeof Count: number

 [
  { Country: 'Canada', totalCount: 2 },
  { Country: 'Germany', totalCount: 2 },
  { Country: 'USA', totalCount: 2 }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM