Cloudflare D1
New database
Meet Drizzle
Get started
New database
Existing database
PostgreSQL Neon Vercel Postgres Supabase Xata PGLite MySQL PlanetScale TiDB SQLite Turso Cloudflare D1 Bun SQLite Expo SQLite OP SQLite
开始使用 Drizzle 和 SQLite
This guide assumes familiarity with:
dotenv - 管理环境变量的包 - 查看这里
tsx - 运行 TypeScript 文件的包 - 查看这里
Cloudflare D1 - 用于从您的 Workers 和 Pages 项目中查询的无服务器 SQL 数据库 - 查看这里
wrangler - Cloudflare 开发者平台命令行界面 - 查看这里
基本文件结构
这是项目的基本文件结构。在 src/db
目录中,我们在 schema.ts
中有表定义。在 drizzle
文件夹中,有 SQL 迁移文件和快照。
📦 <project root>
├ 📂 drizzle
├ 📂 src
│ ├ 📂 db
│ │ └ 📜 schema.ts
│ └ 📜 index.ts
├ 📜 .env
├ 📜 drizzle.config.ts
├ 📜 package.json
└ 📜 tsconfig.json
第一步 - 安装所需的包
npm i drizzle-orm dotenv
npm i -D drizzle-kit tsx
yarn add drizzle-orm dotenv
yarn add -D drizzle-kit tsx
pnpm add drizzle-orm dotenv
pnpm add -D drizzle-kit tsx
bun add drizzle-orm dotenv
bun add -D drizzle-kit tsx
第二步 - 设置 wrangler.toml
您需要一个 wrangler.toml
文件用于 D1 数据库,其内容大致如下:
name = "您的项目名称"
main = "src/index.ts"
compatibility_date = "2022-11-07"
node_compat = true
[[ d1_databases ]]
binding = "DB"
database_name = "您的数据库名称"
database_id = "您的数据库 ID"
migrations_dir = "drizzle"
第三步 - 将 Drizzle ORM 连接到数据库
import { drizzle } from 'drizzle-orm/d1' ;
export interface Env {
< BINDING_NAME > : D1Database ;
}
export default {
async fetch (request : Request , env : Env ) {
const db = drizzle (env. < BINDING_NAME > );
} ,
};
第四步 - 创建一个表
Create a schema.ts
file in the src/db
directory and declare your table:
import { int , sqliteTable , text } from "drizzle-orm/sqlite-core" ;
export const usersTable = sqliteTable ( "users_table" , {
id : int () .primaryKey ({ autoIncrement : true }) ,
name : text () .notNull () ,
age : int () .notNull () ,
email : text () .notNull () .unique () ,
});
第五步 - 设置 Drizzle 配置文件
Drizzle 配置 - 一个供 Drizzle Kit 使用的配置文件,包含有关您的数据库连接、迁移文件夹和模式文件的所有信息。
在项目根目录创建一个 drizzle.config.ts
文件,并添加以下内容:
import 'dotenv/config' ;
import { defineConfig } from 'drizzle-kit' ;
export default defineConfig ({
out : './drizzle' ,
schema : './src/db/schema.ts' ,
dialect : 'sqlite' ,
driver : '' ,
dbCredentials : {
accountId : process . env . CLOUDFLARE_ACCOUNT_ID ! ,
databaseId : process . env . CLOUDFLARE_DATABASE_ID ! ,
token : process . env . CLOUDFLARE_D1_TOKEN ! ,
} ,
});
提示
您可以查看 我们的教程 ,了解如何从 CloudFlare 获取环境变量
第六步 - 将更改应用于数据库
You can directly apply changes to your database using the drizzle-kit push
command. This is a convenient method for quickly testing new schema designs or modifications in a local development environment, allowing for rapid iterations without the need to manage migration files:
npx drizzle-kit push
Read more about the push command in documentation .
Tips
Alternatively, you can generate migrations using the drizzle-kit generate
command and then apply them using the drizzle-kit migrate
command:
Generate migrations:
npx drizzle-kit generate
Apply migrations:
npx drizzle-kit migrate
Read more about migration process in documentation .
第七步 - 初始化并查询数据库
import { drizzle } from 'drizzle-orm/d1' ;
export interface Env {
< BINDING_NAME > : D1Database ;
}
export default {
async fetch (request : Request , env : Env ) {
const db = drizzle (env. < BINDING_NAME > );
const result = await db .select () .from (users) .all ()
return Response .json (result);
} ,
};
第八步 - 运行 index.ts 文件
To run any TypeScript files, you have several options, but let’s stick with one: using tsx
You’ve already installed tsx
, so we can run our queries now
Run index.ts
script
npx tsx src/index.ts
yarn tsx src/index.ts
pnpm tsx src/index.ts
bun tsx src/index.ts
tips
We suggest using bun
to run TypeScript files. With bun
, such scripts can be executed without issues or additional
settings, regardless of whether your project is configured with CommonJS (CJS), ECMAScript Modules (ESM), or any other module format.
To run a script with bun
, use the following command:
bun src/index.ts
If you don’t have bun installed, check the Bun installation docs