CRUD Operations
create
Insert a single record
Insert a new record into a model store.
const user = await client.user.create({
data: {
name: "Alice",
email: "alice@example.com",
},
});With Relations
Nested creates allow you to establish relationships in a single operation:
const user = await client.user.create({
data: {
name: "Alice",
email: "alice@example.com",
profile: {
create: {
bio: "Alice's bio",
},
},
},
});With Selection and Inclusion
Control what fields are returned:
// Only specific fields
const user = await client.user.create({
data: { name: "Alice", email: "alice@example.com" },
select: { id: true, name: true },
});
// Include relations
const user = await client.user.create({
data: { name: "Alice", email: "alice@example.com" },
include: { profile: true },
});With Options
const user = await client.user.create(
{
data: { name: "Alice", email: "alice@example.com" },
},
{
tx: transaction, // Use existing transaction
silent: false, // Emit events (default: false)
addToOutbox: true, // Queue for sync (default: true if sync is enabled)
}
);Generated IDs
If your model uses client-generated IDs (uuid(), cuid(), cuid(2)), you can:
// Explicitly provide
const user = await client.user.create({
data: {
id: crypto.randomUUID(),
name: "Alice",
},
});
// Or let the generator auto-generate
const user = await client.user.create({
data: {
name: "Alice",
},
// id is auto-generated if schema has @default()
});Multiple Relations
Create nested records across multiple relations:
const user = await client.user.create({
data: {
name: "Alice",
email: "alice@example.com",
profile: {
create: {
bio: "Bio",
settings: {
create: {
theme: "dark",
},
},
},
},
todos: {
create: [{ title: "Task 1" }, { title: "Task 2" }],
},
},
});Events
Create operations emit create events (unless silent: true):
client.user.subscribe(["create"], (event) => {
console.log("User created:", event.detail);
});
await client.user.create({
data: { name: "Alice" },
});
// Logs: "User created: { id: '...', name: 'Alice' }"See Also
- createMany - Insert multiple records
- Transactions - Atomic operations
- Events & Subscriptions - React to mutations