Database

Learn how to use Supersaas Database in your site

Supersaas uses Drizzle ORM to provide a collection of database utilities for your site. While Drizzle ORM supports multiple databases, this documentation focuses on using SQLite with Turso, which is the preferred setup for Supersaas projects, but you can use any database you want, like PostgreSQL, MySQL, or SQLite.

Database Setup

Custom Database

The database connection is configured in server/utils/db.js

server/utils/db.js
import { createClient as createLibSQLClient } from "@libsql/client/http";
import { drizzle as drizzleLibSQL } from "drizzle-orm/libsql";

let _db = null;
export * as tables from "~/server/database/schema";

export const useDB = () => {
  if (!_db) {
    if (process.env.TURSO_DB_URL && process.env.TURSO_DB_TOKEN) {
      _db = drizzleLibSQL(
        createLibSQLClient({
          url: process.env.TURSO_DB_URL,
          authToken: process.env.TURSO_DB_TOKEN,
        }),
      );
    } else {
      throw new Error("No database configured for production or development");
    }
  }
  return _db;
};

Ensure that TURSO_DB_URL and TURSO_DB_TOKEN environment variables are set for the database connection to work.

NuxtHub Database

For NuxtHub, the same file is used but the database connection is configured differently.

server/utils/db.js
import { drizzle } from "drizzle-orm/d1";
export { sql, eq, and, or } from "drizzle-orm";

import * as schema from "../database/schema";

export const tables = schema;

export function useDB() {
  return drizzle(hubDatabase(), { schema });
}