Skip to content

Database

Nuxt SaaS Kit leverages Drizzle ORM to be able to interact with the database for your application.

NOTE

Nuxt SaaS Kit uses postgres by default but you can switch to other SQL databases, such as SQLite or MySQL, based on your project’s needs.

Setup

To get started, create your database connection URI from any provider of your choice. The URI typically includes username, password, host, port, and database name.

Example Postgres URI

sh
# The database URL is used to connect to your database.
$ postgres://your_username:your_db_password@your_host:5432/your_database_name

Update your .env file with your database connection string:

DATABASE_URL="your_db_connection_string"

Configuration

Drizzle manages database configuration through drizzle.config.ts. This file defines how Drizzle ORM connects to your database and handles migrations and schema generation.

ts
// drizzle.config.ts
import process from 'node:process'
import 'dotenv/config'
import { defineConfig } from 'drizzle-kit'

if (!process.env.DATABASE_URL) {
  throw new Error('DATABASE_URL is missing')
}

export default defineConfig({
  schema: './server/_/database/schema.ts',
  out: './server/_/database/migrations',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL as string,
  },
  strict: true,
})

Drizzle Studio

Drizzle provides you also with an interactive UI where you can explore your database and test queries called Studio.

To run the studio, you can use the following command:

sh
$ npm db:studio
sh
$ pnpm db:studio
sh
$ yarn db:studio

This will start the studio on https://local.drizzle.studio.