简体   繁体   English

如何计算 prisma io 中的记录?

[英]how to count records in prisma io?

I am executing a query but the result of the account adds the letter "n", I don't understand why when I execute the query in mysql console it shows it correctly.我正在执行查询,但帐户的结果添加了字母“n”,我不明白为什么当我在 mysql 控制台中执行查询时,它会正确显示。

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

在此处输入图像描述

executing the same query but in heidiSQL.执行相同的查询,但在 heidiSQL 中。 在此处输入图像描述

Numbers with an n postfix denote the BigInt type in JavaScript MDN .带有n后缀的数字表示 JavaScript MDN中的 BigInt 类型。 This is probably due to some queryRaw changes that were made in v4.0.0.这可能是由于 v4.0.0 中进行了一些queryRaw更改。 The Integer results are now being returned as BigInt. Integer 结果现在作为 BigInt 返回。 You must change your code to handle the new type . You must change your code to handle the new type See this section of the upgrade guide.请参阅升级指南的这一部分

Example: Given this Schema file示例:给定这个 Schema 文件

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

and this script file和这个脚本文件

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)
}

The output is 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