Quick start
Lets build a quick start app with PostgreSQL
+ postgresjs
and run our first migration.
The first thing we need to do is to install drizzle-orm
and drizzle-kit
:
npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit
yarn add drizzle-orm postgres
yarn add -D drizzle-kit
pnpm add drizzle-orm postgres
pnpm add -D drizzle-kit
bun add drizzle-orm postgres
bun add -D drizzle-kit
Lets declare our schema.ts
:
📦 <project root>
├ ...
├ 📂 src
│ └ 📜 schema.ts
└ 📜 package.json
import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
email: text("email"),
password: text("password"),
role: text("role").$type<"admin" | "customer">(),
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at"),
});
Now lets add drizzle configuration file:
📦 <project root>
├ ...
├ 📂 src
├ 📜 drizzle.config.ts
└ 📜 package.json
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
out: "./drizzle",
});
Add generate
and migrate
commands to package.json
and run our first migrations generation:
{
"name": "first time?",
"version": "0.0.1",
"scripts": {
"generate": "drizzle-kit generate",
"migrate": "drizzle-kit migrate"
},
}
$ npm run generate
...
[✓] Your SQL migration file ➜ drizzle/0000_pale_mister_fear.sql 🚀
Done! We now have our first SQL migration file 🥳
📦 <project root>
├ 📂 drizzle
│ ├ 📂 _meta
│ └ 📜 0000_pale_mister_fear.sql
├ 📂 src
├ 📜 drizzle.config.ts
└ 📜 package.json
Now lets run our first migration to the database:
$ npm run migrate
That’s it, folks!
My personal congratulations 🎉