Gel(前身为 EdgeDB)是一个底层由 Postgres 驱动的图关系数据库。它提供声明式模式语言、迁移系统和面向对象查询语言,此外还支持原始 SQL 查询。它在数据库层解决了对象关系映射问题,消除了在你的应用代码中使用 ORM 库的需求。
首先,如果你还没有安装,请 安装 Gel。
curl https://www.geldata.com/sh --proto "=https" -sSf1 | shirm https://www.geldata.com/ps1 | iexbrew install geldata/tap/gel-cli使用 bun init 创建一个新项目。
mkdir my-edgedb-app
cd my-edgedb-app
bun init -y我们将使用 Gel CLI 为我们的项目初始化一个 Gel 实例。这会在项目根目录创建一个 gel.toml 文件。
gel project initNo `gel.toml` found in `/Users/colinmcd94/Documents/bun/fun/examples/my-gel-app` or above
Do you want to initialize a new project? [Y/n]
> Y
Specify the name of Gel instance to use with this project [default: my_gel_app]:
> my_gel_app
Checking Gel versions...
Specify the version of Gel to use with this project [default: x.y]:
> x.y
┌─────────────────────┬──────────────────────────────────────────────────────────────────┐
│ Project directory │ /Users/colinmcd94/Documents/bun/fun/examples/my-gel-app │
│ Project config │ /Users/colinmcd94/Documents/bun/fun/examples/my-gel-app/gel.toml│
│ Schema dir (empty) │ /Users/colinmcd94/Documents/bun/fun/examples/my-gel-app/dbschema│
│ Installation method │ portable package │
│ Version │ x.y+6d5921b │
│ Instance name │ my_gel_app │
└─────────────────────┴──────────────────────────────────────────────────────────────────┘
Version x.y+6d5921b is already downloaded
Initializing Gel instance...
Applying migrations...
Everything is up to date. Revision initial
Project initialized.
To connect to my_gel_app, run `gel`要查看数据库是否正在运行,让我们打开一个 REPL 并运行一个简单的查询。
gel
gel> select 1 + 1;2然后运行 \quit 退出 REPL。
gel> \quit项目初始化后,我们可以定义一个模式。gel project init 命令已经创建了一个 dbschema/default.esdl 文件来包含我们的模式。
dbschema
├── default.esdl
└── migrations打开该文件并粘贴以下内容。
module default {
type Movie {
required title: str;
releaseYear: int64;
}
};然后生成并应用初始迁移。
gel migration createCreated /Users/colinmcd94/Documents/bun/fun/examples/my-gel-app/dbschema/migrations/00001.edgeql, id: m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrqgel migrateApplied m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq (00001.edgeql)应用模式后,让我们使用 Gel 的 JavaScript 客户端库执行一些查询。我们将安装客户端库和 Gel 的代码生成 CLI,并创建一个 seed.ts 文件。
bun add gel
bun add -D @gel/generate
touch seed.ts将以下代码粘贴到 seed.ts 中。
客户端会自动连接到数据库。我们使用 .execute() 方法插入几部电影。我们将使用 EdgeQL 的 for 表达式将这个批量插入转换为单个优化查询。
import { createClient } from "gel";
const client = createClient();
const INSERT_MOVIE = `
with movies := <array<tuple<title: str, year: int64>>>$movies
for movie in array_unpack(movies) union (
insert Movie {
title := movie.title,
releaseYear := movie.year,
}
)
`;
const movies = [
{ title: "The Matrix", year: 1999 },
{ title: "The Matrix Reloaded", year: 2003 },
{ title: "The Matrix Revolutions", year: 2003 },
];
await client.execute(INSERT_MOVIE, { movies });
console.log(`Seeding complete.`);
process.exit();然后使用 Bun 运行此文件。
bun run seed.tsSeeding complete.Gel 为 TypeScript 实现了许多代码生成工具。为了以类型安全的方式查询我们新播种的数据库,我们将使用 @gel/generate 来代码生成 EdgeQL 查询构建器。
bunx @gel/generate edgeql-jsGenerating query builder...
Detected tsconfig.json, generating TypeScript files.
To override this, use the --target flag.
Run `npx @edgedb/generate --help` for full options.
Introspecting database schema...
Writing files to ./dbschema/edgeql-js
Generation complete! 🤘
Checking the generated query builder into version control
is not recommended. Would you like to update .gitignore to ignore
the query builder directory? The following line will be added:
dbschema/edgeql-js
[y/n] (leave blank for "y")
> y在 index.ts 中,我们可以从 ./dbschema/edgeql-js 导入生成的查询构建器并编写一个简单的 select 查询。
import { createClient } from "gel";
import e from "./dbschema/edgeql-js";
const client = createClient();
const query = e.select(e.Movie, () => ({
title: true,
releaseYear: true,
}));
const results = await query.run(client);
console.log(results);
results; // { title: string, releaseYear: number | null }[]使用 Bun 运行文件,我们可以看到我们插入的电影列表。
bun run index.ts[
{
title: "The Matrix",
releaseYear: 1999
}, {
title: "The Matrix Reloaded",
releaseYear: 2003
}, {
title: "The Matrix Revolutions",
releaseYear: 2003
}
]有关完整文档,请参阅 Gel 文档。