Count
You can use the count
method to count the number of records that match a query. It also allows to count non-null field values with an select
clause.
Click here to open an interactive playground.
count.ts
import { createClient } from './db';
import { createUsersAndPosts } from './utils';
async function main() {
const db = await createClient();
await createUsersAndPosts(db);
console.log('Count all posts');
console.log(await db.post.count());
console.log('Count published posts');
console.log(await db.post.count({ where: { published: true }}));
console.log('Count post fields fields');
console.log(
await db.post.count({ select: { _all: true, content: true }})
);
}
main();
To count relations, please use a find
API with the special _count
field as demonstrated in the Find section.