Bird
Raised Fist0
NestJSframework~20 mins

Why Prisma offers type-safe database access in NestJS - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Prisma Type-Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does Prisma ensure type safety in database queries?
Prisma generates types based on your database schema. What is the main benefit of this for developers?
AIt automatically encrypts all database data for security.
BIt prevents runtime errors by catching type mismatches during development.
CIt allows writing raw SQL queries without any checks.
DIt disables type checking to improve performance.
Attempts:
2 left
💡 Hint
Think about how knowing data types early helps avoid mistakes.
❓ component_behavior
intermediate
2:00remaining
What happens if you try to assign a wrong type to a Prisma query result?
Consider this NestJS code using Prisma Client: const user = await prisma.user.findUnique({ where: { id: 1 } }); What will happen if you try to assign user.name (a string) to a variable declared as a number?
NestJS
const user = await prisma.user.findUnique({ where: { id: 1 } });
const age: number = user.name;
AThe code will run but user.name will be converted to a number automatically.
BThe app will crash at runtime with a type error.
CPrisma will ignore the type mismatch and assign the value.
DTypeScript will show a type error during development.
Attempts:
2 left
💡 Hint
Think about when TypeScript checks types.
📝 Syntax
advanced
2:00remaining
Which Prisma query correctly uses type-safe filtering for a user email?
You want to find a user by email using Prisma Client in NestJS. Which code snippet is type-safe and correct?
Aconst user = await prisma.user.findUnique({ where: { email: null } });
Bconst user = await prisma.user.findUnique({ where: { email: 12345 } });
Cconst user = await prisma.user.findUnique({ where: { email: 'test@example.com' } });
Dconst user = await prisma.user.findUnique({ where: { email: true } });
Attempts:
2 left
💡 Hint
Check the expected type of the email field in the schema.
🔧 Debug
advanced
2:00remaining
Why does this Prisma query cause a TypeScript error?
Look at this code snippet: const posts = await prisma.post.findMany({ where: { published: 'yes' } }); Why does TypeScript show an error here?
NestJS
const posts = await prisma.post.findMany({ where: { published: 'yes' } });
AThe 'published' field expects a boolean, but a string was given.
BThe 'published' field does not exist in the schema.
CPrisma Client does not support findMany queries.
DThe 'where' clause must be empty for findMany.
Attempts:
2 left
💡 Hint
Check the data type of the 'published' field in your Prisma schema.
❓ state_output
expert
2:00remaining
What is the output type of this Prisma query in NestJS?
Given this Prisma Client query: const user = await prisma.user.findUnique({ where: { id: 5 } }); What is the type of 'user' in TypeScript?
NestJS
const user = await prisma.user.findUnique({ where: { id: 5 } });
AUser | null
Bnull
CUser
Dany
Attempts:
2 left
💡 Hint
Consider what happens if no user with id 5 exists.

Practice

(1/5)
1. Why does Prisma provide type-safe database access in NestJS applications?
easy
A. To replace the need for SQL queries entirely
B. To make the database run faster
C. To automatically create database backups
D. To catch errors early by matching queries to the database schema

Solution

  1. Step 1: Understand Prisma's purpose

    Prisma checks that your code queries match the database structure to avoid mistakes.
  2. Step 2: Identify the benefit of type safety

    This early error detection helps developers write safer and more reliable code.
  3. Final Answer:

    To catch errors early by matching queries to the database schema -> Option D
  4. Quick Check:

    Type safety = early error detection [OK]
Hint: Type safety means catching mistakes before running code [OK]
Common Mistakes:
  • Thinking Prisma speeds up the database itself
  • Confusing type safety with backups
  • Believing Prisma removes all SQL usage
2. Which of the following is the correct way to import Prisma Client in a NestJS service for type-safe access?
easy
A. const { Prisma } = require('@prisma/client');
B. import { PrismaClient } from '@prisma/client';
C. import Prisma from 'prisma';
D. const PrismaClient = require('prisma-client');

Solution

  1. Step 1: Check official Prisma import syntax

    Prisma Client is imported using ES module syntax: import { PrismaClient } from '@prisma/client';
  2. Step 2: Identify incorrect options

    Options A, B, and C use wrong module names or CommonJS require syntax not recommended in NestJS with ES modules.
  3. Final Answer:

    import { PrismaClient } from '@prisma/client'; -> Option B
  4. Quick Check:

    Correct import syntax = import { PrismaClient } from '@prisma/client'; [OK]
Hint: Use ES module import syntax for Prisma Client [OK]
Common Mistakes:
  • Using require() instead of import
  • Wrong package names like 'prisma-client'
  • Destructuring Prisma instead of PrismaClient
3. Given this Prisma query in a NestJS service:
const user = await prisma.user.findUnique({ where: { id: 5 } });

What will happen if the id field does not exist in the database schema?
medium
A. The query will run but return null
B. The database will ignore the id filter
C. TypeScript will show a compile-time error
D. The application will crash at runtime

Solution

  1. Step 1: Understand Prisma's type safety

    Prisma generates types from the database schema, so invalid fields cause TypeScript errors before running.
  2. Step 2: Identify error timing

    Since id is missing, TypeScript will catch this mismatch during compilation, preventing runtime errors.
  3. Final Answer:

    TypeScript will show a compile-time error -> Option C
  4. Quick Check:

    Invalid field = compile-time error [OK]
Hint: Prisma type safety catches wrong fields before running code [OK]
Common Mistakes:
  • Assuming runtime crash instead of compile error
  • Thinking database ignores unknown fields silently
  • Believing query returns null without error
4. You wrote this Prisma query in NestJS:
const posts = await prisma.post.findMany({ where: { title: 'Hello' } });

But you get a TypeScript error: Property 'title' does not exist on type 'PostWhereInput'.
What is the most likely cause?
medium
A. The title field is missing in the Prisma schema for Post
B. You forgot to import PrismaClient
C. The database connection is not established
D. You used findMany instead of findUnique

Solution

  1. Step 1: Analyze the TypeScript error

    The error says title is not a valid filter field, meaning it is not defined in the Prisma schema for Post.
  2. Step 2: Rule out other options

    Missing import or connection issues cause different errors; findMany is valid here.
  3. Final Answer:

    The title field is missing in the Prisma schema for Post -> Option A
  4. Quick Check:

    Missing schema field = TypeScript error [OK]
Hint: Check Prisma schema fields if TypeScript errors on filters [OK]
Common Mistakes:
  • Blaming database connection for type errors
  • Confusing method names causing this error
  • Ignoring schema updates after changes
5. In a NestJS app using Prisma, you want to ensure your database queries are type-safe and reflect the latest schema changes. Which approach best achieves this?
hard
A. Run prisma generate after updating the Prisma schema file
B. Manually write TypeScript types for your database models
C. Use raw SQL queries inside Prisma without type checks
D. Disable type checking in your IDE

Solution

  1. Step 1: Understand Prisma's code generation

    Running prisma generate updates the generated client with types matching the current schema.
  2. Step 2: Evaluate other options

    Manual types risk errors; raw SQL skips type safety; disabling checks removes benefits.
  3. Final Answer:

    Run prisma generate after updating the Prisma schema file -> Option A
  4. Quick Check:

    Generate client = updated type safety [OK]
Hint: Always run prisma generate after schema changes [OK]
Common Mistakes:
  • Writing manual types instead of generating
  • Ignoring schema updates in code
  • Disabling helpful type checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard