Basic Data Handling with Prisma and Drizzle ORM in Deno
📖 Scenario: You are building a simple Deno application to manage a list of books in a library. You want to use an ORM to interact with your database easily. This project will guide you through setting up data, configuring ORM options, writing core logic to fetch data, and completing the setup for Prisma and Drizzle ORM.
🎯 Goal: Build a basic Deno app that uses Prisma and Drizzle ORM to define a books data structure, configure connection settings, query the list of books, and finalize the ORM setup.
📋 What You'll Learn
Create a data structure for books with exact fields
Add a configuration variable for database connection
Write a query to fetch all books using ORM syntax
Complete the ORM setup with the final export or initialization
💡 Why This Matters
🌍 Real World
Many applications need to manage data stored in databases. ORMs like Prisma and Drizzle help developers write simpler code to interact with databases without writing raw SQL.
💼 Career
Understanding how to set up and use ORMs is important for backend developers working with databases in modern JavaScript and TypeScript environments like Deno.
Progress0 / 4 steps
1
Create the books data model
Create a constant called books as an array of objects with these exact entries: { id: 1, title: "Deno Basics", author: "Jane Doe" } and { id: 2, title: "Learning Prisma", author: "John Smith" }.
Deno
Hint
Use const books = [ ... ] with two objects inside the array.
2
Add database connection configuration
Add a constant called databaseUrl and set it to the string "postgresql://user:password@localhost:5432/library".
Deno
Hint
Use const databaseUrl = "postgresql://user:password@localhost:5432/library".
3
Write a query to fetch all books using Prisma syntax
Write an async function called getAllBooksPrisma that returns prisma.book.findMany(). Assume prisma is already imported and configured.
Deno
Hint
Define async function getAllBooksPrisma() and return prisma.book.findMany().
4
Complete Drizzle ORM setup with export
Export a constant called db by calling drizzle(client). Assume client is already created.
Deno
Hint
Use export const db = drizzle(client); to complete the setup.
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
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 D
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
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 A
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'?
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 C
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
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 B
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 });
}