← スキル一覧に戻る

using-drizzle-queries
by andrelandgraf
A Shadcn registry and collection of production-ready patterns and step-by-step guides (recipes) for full stack web AI apps
⭐ 6🍴 1📅 2026年1月23日
SKILL.md
name: using-drizzle-queries description: Write type-safe database queries with Drizzle ORM. Covers select, insert, update, delete, relational queries, and adding new tables.
Working with Drizzle
Write type-safe database queries with Drizzle ORM. Covers select, insert, update, delete, relational queries, and adding new tables.
Implement Working with Drizzle
Write type-safe database queries with Drizzle ORM. Covers select, insert, update, delete, relational queries, and adding new tables.
See:
- Resource:
using-drizzle-queriesin Fullstack Recipes - URL: https://fullstackrecipes.com/recipes/using-drizzle-queries
Writing Queries
Use Drizzle's query API for type-safe database operations:
import { db } from "@/lib/db/client";
import { chats } from "@/lib/chat/schema";
import { eq, desc } from "drizzle-orm";
// Select all
const allChats = await db.select().from(chats);
// Select with filter
const userChats = await db
.select()
.from(chats)
.where(eq(chats.userId, userId))
.orderBy(desc(chats.createdAt));
// Select single record
const chat = await db
.select()
.from(chats)
.where(eq(chats.id, chatId))
.limit(1)
.then((rows) => rows[0]);
Inserting Data
import { db } from "@/lib/db/client";
import { chats } from "@/lib/chat/schema";
// Insert single record
const [newChat] = await db
.insert(chats)
.values({
userId,
title: "New Chat",
})
.returning();
// Insert multiple records
await db.insert(messages).values([
{ chatId, role: "user", content: "Hello" },
{ chatId, role: "assistant", content: "Hi there!" },
]);
Updating Data
import { db } from "@/lib/db/client";
import { chats } from "@/lib/chat/schema";
import { eq } from "drizzle-orm";
await db
.update(chats)
.set({ title: "Updated Title" })
.where(eq(chats.id, chatId));
Deleting Data
import { db } from "@/lib/db/client";
import { chats } from "@/lib/chat/schema";
import { eq } from "drizzle-orm";
await db.delete(chats).where(eq(chats.id, chatId));
Using Relational Queries
For queries with relations, use the query API:
import { db } from "@/lib/db/client";
const chatWithMessages = await db.query.chats.findFirst({
where: eq(chats.id, chatId),
with: {
messages: {
orderBy: (messages, { asc }) => [asc(messages.createdAt)],
},
},
});
Adding New Tables
- Create the schema in the feature's library folder:
// src/lib/feature/schema.ts
import { pgTable, text, uuid, timestamp } from "drizzle-orm/pg-core";
export const items = pgTable("items", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
- Import the schema in
src/lib/db/client.ts:
import * as itemSchema from "@/lib/feature/schema";
const schema = {
...authSchema,
...chatSchema,
...itemSchema,
};
- Generate and run migrations:
bun run db:generate
bun run db:migrate
References
スコア
総合スコア
60/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
✓説明文
100文字以上の説明がある
+10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です