Queries
All query methods are accessed directly from the database instance using the table name.
find
Find all rows matching the query options.
// All users
const users = await db.users.find();
// With conditions
const adults = await db.users.find({
where: { age: { gte: 18 } },
orderBy: { name: "asc" },
take: 10,
skip: 0,
});findMany
Alias for find.
const users = await db.users.findMany({ where: { active: true } });findFirst
Find the first row matching the query options. Returns null if no match.
const user = await db.users.findFirst({ where: { email: "alice@example.com" } });insert
Insert a single row and return the inserted data.
const user = await db.users.insert({
name: "Alice",
email: "alice@example.com",
age: 25,
});insertMany
Insert multiple rows at once.
const users = await db.users.insertMany([
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
]);update
Update rows matching the where clause. Returns the updated rows.
const updated = await db.users.update({
where: { name: "Alice" },
data: { age: 26 },
});WARNING
A where clause is required. To update all rows, use a condition that matches everything.
delete
Delete rows matching the where clause. Returns the number of deleted rows.
const count = await db.users.delete({ where: { active: false } });WARNING
A where clause is required.
count
Count rows matching the conditions.
const total = await db.users.count();
const activeCount = await db.users.count({ where: { active: true } });exists
Check if any row matches the where clause.
const hasAdmin = await db.users.exists({ where: { role: "admin" } });upsert
Insert if not found, update if found.
const user = await db.users.upsert({
where: { email: "alice@example.com" },
create: { name: "Alice", email: "alice@example.com", age: 25 },
update: { age: 26 },
});truncate
Remove all rows from a table.
await db.users.truncate();Where Conditions
Simple equality
where: { name: "Alice" }Comparison operators
where: {
age: {
eq: 25, // Equal
neq: 25, // Not equal
gt: 18, // Greater than
gte: 18, // Greater than or equal
lt: 65, // Less than
lte: 65, // Less than or equal
}
}String matching
where: { name: { like: "%alice%" } }
where: { name: { notLike: "%test%" } }IN / NOT IN
where: { id: { in: [1, 2, 3] } }
where: { status: { notIn: ["banned", "deleted"] } }NULL checks
where: { deletedAt: { isNull: true } }
where: { email: { isNull: false } }BETWEEN
where: { age: { between: [18, 65] } }OR / AND
where: {
OR: [
{ name: "Alice" },
{ name: "Bob" },
]
}
where: {
AND: [
{ age: { gte: 18 } },
{ active: true },
]
}Query Options
await db.users.find({
where: { active: true }, // Filter conditions
select: ["id", "name"], // Select specific columns
orderBy: { name: "asc" }, // Sort order
take: 10, // Limit results
skip: 20, // Offset for pagination
with: { posts: true }, // Load relations
});Raw SQL
Execute raw SQL queries when you need full control:
const rows = await db.raw("SELECT * FROM users WHERE age > ?", [18]);Transactions
Wrap multiple operations in a transaction:
await db.transaction(async () => {
await db.raw("INSERT INTO users (name) VALUES (?)", ["Alice"]);
await db.raw("INSERT INTO posts (userId, title) VALUES (?, ?)", [1, "Hello"]);
});