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 --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
drizzle-orm | ^0.45.4 | runtime |
pg | ^8.23.1 | runtime |
@types/pg | ^8.23.1 | dev |
dotenv | ^18.0.10 | dev |
drizzle-kit | ^0.31.11 | dev |
New files
DATABASE_URL=postgres://postgres:postgres@localhost:5432/my-app
<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>
# 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:
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",
},
});
import { desc } from "drizzle-orm";
import { todos } from "../db/schema";
export default defineEventHandler(() =>
db.select().from(todos).orderBy(desc(todos.createdAt)).limit(100),
);
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;
});
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(),
});
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
⋯ 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
<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
.env.example.gitignoreREADME.mdapp/app.vueapp/assets/css/main.cssapp/components/CounterCard.vueapp/pages/about.vueapp/pages/index.vueapp/pages/todos.vuedocker-compose.ymldrizzle.config.tsnuxt.config.tspackage.jsonpublic/robots.txtserver/api/todos.get.tsserver/api/todos.post.tsserver/db/schema.tsserver/utils/db.tstsconfig.json