Drizzle 和 Gel 入门

本指南假定您熟悉

Drizzle 通过 gel 客户端原生支持 Gel 连接。

这是项目基本的文件结构。在 src 目录中,我们在 index.ts 中定义了表。在 drizzle 文件夹中有生成的 Gel 到 Drizzle 模式。

📦 <project root>
 ├ 📂 drizzle
 ├ 📂 src
 │ └ 📜 index.ts
 ├ 📜 drizzle.config.ts
 ├ 📜 package.json
 └ 📜 tsconfig.json

步骤 1 - 安装并初始化 Gel 项目

npm
yarn
pnpm
bun
npx gel project init

步骤 2 - 定义基本 Gel 模式

dbschema/default.esdl 文件中添加基本 Gel 模式

module default {
    type user {
        name: str;
        required email: str;
        age: int16;
    }
}

步骤 3 - 将 Gel 模式推送到数据库

生成 Gel 迁移文件

gel migration create

将 Gel 迁移应用到数据库

gel migration apply

现在您应该拥有以下文件结构

📦 <project root>
 ├ 📂 dbschema
 │ ├ 📂 migrations
 │ ├ 📜 default.esdl
 │ └ 📜 scoping.esdl
 ├ 📂 src
 │ └ 📜 index.ts
 ├ 📜 drizzle.config.ts
 ├ 📜 edgedb.toml
 ├ 📜 package.json
 └ 📜 tsconfig.json

步骤 4 - 安装所需包

npm
yarn
pnpm
bun
npm i drizzle-orm gel
npm i -D drizzle-kit tsx

第 5 步 - 设置 Drizzle 配置文件

Drizzle config - 由 Drizzle Kit 使用的配置文件,包含有关你的数据库连接、迁移文件夹和 schema 文件的所有信息。

在项目的根目录中创建 drizzle.config.ts 文件并添加以下内容

drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  dialect: 'gel',
});

步骤 6 - 将 Gel 类型拉取到 Drizzle 模式

拉取您的数据库模式

npm
yarn
pnpm
bun
npx drizzle-kit pull

以下是生成的 schema.ts 文件的示例

drizzle/schema.ts
import { gelTable, uniqueIndex, uuid, smallint, text } from "drizzle-orm/gel-core"
import { sql } from "drizzle-orm"

export const users = gelTable("users", {
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
	age: smallint(),
	email: text().notNull(),
	name: text(),
}, (table) => [
	uniqueIndex("a8c6061c-f37f-11ef-9249-0d78f6c1807b;schemaconstr").using("btree", table.id.asc().nullsLast().op("uuid_ops")),
]);

步骤 7 - 将 Drizzle ORM 连接到数据库

src 目录中创建 index.ts 文件并初始化连接

src/index.ts
import { drizzle } from "drizzle-orm/gel";
import { createClient } from "gel";

const gelClient = createClient();
const db = drizzle({ client: gelClient });

步骤 8 - 查询数据库

src/index.ts
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/gel";
import { createClient } from "gel";
import { users } from "../drizzle/schema";

const gelClient = createClient();
const db = drizzle({ client: gelClient });

async function main() {
  const user: typeof users.$inferInsert = {
    name: "John",
    age: 30,
    email: "[email protected]",
  };

  await db.insert(users).values(user);
  console.log("New user created!");

  const usersResponse = await db.select().from(users);
  console.log("Getting all users from the database: ", usersResponse);
  /*
  const users: {
    id: number;
    name: string;
    age: number;
    email: string;
  }[]
  */

  await db
    .update(users)
    .set({
      age: 31,
    })
    .where(eq(users.email, user.email));
  console.log("User info updated!");

  await db.delete(users).where(eq(users.email, user.email));
  console.log("User deleted!");
}

main();

步骤 9 - 运行 index.ts 文件

要运行任何 TypeScript 文件,你有多种选择,但我们只使用一种:使用 tsx

你已经安装了 tsx,所以我们现在可以运行查询了

运行 index.ts 脚本

npm
yarn
pnpm
bun
npx tsx src/index.ts
提示

我们建议使用 bun 运行 TypeScript 文件。使用 bun,无论您的项目配置为 CommonJS (CJS)、ECMAScript Modules (ESM) 或任何其他模块格式,此类脚本都可以顺利执行,无需额外设置。要使用 bun 运行脚本,请使用以下命令

bun src/index.ts

如果你没有安装 bun,请查看 Bun 安装文档