Enzot
08/12/2022, 9:17 AM100n
for BigInt? I wonder if I can somehow remove that n
automaticallyNurul
08/12/2022, 11:52 AMn
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
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:
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
Enzot
08/12/2022, 11:56 AMNurul
08/12/2022, 1:11 PMDavid Ilizarov
10/14/2022, 1:18 AMNurul
10/14/2022, 6:19 AM