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.

From the root import, db and client are typed unknown; narrow them to the installed driver's types, as in the example above. The @neutron-build/data/drizzle subpath returns genuine Drizzle types when you pass an explicit profile:

import { createDrizzleDatabase } from "@neutron-build/data/drizzle";

const database = await createDrizzleDatabase({
  profile: { provider: "postgres", connectionString: process.env.DATABASE_URL! },
  schema,
});
// database.db is a PostgresJsDatabase<typeof schema>; database.close() closes it.

This package is the Drizzle interop path. For new application SQL, Neutron's own schema, query and migration surface is @neutron-build/sql, whose schema documents feed neutron migrate generate and neutron db push.

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.