It is normal behavior that Prisma return e.g. `100...
# orm-help
e
It is normal behavior that Prisma return e.g.
100n
for BigInt? I wonder if I can somehow remove that
n
automatically
1
n
Hey! 👋 From prisma version 4 and above this is indeed expected behaviour. If you want to remove the
n
suffix, you could do something like this In this example the count was returned as BigInt and then it was converted to number through
Number
constructor
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query'],
});

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

  const clients = await prisma.customers.groupBy({
    by: ['country'],
    _count: true,
  });

  console.log(clients);

  const clientsWithRawQuery: any =
    await prisma.$queryRaw`SELECT Country, COUNT(country) as totalCount
FROM "public"."Customers" GROUP BY Country`;

  console.log(clientsWithRawQuery);

  // @ts-ignore
  console.log(
    'Before Conversion: Typeof Count:',
    typeof clientsWithRawQuery[0].totalcount
  );

  //Converting to Integer
  // @ts-ignore
  clientsWithRawQuery.forEach((countryObject: any) => {
    countryObject.totalcount = Number(countryObject.totalcount);
  });

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

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
Response:
Copy code
prisma:query SELECT Country, COUNT(country) as totalCount
FROM "public"."Customers" GROUP BY Country
[
  { country: 'USA', totalcount: 4n },
  { country: 'Canada', totalcount: 2n }
]
Before Conversion: Typeof Count: bigint
[
  { country: 'USA', totalcount: 4 },
  { country: 'Canada', totalcount: 2 }
]
After Conversion: Typeof Count: number
e
And I will stay with precision? I mean if I get big number if it will cut data?
n
Integers can store data up to 2^53 precisely, so till 9007199254740991 you shouldn’t lose any data
👍 1
d
I didn't realize that _count could return BigInt... is there a cut-off we should know about when it returns number versus BigInt?
n
If you are using Raw Query after Prisma version 4.0 then the numbers will always be of BigInt type.