Quick start
Lets build a quick start app with PostgreSQL + postgresjs and run our first migration
npm install drizzle-orm postgres
npm install -D drizzle-kitLets declare our schema.ts
π¦ <project root>
β ...
β π src
β β π schema.ts
β π package.jsonschema.ts
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.jsonimport type { Config } from "drizzle-kit";
export default {
schema: "./src/schema.ts",
out: "./drizzle",
} satisfies Config;Add generate command to package.json and run our first migrations generation
package.json
{
"name": "first time?",
"version": "0.0.1",
"scripts": {
"generate": "drizzle-kit generate:pg",
},
}terminal
$ 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.jsonNow lets run our first migration to the database
π¦ <project root>
β π drizzle
β π src
β β π schema.ts
β β π index.ts
β π drizzle.config.ts
β π package.jsonindex.ts
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
const connectionString = "..."
const sql = postgres(connectionString, { max: 1 })
const db = drizzle(sql);
await migrate(db, { migrationsFolder: "drizzle" });That's it, folks!
My personal congratulations π