We're going to go through the steps for setting up a Postgres storage instance on Vercel. I want to setup a new database to enable another feature I am building. In a future post we will explore creating a simple integration with Anthrophic Claude and I want to have the ability to store user submissions and results.
Lets get started by installing the Vercel dependencies.
At this point you have a Postgres instance created and can start creating tables directly in Vercel. @vercel/postgres gives you some basic functionality to interact with the database but also recommends using one of their supported ORMs for application development. We're going to move on to setting up Drizzle to handle table schemas, migrations and use the Drizzle ORM for queries.
What is Drizzle?
"Drizzle ORM is a headless TypeScript ORM with a head. š²" If you aren't familiar with ORMs just know that they provide a abstraction layer between your application code and your database allowing you to interact with tables and make queries in code without requiring your write SQL queries directly. You can read more about the Drizzle ORM.
Setup and Configure Drizzle
You can follow the Drizzle with Vercel Guide used here. I include the changes made for my own application needs.
Next we're going to create our Drizzle config files, initial schema and some base queries for interacting with our database tables. My db directory ended up looking like the following.
Create your first tables by adding a database schema to db/schema.ts.
Now let's create a drizzle.config.ts file in the root of your project directory.
At this point we can generate our initial migration and push the schema to Vercel. I'm using the default configuration for Vercel that creates a development cloud instance of the database. You can also configure a local install of the database if you prefer and use a container service like docker to run a local instance.
Apply Changes to the Database
First let's generate a new migration from our schema changes.
npx drizzle-kit generate
You can see the migration content in your db/migrations folder to see what Drizzle is doing under the hood.
Now let's run our migration
npx drizzle-kit migrate
Making Queries
Our last step here before we can start writing components to interact with our database is to create some queries to INSERT, SELECT, UPDATE and DELETE our data.
We're going to add four files to a db/queries folder; insert.ts, select.ts, update.ts and delete.ts.
Whats Next?
Now that you have the basic queries needed to interact with your database you can start creating components that will take your user inputs, submit the information and store it in your database! Remember that any time you make changes to your schema you will need to generate a new migration and migrate the database to keep the changes in sync.
I'll cover more about creating and using components with queries in another post along with how to setup Anthropic Claude to assist in building structured lesson plans for independent learning.