Database and Drizzle

View as Markdown

createDrizzleDatabase() from @neutron-build/data resolves a database profile and creates the matching Drizzle client.

Install a driver

For PostgreSQL or Nucleus SQL:

npm install @neutron-build/data drizzle-orm postgres

For SQLite:

npm install @neutron-build/data drizzle-orm @libsql/client

Connect

import { createDrizzleDatabase } from "@neutron-build/data";
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import * as schema from "./schema";

const database = await createDrizzleDatabase({
  config: { database: "postgres" },
  schema,
});
const db = database.db as PostgresJsDatabase<typeof schema>;

try {
  const users = await db.select().from(schema.users);
} finally {
  await database.close();
}

The returned object contains:

  • profile — the selected provider and connection string.
  • db — the Drizzle database instance.
  • client — the underlying Postgres or libSQL client.
  • nucleus — the optional multi-model client when using Nucleus.
  • close() — closes the created clients.

The db and client fields are currently exposed as unknown; narrow them to the installed driver's types, as in the example above.

Select a provider

Pass a provider explicitly:

const database = await createDrizzleDatabase({
  config: { database: "postgres" },
  schema,
});

Without an explicit provider, selection checks NUCLEUS_URL, then DATABASE_URL, then uses .neutron/dev.db with SQLite. Nucleus uses the PostgreSQL driver for SQL and optionally creates an @neutron-build/nucleus client for its other data models.