Database client
Use database client to interact with database
The database client is an instance of the Drizzle client, automatically typed based on the defined schema. It is exposed as the db object and used throughout the application to interact with the database.
This guide covers how to initialize the client and perform basic operations such as querying, creating, updating, and deleting records. For more details, refer to the official Drizzle documentation.
Initializing the clinet
Pass the validated DATABASE_URL to the client to initialize it.
Now it's exported from the db/index.ts
package and can be used across the codebase (server-side).
Querying data
In tRPC procedures, the db instance from Drizzle ORM is used to interact with the database. Since Drizzle provides fully typed queries, it ensures type safety across the backend and frontend when combined with tRPC.
A typical tRPC query procedure retrieves data by calling db inside a resolver function. This allows fetching, filtering, and returning database records efficiently while keeping the API strongly typed.
Mutating data
You can use the exported utilities to mutate data. Insert, update or delete records in fast and fully type-safe way:
How is this guide?