Back to Drizzle Orm

Quick start

src/content/docs/quick.mdx

latest1.9 KB
Original Source

import Npm from "@mdx/Npm.astro";

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> drizzle-orm postgres -D drizzle-kit </Npm>

Lets declare our schema.ts:

plaintext
šŸ“¦ <project root>
 ā”œ ...
 ā”œ šŸ“‚ src
 │ ā”” šŸ“œ schema.ts
 ā”” šŸ“œ package.json
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:

plaintext
šŸ“¦ <project root>
 ā”œ ...
 ā”œ šŸ“‚ src
 ā”œ šŸ“œ drizzle.config.ts
 ā”” šŸ“œ package.json
ts
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:

json
{
  "name": "first time?",
  "version": "0.0.1",
  "scripts": {
    "generate": "drizzle-kit generate",
    "migrate": "drizzle-kit migrate"
  }, 
}
shell
$ npm run generate
...

[āœ“] Your SQL migration file āžœ drizzle/20242409125510_pale_mister_fear/migration.sql šŸš€

Done! We now have our first SQL migration file 🄳

plaintext
šŸ“¦ <project root>
 ā”œ šŸ“‚ drizzle
 │ ā”œ šŸ“‚ 20242409125510_pale_mister_fear
 ā”œ šŸ“‚ src
 ā”œ šŸ“œ drizzle.config.ts
 ā”” šŸ“œ package.json

Now lets run our first migration to the database:

shell
$ npm run migrate

That's it, folks!

My personal congratulations šŸŽ‰