Skip to content

Latest commit

 

History

History
44 lines (27 loc) · 1.13 KB

File metadata and controls

44 lines (27 loc) · 1.13 KB

Prisma Multiple Relation Counts

This example show a way to fetch multiple counts of the same relation using different filtering criteria with Prisma. Specifically, it uses a total of 2 SQL queries to get a list of all users including:

  • The count of all posts for each user
  • The count of all posts with at least one comment for each user

The second query takes advantage of Prisma Client's built-in dataloader which automatically batches requests to findUnique.

How to use

Prerequisites

1. Download example & install dependencies

Clone this repository:

git clone git@github.com:sbking/prisma-multiple-relation-counts.git

Install dependencies:

npm install

2. Create an SQLite database and run migrations

Run the following command. An SQLite database will be created automatically:

npx prisma migrate dev --name init

3. Run the dev script

To run the script.ts file, run the following command:

npm run dev