Skip to main content
Version: 3.0 Beta

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.

Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below