Database
Set up a PostgreSQL database in ShipAhead using Drizzle ORM to manage data like users and sessions.
Tools Used
- Drizzle ORM: Provides a lightweight, type-safe way to interact with PostgreSQL databases.
Setup & Configuration
- Sign up for a PostgreSQL database at Supabase, Neon, or another provider.
- Copy the Database URL from your provider’s dashboard (e.g.,
postgres://user:password@host:port/dbname). - Set environment variable:
.env.local
DATABASE_URL="your-postgresql-database-url" - Generate schema migrations:
terminal
npm run db:generate - Apply migrations to your database:
terminal
npm run db:migrate
Usage
Database Connection: The database is initialized in
server/database/init.jsusingDATABASE_URLand connects to tables defined inserver/database/schema.js.Querying Data: Use Drizzle ORM in server routes or APIs. Example:
javascript// server/api/users.get.js import { getDatabase } from '~~/server/database/init'; import { user } from '~~/server/database/schema'; export default defineEventHandler(async () => { const db = getDatabase(); return await db.select().from(user); });Managing Schemas: Update
server/database/schema.jsto add or modify tables. Runnpm run db:generateandnpm run db:migrateafter changes.Verify Setup: Check your database provider’s dashboard (e.g., Supabase or Neon) to confirm tables are created. For more details, visit Drizzle ORM Documentation.