SQL databases require you to specify a strict schema of entities youβre going to store upfront
and if (when) you need to change the shape of those entities - you will need to do it via schema migrations.
Thereβre multiple production grade ways of managing database migrations.
Drizzle is designed to perfectly suite all of them, regardless of you going database first or codebase first.
Database first is when your database schema is a source of truth. You manage your database schema either directly on the database or
via database migration tools and then you pull your database schema to your codebase application level entities.
Codebase first is when database schema in your codebase is a source of truth and is under version control. You declare and manage your database schema in JavaScript/TypeScript
and then you apply that schema to the database itself either with Drizzle, directly or via external migration tools.
How can Drizzle help?
Weβve built drizzle-kit - CLI app for managing migrations with Drizzle.
It is designed to let you choose how to approach migrations based on your current business demands.
It fits in both database and codebase first approaches, it lets you push your schema or generate SQL migration files or pull the schema from database.
It is perfect wether you work alone or in a team.
Now letβs pick the best option for your project:
Option 1
I manage database schema myself using external migration tools or by running SQL migrations directly on my database.
From Drizzle I just need to get current state of the schema from my database and save it as TypeScript schema file.
Expand details
Thatβs a database first approach. You have your database schema as a source of truth and
Drizzle letβs you pull database schema to TypeScript using drizzle-kit pull command.
Option 2
I want to have database schema in my TypeScript codebase,
I donβt wanna deal with SQL migration files.
I want Drizzle to βpushβ my schema directly to the database
Expand details
Thatβs a codebase first approach. You have your TypeScript Drizzle schema as a source of truth and
Drizzle letβs you push schema changes to the database using drizzle-kit push command.
Thatβs the best approach for rapid prototyping and weβve seen dozens of teams
and solo developers successfully using it as a primary migrations flow in their production applications.
Option 3
I want to have database schema in my TypeScript codebase,
I want Drizzle to generate SQL migration files for me and apply them to my database
Expand details
Thatβs a codebase first approach. You have your TypeScript Drizzle schema as a source of truth and
Drizzle letβs you generate SQL migration files based on your schema changes with drizzle-kit generate
and then apply them to the database with drizzle-kit migrate commands.
Option 4
I want to have database schema in my TypeScript codebase,
I want Drizzle to generate SQL migration files for me and I want Drizzle to apply them during runtime
Expand details
Thatβs a codebase first approach. You have your TypeScript Drizzle schema as a source of truth and
Drizzle letβs you generate SQL migration files based on your schema changes with drizzle-kit generate and then
you can apply them to the database during runtime of your application.
This approach is widely used for monolithic applications when you apply database migrations
during zero downtime deployment and rollback DDL changes if something fails.
This is also used in serverless deployments with migrations running in custom resource once during deployment process.
Option 5
I want to have database schema in my TypeScript codebase,
I want Drizzle to generate SQL migration files for me,
but I will apply them to my database myself or via external migration tools
Expand details
Thatβs a codebase first approach. You have your TypeScript Drizzle schema as a source of truth and
Drizzle letβs you generate SQL migration files based on your schema changes with drizzle-kit generate and then
you can apply them to the database either directly or via external migration tools.
Option 6
I want to have database schema in my TypeScript codebase,
I want Drizzle to output the SQL representation of my Drizzle schema to the console,
and I will apply them to my database via Atlas
Expand details
Thatβs a codebase first approach. You have your TypeScript Drizzle schema as a source of truth and
Drizzle letβs you export SQL statements based on your schema changes with drizzle-kit export and then
you can apply them to the database via external migration tools.