pickstack

Nuxt + PostgreSQL (Drizzle)

PostgreSQL with Drizzle ORM: a schema, migration commands, a todo example and a local docker-compose. This page shows exactly what pickstack adds to a Nuxt project for PostgreSQL (Drizzle): the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.

$ npm create pickstack@latest my-app -- --framework nuxt --modules drizzle --yes
Customize in the configurator

Packages it adds

PackageVersionType
drizzle-orm^0.45.4runtime
pg^8.23.1runtime
@types/pg^8.23.1dev
dotenv^18.0.10dev
drizzle-kit^0.31.11dev

New files

.env.example
DATABASE_URL=postgres://postgres:postgres@localhost:5432/my-app
app/pages/todos.vue
<script setup lang="ts">
const { data: todos, error, refresh } = await useFetch("/api/todos");
const title = ref("");

async function add() {
  await $fetch("/api/todos", { method: "POST", body: { title: title.value } });
  title.value = "";
  await refresh();
}
</script>

<template>
  <section class="page">
    <h1 class="title">Todos</h1>
    <form class="row" @submit.prevent="add">
      <input
        v-model="title"
        required
        maxlength="200"
        placeholder="What needs doing?"
      />
      <button class="btn" type="submit">Add</button>
    </form>
    <p v-if="error" class="text">
      The database isn't ready: run docker compose up -d, then npm run db:push.
    </p>
    <ul v-else>
      <li v-for="todo in todos" :key="todo.id">{{ todo.title }}</li>
    </ul>
  </section>
</template>
docker-compose.yml
# Local PostgreSQL for development: docker compose up -d
services:
  postgres:
    image: postgres:18
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: my-app
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql

volumes:
  postgres-data:
drizzle.config.ts
import "dotenv/config";
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  out: "./drizzle",
  schema: "./server/db/schema.ts",
  dialect: "postgresql",
  dbCredentials: {
    url:
      process.env.DATABASE_URL ??
      "postgres://postgres:postgres@localhost:5432/my-app",
  },
});
server/api/todos.get.ts
import { desc } from "drizzle-orm";
import { todos } from "../db/schema";

export default defineEventHandler(() =>
  db.select().from(todos).orderBy(desc(todos.createdAt)).limit(100),
);
server/api/todos.post.ts
import { todos } from "../db/schema";

export default defineEventHandler(async (event) => {
  const body = await readBody<{ title?: unknown }>(event);
  const title = typeof body?.title === "string" ? body.title.trim() : "";
  if (!title) {
    throw createError({ statusCode: 400, statusMessage: "title is required" });
  }
  const [todo] = await db
    .insert(todos)
    .values({ title: title.slice(0, 200) })
    .returning();
  setResponseStatus(event, 201);
  return todo;
});
server/db/schema.ts
import {
  boolean,
  integer,
  pgTable,
  timestamp,
  varchar,
} from "drizzle-orm/pg-core";

export const todos = pgTable("todos", {
  id: integer().primaryKey().generatedAlwaysAsIdentity(),
  title: varchar({ length: 200 }).notNull(),
  done: boolean().notNull().default(false),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});
server/utils/db.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

// Connects lazily: the first query opens a connection.
const pool = new Pool({
  connectionString:
    process.env.DATABASE_URL ??
    "postgres://postgres:postgres@localhost:5432/my-app",
});
// An idle connection can drop (e.g. Postgres restarts); without a listener that crashes the process.
pool.on("error", (error) =>
  console.error("Postgres pool error:", error.message),
);

export const db = drizzle({ client: pool });

Changes to existing files

app/app.vue (changed)
⋯ 3 unchanged lines     <nav class="nav">       <NuxtLink class="link" to="/">Home</NuxtLink>       <NuxtLink class="link" to="/about">About</NuxtLink>+      <NuxtLink class="link" to="/todos">Todos</NuxtLink>     </nav>     <NuxtPage />   </main>⋯ 2 unchanged lines
app/pages/about.vue (changed)
 <template>   <section class="page">     <h1 class="title">About</h1>+    <p class="text">+      Built with Nuxt 4, Vue 3, TypeScript, PostgreSQL, Drizzle ORM.+    </p>−    <p class="text">Built with Nuxt 4, Vue 3, TypeScript.</p>   </section> </template>  

Project files

Related templates