npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@deox/drizzle-d1-utils

v0.0.3

Published

Drizzle D1 utils

Downloads

1,739

Readme

@deox/drizzle-d1-utils

A helper that builds a complete Drizzle Kit Config object pre-configured for a Cloudflare D1 database. It reads your Wrangler configuration to resolve the correct binding, handles local SQLite file creation automatically, and switches between local, remote, and preview modes with a single option.

Installation

npm install -D @deox/drizzle-d1-utils

drizzle-kit and wrangler are peer dependencies and must also be present in your project:

npm install -D drizzle-kit wrangler

When using a local database, Drizzle Kit also requires a SQLite driver. Install one of:

npm install -D better-sqlite3
# or
npm install -D @libsql/client

Quick Start

// drizzle.config.ts
import { drizzleD1Config } from "@deox/drizzle-d1-utils";

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  { binding: "DB" },
);

Run Drizzle Kit as normal:

npx drizzle-kit generate
npx drizzle-kit migrate

Modes

Local (default)

Reads the SQLite file path from your Wrangler config and passes it to Drizzle Kit as dbCredentials.url. This is the default for local development.

Requires a SQLite driver: install either better-sqlite3 or @libsql/client — see Installation.

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  { binding: "DB" },
);

If the SQLite file does not exist yet, the helper detects this and offers to run wrangler d1 execute --local interactively to create it. You will see a prompt like:

[!] SQLite file for D1 binding 'DB' does not exist.
[!] The file can be created by executing the following command:
    wrangler d1 execute MyDatabase --command='SELECT 1' --local
[!] Run this command to create it? (y/N)

Answering y creates the file and continues. Answering n throws an error and exits.

Remote

Targets the live Cloudflare D1 database via the HTTP API (d1-http driver). Requires accountId and apiToken.

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  {
    binding: "DB",
    remote: true,
    accountId: process.env.CF_ACCOUNT_ID,
    apiToken: process.env.CF_API_TOKEN,
  },
);

Tip: Store credentials in environment variables and never commit them to source control.

The remote option also inherits its default from the binding's own remote flag in the Wrangler config (if set), so you can configure it once in wrangler.toml rather than repeating it here.

Preview

Targets the preview_database_id declared in the Wrangler config instead of the production database_id. Can be combined with either local or remote mode.

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  { binding: "DB", preview: true },
);

API

drizzleD1Config(config, options?)

Builds and returns a complete Drizzle Kit Config.

| Parameter | Type | Description | | --------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | config | DrizzleKitConfig | Base Drizzle Kit config — schema, out, migrations, etc. The dialect, driver, and dbCredentials fields are injected automatically and must not be set here. | | options | DrizzleD1Options | D1-specific options (see below). All fields are optional. |

Returns a complete Config object ready to be the default export of drizzle.config.ts.


DrizzleD1Options

| Option | Type | Default | Description | | ------------- | --------- | ----------------------------------- | ----------------------------------------------------------------------------------------------------------------- | | binding | string | First D1 binding found | Name of the D1 binding as declared in the Wrangler config. | | environment | string | — | Wrangler environment name (e.g. "staging", "production"). | | configPath | string | — | Path to the Wrangler config file (wrangler.toml or wrangler.json). | | persistTo | string | — | Directory where the local D1 SQLite file is persisted. | | remote | boolean | Binding's remote flag, or false | When true, connects to the remote Cloudflare D1 database via the HTTP API. Requires accountId and apiToken. | | preview | boolean | false | When true, targets the preview_database_id instead of database_id. | | accountId | string | — | Cloudflare account ID. Required when remote is true. | | apiToken | string | — | Cloudflare API token with D1 read/write permissions. Required when remote is true. |


DrizzleKitConfig

A re-export of Drizzle Kit's Config type with dialect, driver, and dbCredentials omitted. Use this type when building the first argument to drizzleD1Config programmatically.

import type { DrizzleKitConfig } from "@deox/drizzle-d1-utils";

const base: DrizzleKitConfig = {
  schema: "./src/schema.ts",
  out: "./drizzle",
};

Error Reference

| Error | Cause | | ----------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | | Options accountId, apiToken are required when using remote database. Missing: ... | remote: true was set but one or both credentials were not provided. | | database_id is not set for D1 binding '...' | The resolved binding has no database_id in the Wrangler config. | | preview_database_id is not set for D1 binding '...' | preview: true was set but the binding has no preview_database_id. | | Could not find SQLite file for D1 binding '...' | Local mode: the SQLite file does not exist and could not be created (either wrangler was not found, or the user declined the prompt). | | Aborted by user. | The interactive prompt to create the SQLite file was declined. |


Console Output

Every run prints a summary of the resolved database before Drizzle Kit executes:

----------------------------------------------------------
  Using following D1 Database:
----------------------------------------------------------
  Binding        : DB
  Database name  : MyDatabase
  Database Id    : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  Mode           : LOCAL
                   (using local SQLite database)
  SQLite file    : .wrangler/state/v3/d1/miniflare-D1DatabaseObject/abc123.sqlite
----------------------------------------------------------

In remote mode, Mode shows REMOTE and the SQLite file line is omitted.


Examples

Multiple environments

// drizzle.config.ts
import { drizzleD1Config } from "@deox/drizzle-d1-utils";

const env = process.env.ENV ?? "development";

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  { binding: "DB", environment: env },
);

Remote with preview database

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  {
    binding: "DB",
    remote: true,
    preview: true,
    accountId: process.env.CF_ACCOUNT_ID,
    apiToken: process.env.CF_API_TOKEN,
  },
);

Custom Wrangler config path

export default drizzleD1Config(
  { schema: "./src/schema.ts", out: "./drizzle" },
  {
    binding: "DB",
    configPath: "./config/wrangler.toml",
    persistTo: "./.wrangler/state",
  },
);