Setup

Astro DB is powered by Drizzle!
... and we regret everything omg this thing sucks
- From Drizzles official page

A Simple Example

Let's create a simple Drizzle script that will declare a task table and read all the tasks from the table.

Create a new supabase database and recreate the task table from the SQL chapter. Don't add the project IDs and table yet, that will follow later.

Create a new TypeScript project:

pnpm init
pnpm add typescript tsx --save-dev
pnpm tsc --init

Install Drizzle:

pnpm add drizzle-orm postgres
pnpm add drizzle-kit --save-dev

Create the following file demo.ts:

import { pgTable, serial, text, integer, timestamp, varchar } from 'drizzle-orm/pg-core';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

// Paste the supabase URI here
const databaseURI = '...';

// Declare the task table
export const taskTable = pgTable('task', {
  id: serial('id').primaryKey(),
  title: varchar('title', { length: 255 }).notNull(),
  description: text('description').notNull(),
  status: varchar('status', { length: 255 }).notNull(),
  duration: integer('duration'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

const client = postgres(databaseURI);
const db = drizzle(client);

async function getTasks() {
  return await db.select().from(taskTable);
}

getTasks().then(console.log);

Note that the check constraint is not yet implemented in Drizzle at the time of this writing.

Execute the file:

pnpm tsx demo.ts

You will see a list of all the tasks that are currently present in the table.

Drizzle as Typesafe SQL

Did you notice how similar the Drizzle function and the SQL statement are? The Drizzle function is:

db.select().from(taskTable);

The SQL function was:

select * from task;

This similarity is intentional and will be a major theme in this chapter. Unlike many other frameworks which try to "abstract" SQL away, Drizzle embraces SQL and only adds a bit of type safety on top of it.

If you know SQL, learning Drizzle is a very fast process.