现在你应该拥有这样的文件结构
📦 <project root>
├ 📂 dbschema
│ ├ 📂 migrations
│ ├ 📜 default.esdl
│ └ 📜 scoping.esdl
├ 📂 src
│ └ 📜 index.ts
├ 📜 drizzle.config.ts
├ 📜 edgedb.toml
├ 📜 package.json
└ 📜 tsconfig.json
Drizzle 原生支持与 gel
客户端的 Gel 连接。
这是项目的基本文件结构。在 src
目录中,我们有 index.ts
中的表定义。在 drizzle
文件夹中,包含生成的 Gel 到 Drizzle 模式
📦 <project root>
├ 📂 drizzle
├ 📂 src
│ └ 📜 index.ts
├ 📜 drizzle.config.ts
├ 📜 package.json
└ 📜 tsconfig.json
npx gel 项目初始化
在 dbschema/default.esdl
文件中添加一个基本的 Gel 模式
module default {
type user {
name: str;
required email: str;
age: int16;
}
}
生成 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
npm i drizzle-orm gel -D drizzle-kit tsx
Drizzle 配置 - Drizzle 套件 使用的配置文件,包含有关数据库连接、迁移文件夹和模式文件的所有信息。
在项目根目录中创建一个 drizzle.config.ts
文件并添加以下内容:
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
dialect: 'gel',
});
拉取数据库模式:
npx drizzle-kit pull
以下是生成的 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")),
]);
在 src
目录中创建一个 index.ts
文件并初始化连接:
import { drizzle } from "drizzle-orm/gel";
import { createClient } from "gel";
const gelClient = createClient();
const db = drizzle({ client: gelClient });
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: "john@example.com",
};
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();
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
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