CRUD Operations
Introduction to CRUD
Complete guide to creating, reading, updating, and deleting data
The Prisma IDB client provides a comprehensive CRUD API that mirrors Prisma Client, making it easy to work with IndexedDB.
Overview
All CRUD operations are asynchronous and return typed results matching your Prisma schema. They support:
- Filtering: Query with
whereclauses - Sorting: Order results with
orderBy - Pagination: Use
skipandtake - Selection: Include/exclude fields with
selectandinclude - Transactions: Execute atomically across multiple stores
- Events: Mutations emit events for reactive updates
- Outbox: Changes queue for server sync (when enabled)
Operations
Create
- create - Insert a single record
- createMany - Insert multiple records
- createManyAndReturn - Batch insert with results
Read
- findMany - Query multiple records
- findFirst - Get first matching record
- findFirstOrThrow - First with error handling
- findUnique - Get by unique identifier
- findUniqueOrThrow - Unique with error handling
- count - Count matching records
- aggregate - Min, max, avg, sum across records
Update
- update - Modify a single record
- updateMany - Modify multiple records
Delete
- delete - Remove a single record
- deleteMany - Remove multiple records
Upsert
- upsert - Insert or update
Common Options
All mutation operations (create, update, delete, upsert) accept options:
{
tx?: IDBPTransaction, // Use existing transaction
silent?: boolean, // Don't emit events (default: false)
addToOutbox?: boolean, // Queue for sync (default: true if sync enabled)
}All read operations accept:
{
tx?: IDBPTransaction, // Use existing transaction (read-only)
}Examples
Simple Create and Read
// Create
const user = await client.user.create({
data: { name: "Alice", email: "alice@example.com" },
});
// Read
const users = await client.user.findMany();
const alice = await client.user.findUnique({ where: { email: "alice@example.com" } });With Relations
const user = await client.user.create({
data: {
name: "Alice",
profile: { create: { bio: "Hi!" } },
},
include: { profile: true },
});Filtering and Pagination
const todos = await client.todo.findMany({
where: { done: false, priority: { gt: 5 } },
orderBy: { createdAt: "desc" },
skip: 0,
take: 10,
});Aggregations
const stats = await client.todo.aggregate({
_count: true,
_avg: { priority: true },
_max: { priority: true },
});
console.log(`${stats._count} todos, avg priority: ${stats._avg.priority}`);See Also
- Events & Subscriptions - React to mutations
- Transactions - Atomic multi-operation
- Generated Output - Understanding the client