Bird
Raised Fist0
Denoframework~10 mins

ORM options (Prisma, Drizzle) in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ORM options (Prisma, Drizzle)
Start: Choose ORM
↓
Define Schema
↓
Generate Client
↓
Use Client in Code
↓
Query Database
↓
End
↓
Define Schema with TS
↓
Use Drizzle ORM Functions
↓
Query Database
↓
End
This flow shows how you pick either Prisma or Drizzle ORM, define your schema, generate or use the client, then query the database.
Execution Sample
Deno
import { PrismaClient } from 'npm:@prisma/client';
const prisma = new PrismaClient();

async function main() {
  const users = await prisma.user.findMany();
  console.log(users);
}

main();
This code connects to the database using Prisma, fetches all users, and prints them.
Execution Table
StepActionEvaluationResult
1Import PrismaClientLoad Prisma ORM clientPrismaClient class ready
2Create prisma instancenew PrismaClient()prisma object created
3Call prisma.user.findMany()Fetch all users from DBReturns array of user objects
4Console.log(users)Print users arrayUsers printed to console
5End main()Program finishesNo errors, program ends
💡 All users fetched and printed, program ends normally
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
prismaundefinedPrismaClient instancePrismaClient instancePrismaClient instancePrismaClient instance
usersundefinedundefined[{id:1, name:'Alice'}, {id:2, name:'Bob'}][{id:1, name:'Alice'}, {id:2, name:'Bob'}][{id:1, name:'Alice'}, {id:2, name:'Bob'}]
Key Moments - 2 Insights
Why do we create a new PrismaClient instance before querying?
The prisma variable holds the connection and methods to query the database. Without creating it (Step 2), you cannot call findMany (Step 3).
What does prisma.user.findMany() return?
It returns an array of user objects from the database, as shown in Step 3 and tracked in the variable 'users'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'users' after Step 3?
Aundefined
BA PrismaClient instance
CAn array of user objects
DA database connection string
💡 Hint
Check the 'Result' column in Step 3 and variable_tracker for 'users' after Step 3
At which step is the PrismaClient instance created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in the execution_table for Step 2
If we skip creating the prisma instance, what happens when calling findMany?
AThrows an error because prisma is undefined
BReturns empty array
CIt works normally
DReturns a connection string
💡 Hint
Refer to variable_tracker and execution_table Step 2 and 3 about prisma initialization
Concept Snapshot
ORM options in Deno:
- Prisma: Define schema, generate client, use prisma client to query.
- Drizzle: Define schema in TypeScript, use Drizzle functions to query.
- Both simplify database access with typed queries.
- Always create ORM client instance before querying.
- Queries return data arrays or objects.
Full Transcript
This visual execution shows how to use ORM options Prisma and Drizzle in Deno. First, you choose an ORM. For Prisma, you define a schema, generate a client, then create a PrismaClient instance in code. You call methods like findMany to fetch data. The execution table traces importing PrismaClient, creating the client instance, querying users, and printing results. Variables like prisma and users change values step-by-step. Key moments clarify why the client instance is needed and what findMany returns. The quiz tests understanding of variable values and execution steps. The snapshot summarizes the main points: Prisma and Drizzle help access databases with typed queries, requiring client creation before querying.

Practice

(1/5)
1. What is the main purpose of using an ORM like Prisma or Drizzle in a Deno project?
easy
A. To manage server configurations and deployments
B. To create user interfaces for web applications
C. To write low-level network protocols
D. To interact with databases using simple and safe code instead of raw SQL

Solution

  1. Step 1: Understand what ORM stands for

    ORM means Object-Relational Mapping, which helps connect code with databases easily.
  2. Step 2: Identify ORM's role in Deno projects

    ORMs like Prisma and Drizzle let you write code to manage database data without writing raw SQL queries.
  3. Final Answer:

    To interact with databases using simple and safe code instead of raw SQL -> Option D
  4. Quick Check:

    ORM = Simple database code [OK]
Hint: ORMs simplify database work by hiding raw SQL [OK]
Common Mistakes:
  • Confusing ORM with UI libraries
  • Thinking ORM manages server setup
  • Assuming ORM handles network protocols
2. Which of the following is the correct way to import Prisma Client in a Deno project?
easy
A. import { PrismaClient } from "@prisma/client";
B. import PrismaClient from "prisma/client";
C. const PrismaClient = require("@prisma/client");
D. import prisma from "deno-prisma";

Solution

  1. Step 1: Recall Prisma Client import syntax

    Prisma Client is imported using ES module syntax with curly braces from "@prisma/client".
  2. Step 2: Check each option for correctness

    import { PrismaClient } from "@prisma/client"; uses correct ES import syntax. import PrismaClient from "prisma/client"; misses curly braces. const PrismaClient = require("@prisma/client"); uses CommonJS require which is not used in Deno. import prisma from "deno-prisma"; uses a wrong package name.
  3. Final Answer:

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

    Prisma import uses curly braces and correct package [OK]
Hint: Prisma uses named import from "@prisma/client" [OK]
Common Mistakes:
  • Using require() instead of import
  • Missing curly braces in import
  • Wrong package names
3. Given this Drizzle code snippet in Deno, what will be the output if the database has two users named 'Alice' and 'Bob'?
const users = await db.select().from(usersTable);
console.log(users.map(u => u.name));
medium
A. [{name: "Alice"}, {name: "Bob"}]
B. undefined
C. ["Alice", "Bob"]
D. SyntaxError

Solution

  1. Step 1: Understand the select query

    The code selects all rows from usersTable and stores them in users array.
  2. Step 2: Map user objects to their names

    users.map(u => u.name) creates a new array with only the name values from each user object.
  3. Final Answer:

    ["Alice", "Bob"] -> Option C
  4. Quick Check:

    Mapping user objects to names returns array of names [OK]
Hint: Mapping extracts just the names from user objects [OK]
Common Mistakes:
  • Expecting full objects instead of names array
  • Confusing map output with original array
  • Assuming syntax error due to async
4. You wrote this Prisma query in Deno but get an error: const users = await prisma.user.findMany(). What is the likely cause?
medium
A. You must import Drizzle instead of Prisma
B. You forgot to instantiate PrismaClient before using it
C. You need to use raw SQL instead of Prisma queries
D. The findMany method does not exist in Prisma

Solution

  1. Step 1: Check PrismaClient usage

    Before calling prisma.user.findMany(), you must create an instance: const prisma = new PrismaClient();
  2. Step 2: Understand error cause

    If prisma is not instantiated, calling findMany() causes an error because prisma is undefined.
  3. Final Answer:

    You forgot to instantiate PrismaClient before using it -> Option B
  4. Quick Check:

    Instantiate PrismaClient before queries [OK]
Hint: Always create PrismaClient instance before queries [OK]
Common Mistakes:
  • Assuming findMany doesn't exist
  • Trying raw SQL instead of ORM methods
  • Mixing Prisma and Drizzle imports
5. You want to use Drizzle ORM in Deno to insert a new user only if the username is not empty and then fetch all users. Which code snippet correctly applies this logic?
hard
A. if (username) { await db.insert(usersTable).values({ name: username }); } const allUsers = await db.select().from(usersTable);
B. await db.insert(usersTable).values({ name: username }); if (username) { const allUsers = await db.select().from(usersTable); }
C. const allUsers = await db.select().from(usersTable); if (username) { await db.insert(usersTable).values({ name: username }); }
D. await db.insert(usersTable).values({ name: username }); const allUsers = await db.select().from(usersTable);

Solution

  1. Step 1: Check condition before insert

    We must insert only if username is not empty, so the insert is inside the if (username) block.
  2. Step 2: Fetch all users after conditional insert

    After the conditional insert, we fetch all users with db.select().from(usersTable) and store in allUsers.
  3. Final Answer:

    if (username) { await db.insert(usersTable).values({ name: username }); } const allUsers = await db.select().from(usersTable); -> Option A
  4. Quick Check:

    Insert only if username, then fetch all users [OK]
Hint: Put insert inside if, then select all users [OK]
Common Mistakes:
  • Inserting without checking username
  • Fetching users before insert
  • Fetching users inside the if block
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