We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Drizzle ORM

Drizzle is a ORM and migration tool written in TypeScript. Its API and syntax is very similar to SQL, making it a perfect fit for this project (we wanna stay close to the raw SQL).

What Is a Migration?

A migration is just a set of changes to your database table. You can have as many migrations as needed as your requirements change over time. For example, one migration might create a new table, one might delete a column, and one might add 2 new columns.

An "up" migration moves the state of the database from its current schema to the schema that you want. So, to get a "blank" database to the state it needs to be ready to run your application, you run all the "up" migrations.

If something breaks, you can run one of the "down" migrations to revert the database to a previous state. "Down" migrations are also used if you need to reset a local testing database to a known state.

Assignment

npm i drizzle-orm postgres
npm i -D drizzle-kit
    • id: a UUID that will serve as the primary key
    • created_at: a TIMESTAMP that can not be null
    • updated_at: a TIMESTAMP that can not be null
    • name: a unique string that can not be null
import { pgTable, timestamp, uuid, text } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  id: uuid("id").primaryKey().defaultRandom().notNull(),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  updatedAt: timestamp("updated_at")
    .notNull()
    .defaultNow()
    .$onUpdate(() => new Date()),
  name: text("name").notNull().unique(),
});

The $onUpdate function sets the updatedAt field to a default value whenever the row is updated.

protocol://username:password@host:port/database

Here are examples:

  • macOS (no password, your username): postgres://wagslane:@localhost:5432/gator
  • Linux (password from last lesson, postgres user): postgres://postgres:postgres@localhost:5432/gator

Test your connection string by running psql, for example:

psql "postgres://wagslane:@localhost:5432/gator"

It should connect you to the gator database directly. If it's working, great. exit out of psql and save the connection string.

protocol://username:password@host:port/database?sslmode=disable

Your application code needs to know to not try to use SSL locally.

import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "src/<path_to_schema>",
  out: "src/<path_to_generated_files>",
  dialect: "postgresql",
  dbCredentials: {
    url: "your_connection_string",
  },
});

You can use the readConfig function to read the connection string from the .gatorconfig.json file instead of hard-coding it.

I created a src/lib/db directory to hold all of my database-related files. You can do the same, or put them wherever you like.

npx drizzle-kit generate
npx drizzle-kit migrate

Submit the CLI tests.

Tip

  • You can add scripts to your package.json to make running drizzle-kit easier. For example:
{
  "scripts": {
    "generate": "drizzle-kit generate",
    "migrate": "drizzle-kit migrate"
  }
}