- Home
- Skills
- Andrelandgraf
- Fullstackrecipes
- Using Drizzle Queries
using-drizzle-queries_skill
- TypeScript
8
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill andrelandgraf/fullstackrecipes --skill using-drizzle-queries- SKILL.md3.2 KB
Overview
This skill teaches how to write type-safe database queries using Drizzle ORM in a TypeScript full-stack app. It covers selecting, inserting, updating, deleting, relational queries, and adding new tables with practical code examples. The guidance is geared toward production-ready patterns and easy integration into feature libraries. Expect clear snippets for common CRUD operations and relations.
How this skill works
The skill shows how to use Drizzle's query API and typed table schemas to perform database operations via a shared db client. It demonstrates select(), insert(), update(), delete(), and db.query.* relational patterns, including ordering and limiting results. It also walks through adding a new table schema, importing it into the central schema object, and generating migrations. Code examples use TypeScript types and Drizzle helpers (eq, desc, orderBy).
When to use it
- Building or maintaining server-side data access in a TypeScript app
- Implementing type-safe CRUD operations with Postgres and Drizzle ORM
- Adding relationships and eager-loading related rows in queries
- Introducing new tables/features and wiring them into migrations
- Ensuring compile-time safety for SQL-like operations
Best practices
- Define table schemas per feature and export them from feature folders
- Centralize schemas in the DB client so migrations and queries share types
- Prefer db.query.* for relational queries to eager-load relations with ordering
- Use returning() on inserts when you need the created row with generated fields
- Wrap writes in transactions when performing multiple related inserts/updates/deletes
Example use cases
- Fetch all user chats ordered by creation date with db.select().from(...).orderBy(desc(...))
- Insert a new chat and immediately return its generated id and timestamps using .insert(...).returning()
- Update a chat title with db.update().set(...).where(eq(...))
- Delete a chat by id with db.delete(...).where(eq(...))
- Add a new items table in a feature folder, import its schema into the central schema object, then generate and run migrations
FAQ
Use the relational API: db.query.chats.findFirst({ where: eq(chats.id, id), with: { messages: { orderBy: (m, { asc }) => [asc(m.createdAt)] } } }) to eager-load ordered messages.
How do I add a new table to the project?
Create a pgTable schema in the feature folder, export it, merge it into the central schema object in the DB client, then run the generate and migrate commands to produce and apply migrations.