iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤶

Notes on using Drizzle Studio and D1 Driver locally

に公開

Hello, I am (@sugar235711).
This article is for Day 24 of the Cloudflare Advent Calendar 2023.
https://qiita.com/advent-calendar/2023/cloudflare

Introduction

This article introduces an issue encountered when using libraries related to Cloudflare D1 and Drizzle.
(It might not be very related to Cloudflare, sorry!)


Currently, the drizzle-kit repository itself has not been made public yet, and the fix for the issue below seems to be a while away, so I am leaving a note as a temporary workaround for now.
https://github.com/drizzle-team/drizzle-kit-mirror/issues/242


When choosing an ORM for a setup with Cloudflare Workers + Cloudflare D1, many people recently adopt Drizzle ORM as their query builder.

Drizzle ORM is one of the libraries provided by the Drizzle Team. In addition to Drizzle ORM, which provides query builder functionality, there is a library called Drizzle Kit that provides features such as DDL generation and migrations.

From this Drizzle Kit, a useful SQL client tool called Drizzle Studio is provided, which allows you to execute SQL queries and check views in the browser.

https://orm.drizzle.team/drizzle-studio/overview

When using this Drizzle Studio with Cloudflare D1, you can connect to a remote D1 by specifying d1 as the driver in drizzle.config, providing the database name information listed in wrangler.toml, and starting the server with drizzle-kit studio.

drizzle.config.ts
import type { Config } from 'drizzle-kit'

const cfConfig = {
  schema: './schema/db/schema.ts',
  out: './schema/db/migrations',
  driver: 'd1',
  dbCredentials: {
    wranglerConfigPath: './wrangler.toml',
    dbName: 'test-app',
  },
  verbose: false,
  strict: true,
} satisfies Config
wrangler.toml
name = "api"
compatibility_date = "2023-10-16"
send_metrics = false
node_compat = true
main = "cmd/server/index.ts"


[[d1_databases]]
binding = "DB"
database_name = "test-db"
database_id = "xxx"
migrations_dir = "./schema/db/migrations"

However, with this configuration method, Drizzle Studio cannot connect to the local D1.
(This is because it does not use the Cloudflare runtime to internally switch to using local SQLite as wrangler dev does.)

Looking at the current type definitions for Drizzle's Config, it seems there is no method provided to connect to local D1 when using driver: "d1".

export type Config = {
    out?: string | undefined;
    breakpoints?: boolean | undefined;
    tablesFilter?: string | string[] | undefined;
    schemaFilter?: string | string[] | undefined;
    schema?: string | string[];
    verbose?: boolean | undefined;
    strict?: boolean | undefined;
} (
    // ...
    {
    driver: "d1";
    dbCredentials: {
        wranglerConfigPath: string;
        dbName: string;
    };
} | {});

Therefore, when using D1 locally, you need to change the driver in drizzle.config to better-sqlite and specify the SQLite binary generated by the wrangler command in dbCredentials.

Reference implementation ↓
https://github.com/sugar-cat7/d1-drizzle-studio/blob/main/service/api/drizzle.config.ts

With this, you can use Drizzle Studio against local D1 for the time being.
image

Please let me know if you know of any other better methods.

Discussion