Relational Queries
Objective
- Prisma queries.
- Cascade delete and update.
- Relation queries, projections, and inclusions.
- Filtering, sorting, and full-text search.
- Aggregation, grouping, and summarization.
1 Bank Accounts
-
Extend the schema from 10.2 Bank Accounts to include clients and link them to their accounts.
prisma/schema.prismamodel Client { } model Account { accountClient Client @relation(fields: [client], references: [id], onDelete: Cascade, onUpdate: Cascade) client String } model Transaction { transactionAccount Account @relation(fields: [account], references: [id], onDelete: Cascade, onUpdate: Cascade) account String }
-
Create a data repository service,
repos/clients.js
, with CRUD methods, and update the other services, if required:repos/clients.jsimport prisma from "@/repos/prisma";
-
Extend the data repository services with the following methods:
getAccountBalanceAvg()
returns the average account balances by type.getAccountMinMax()
returns the accounts with the smallest and largest balances.getAccountTop3()
returns the top three accounts with the largest balance.getTransactionAccount(id, from, to)
returns the transactions for a particular account within a date range.getTransactionTotal(from, to)
return the total amount of withdrawals and deposits made within a date range.getTransactionAccountTotal(id, from, to)
returns the total amount of withdrawals and deposits for an account, made within a date range.getClientReport(id)
returns all the information about a certain client, including their accounts, and for each account, its corresponding transactions.getClientTop()
returns the client with the largest total balance.
2 Project Tracker
- Extend the schema from 10.1 Task Tracker to manage multiple projects, with each project having multiple tasks.
- Update the schema to be able to attach files to projects and tasks.
- Update the schema to be able to create tasks within other tasks.
- Show the total number of projects that are past their deadline.
- Show the total number of tasks that are past their deadline.
- Show for every project the total number of its tasks.
- Show for every task the total number of its subtasks.
Complete the 8 Posting Files guide to learn more about dealing with files.