Documentation Index
Fetch the complete documentation index at: https://mintlify.com/drizzle-team/drizzle-orm/llms.txt
Use this file to discover all available pages before exploring further.
Query builder APIs for SELECT, INSERT, UPDATE, and DELETE operations
Documentation Index
Fetch the complete documentation index at: https://mintlify.com/drizzle-team/drizzle-orm/llms.txt
Use this file to discover all available pages before exploring further.
from<TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(
source: TFrom
): SQLiteSelectQuery
const result = await db.select().from(users);
where(condition: SQL | undefined): this
import { eq } from 'drizzle-orm';
const result = await db
.select()
.from(users)
.where(eq(users.id, 1));
orderBy(...columns: (SQLiteColumn | SQL)[]): this
import { asc } from 'drizzle-orm';
const result = await db
.select()
.from(users)
.orderBy(users.name);
// or explicitly:
.orderBy(asc(users.name));
limit(limit: number | Placeholder): this
const result = await db
.select()
.from(users)
.limit(10);
offset(offset: number | Placeholder): this
const result = await db
.select()
.from(users)
.limit(10)
.offset(20);
groupBy(...columns: (SQLiteColumn | SQL)[]): this
import { count } from 'drizzle-orm';
const result = await db
.select({
role: users.role,
count: count(),
})
.from(users)
.groupBy(users.role);
having(condition: SQL | undefined): this
import { count, gt } from 'drizzle-orm';
const result = await db
.select({
role: users.role,
count: count(),
})
.from(users)
.groupBy(users.role)
.having(gt(count(), 5));
leftJoin<TJoinedTable>(
table: TJoinedTable,
on: SQL | undefined
): this
const result = await db
.select()
.from(users)
.leftJoin(posts, eq(users.id, posts.authorId));
rightJoin<TJoinedTable>(
table: TJoinedTable,
on: SQL | undefined
): this
const result = await db
.select()
.from(users)
.rightJoin(posts, eq(users.id, posts.authorId));
innerJoin<TJoinedTable>(
table: TJoinedTable,
on: SQL | undefined
): this
const result = await db
.select()
.from(users)
.innerJoin(posts, eq(users.id, posts.authorId));
fullJoin<TJoinedTable>(
table: TJoinedTable,
on: SQL | undefined
): this
const result = await db
.select()
.from(users)
.fullJoin(posts, eq(users.id, posts.authorId));
const result = await db
.select({
userId: users.id,
userName: users.name,
postTitle: posts.title,
})
.from(users)
.leftJoin(posts, eq(users.id, posts.authorId));
union<T extends AnySQLiteSelect>(other: T): SQLiteSetOperator
const activeUsers = db.select().from(users).where(eq(users.active, true));
const adminUsers = db.select().from(users).where(eq(users.role, 'admin'));
const result = await activeUsers.union(adminUsers);
unionAll<T extends AnySQLiteSelect>(other: T): SQLiteSetOperator
const result = await query1.unionAll(query2);
intersect<T extends AnySQLiteSelect>(other: T): SQLiteSetOperator
const result = await query1.intersect(query2);
except<T extends AnySQLiteSelect>(other: T): SQLiteSetOperator
const allUsers = db.select().from(users);
const inactiveUsers = db.select().from(users).where(eq(users.active, false));
const activeUsers = await allUsers.except(inactiveUsers);
values(
value: SQLiteInsertValue<TTable>
): SQLiteInsertBase
values(
values: SQLiteInsertValue<TTable>[]
): SQLiteInsertBase
await db.insert(users).values({
name: 'Alice',
email: 'alice@example.com',
});
select(selectQuery: SQLiteSelectQuery): SQLiteInsertBase
await db.insert(usersCopy)
.select(
db.select().from(users).where(eq(users.active, true))
);
onConflictDoNothing(config?: { target?: IndexColumn | IndexColumn[] }): this
await db.insert(users)
.values({ id: 1, name: 'Alice' })
.onConflictDoNothing();
onConflictDoUpdate(config: {
target: IndexColumn | IndexColumn[];
set: SQLiteUpdateSetSource<TTable>;
where?: SQL;
targetWhere?: SQL;
setWhere?: SQL;
}): this
import { sql } from 'drizzle-orm';
await db.insert(users)
.values({ email: 'alice@example.com', name: 'Alice' })
.onConflictDoUpdate({
target: users.email,
set: { name: sql`excluded.name` },
});
returning(): SQLiteInsert
returning<TSelectedFields>(
fields: TSelectedFields
): SQLiteInsert
const result = await db.insert(users)
.values({ name: 'Alice' })
.returning();
// [{ id: 1, name: 'Alice', email: null, ... }]
set(values: SQLiteUpdateSetSource<TTable>): this
await db.update(users)
.set({ name: 'Alice Updated' })
.where(eq(users.id, 1));
where(condition: SQL | undefined): this
await db.update(users)
.set({ active: false })
.where(eq(users.email, 'user@example.com'));
returning(): SQLiteUpdate
returning<TSelectedFields>(
fields: TSelectedFields
): SQLiteUpdate
const result = await db.update(users)
.set({ active: true })
.where(eq(users.id, 1))
.returning({ id: users.id, active: users.active });
from<TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(
source: TFrom
): this
await db.update(users)
.set({ role: 'premium' })
.from(subscriptions)
.where(and(
eq(users.id, subscriptions.userId),
eq(subscriptions.status, 'active')
));
where(condition: SQL | undefined): this
await db.delete(users)
.where(eq(users.id, 1));
returning(): SQLiteDelete
returning<TSelectedFields>(
fields: TSelectedFields
): SQLiteDelete
const deleted = await db.delete(users)
.where(eq(users.id, 1))
.returning();
console.log('Deleted user:', deleted[0]);
orderBy(...columns: (SQLiteColumn | SQL)[]): this
import { desc } from 'drizzle-orm';
// Delete oldest 10 inactive users
await db.delete(users)
.where(eq(users.active, false))
.orderBy(users.createdAt)
.limit(10);
limit(limit: number | Placeholder): this
// Delete up to 100 old records
await db.delete(logs)
.where(lt(logs.createdAt, thirtyDaysAgo))
.limit(100);
prepare(): SQLitePreparedQuery
const prepared = db
.select()
.from(users)
.where(eq(users.id, placeholder('id')))
.prepare();
const user1 = await prepared.get({ id: 1 });
const user2 = await prepared.get({ id: 2 });
$dynamic(): this
let query = db.select().from(users).$dynamic();
if (filters.role) {
query = query.where(eq(users.role, filters.role));
}
if (filters.minAge) {
query = query.where(gte(users.age, filters.minAge));
}
if (orderBy === 'name') {
query = query.orderBy(users.name);
} else if (orderBy === 'age') {
query = query.orderBy(users.age);
}
const result = await query;