ORMs help you work with databases using simple code instead of writing complex queries. Prisma and Drizzle make it easier to save, find, and change data in your app.
ORM options (Prisma, Drizzle) in Deno
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); // Example: Find all users const users = await prisma.user.findMany(); // Drizzle example import { drizzle } from 'drizzle-orm/deno-postgres'; import { Client } from 'deno-postgres'; const client = new Client({ /* connection options */ }); await client.connect(); const db = drizzle(client); // Example: Select all users const users = await db.select().from(usersTable);
Prisma uses a generated client based on your database schema.
Drizzle works directly with SQL-like queries but keeps type safety and simplicity.
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); // Get all posts const posts = await prisma.post.findMany();
import { drizzle } from 'drizzle-orm/deno-postgres'; import { Client } from 'deno-postgres'; const client = new Client({ /* config */ }); await client.connect(); const db = drizzle(client); const posts = await db.select().from(postsTable);
This Deno program uses Prisma to add a new user and then fetch all users from the database. It shows how simple it is to create and read data with Prisma.
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { // Create a new user const newUser = await prisma.user.create({ data: { name: 'Alice', email: 'alice@example.com' } }); // Fetch all users const users = await prisma.user.findMany(); console.log('New user created:', newUser); console.log('All users:', users); } main() .catch(e => console.error(e)) .finally(async () => { await prisma.$disconnect(); });
Prisma requires generating the client after defining your schema.
Drizzle is lightweight and works well with Deno's native Postgres client.
Both ORMs improve code readability and reduce errors compared to raw SQL.
ORMs like Prisma and Drizzle let you work with databases using simple code.
They help you avoid writing raw SQL and keep your code clean and safe.
Prisma is more schema-driven, while Drizzle is lightweight and flexible for Deno.
Practice
Solution
Step 1: Understand what ORM stands for
ORM means Object-Relational Mapping, which helps connect code with databases easily.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.Final Answer:
To interact with databases using simple and safe code instead of raw SQL -> Option DQuick Check:
ORM = Simple database code [OK]
- Confusing ORM with UI libraries
- Thinking ORM manages server setup
- Assuming ORM handles network protocols
Solution
Step 1: Recall Prisma Client import syntax
Prisma Client is imported using ES module syntax with curly braces from "@prisma/client".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.Final Answer:
import { PrismaClient } from "@prisma/client"; -> Option AQuick Check:
Prisma import uses curly braces and correct package [OK]
- Using require() instead of import
- Missing curly braces in import
- Wrong package names
const users = await db.select().from(usersTable); console.log(users.map(u => u.name));
Solution
Step 1: Understand the select query
The code selects all rows from usersTable and stores them in users array.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.Final Answer:
["Alice", "Bob"] -> Option CQuick Check:
Mapping user objects to names returns array of names [OK]
- Expecting full objects instead of names array
- Confusing map output with original array
- Assuming syntax error due to async
const users = await prisma.user.findMany(). What is the likely cause?Solution
Step 1: Check PrismaClient usage
Before calling prisma.user.findMany(), you must create an instance: const prisma = new PrismaClient();Step 2: Understand error cause
If prisma is not instantiated, calling findMany() causes an error because prisma is undefined.Final Answer:
You forgot to instantiate PrismaClient before using it -> Option BQuick Check:
Instantiate PrismaClient before queries [OK]
- Assuming findMany doesn't exist
- Trying raw SQL instead of ORM methods
- Mixing Prisma and Drizzle imports
Solution
Step 1: Check condition before insert
We must insert only if username is not empty, so the insert is inside the if (username) block.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.Final Answer:
if (username) { await db.insert(usersTable).values({ name: username }); } const allUsers = await db.select().from(usersTable); -> Option AQuick Check:
Insert only if username, then fetch all users [OK]
- Inserting without checking username
- Fetching users before insert
- Fetching users inside the if block
Ready to prove it?
Pass the test and this pattern moves unknown
