EdgeDB 是一種由 Postgres 驅動的圖關係資料庫。它提供了宣告式 schema 語言、遷移系統和麵向物件查詢語言,此外還支援原始 SQL 查詢。它在資料庫層解決了物件關係對映問題,無需在應用程式程式碼中使用 ORM 庫。
首先,如果您尚未安裝 EdgeDB,請安裝 EdgeDB。
curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | shiwr https://ps1.edgedb.com -useb | iex使用 bun init 建立一個新專案。
mkdir my-edgedb-appcd my-edgedb-appbun init -y我們將使用 EdgeDB CLI 為專案初始化一個 EdgeDB 例項。這會在專案根目錄中建立一個 edgedb.toml 檔案。
edgedb project initNo `edgedb.toml` found in `/Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app` or above
Do you want to initialize a new project? [Y/n]YSpecify the name of EdgeDB instance to use with this project [default: my_edgedb_app]:my_edgedb_appChecking EdgeDB versions...
Specify the version of EdgeDB to use with this project [default: x.y]:x.y┌─────────────────────┬────────────────────────────────────────────────────────────────────────┐
│ Project directory │ /Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app │
│ Project config │ /Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app/edgedb.toml │
│ Schema dir (empty) │ /Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app/dbschema │
│ Installation method │ portable package │
│ Version │ x.y+6d5921b │
│ Instance name │ my_edgedb_app │
└─────────────────────┴────────────────────────────────────────────────────────────────────────┘
Version x.y+6d5921b is already downloaded
Initializing EdgeDB instance...
Applying migrations...
Everything is up to date. Revision initial
Project initialized.
To connect to my_edgedb_app, run `edgedb`為了檢視資料庫是否正在執行,讓我們開啟一個 REPL 並執行一個簡單的查詢。
然後執行 \quit 退出 REPL。
edgedbedgedb> select 1 + 1;
2
edgedb> \quit專案初始化後,我們可以定義一個 schema。edgedb project init 命令已經建立了一個 dbschema/default.esdl 檔案來包含我們的 schema。
dbschema
├── default.esdl
└── migrations
開啟該檔案並貼上以下內容。
module default {
type Movie {
required title: str;
releaseYear: int64;
}
};
然後生成並應用初始遷移。
edgedb migration createCreated /Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app/dbschema/migrations/00001.edgeql, id: m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrqedgedb migrateApplied m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq (00001.edgeql)應用 schema 後,讓我們使用 EdgeDB 的 JavaScript 客戶端庫執行一些查詢。我們將安裝客戶端庫和 EdgeDB 的 codegen CLI,並建立一個 seed.ts 檔案。
bun add edgedbbun add -D @edgedb/generatetouch seed.ts將以下程式碼貼上到 seed.ts 中。
客戶端會自動連線到資料庫。我們使用 .execute() 方法插入幾部電影。我們將使用 EdgeQL 的 for 表示式將這種批次插入轉換為單個最佳化查詢。
import { createClient } from "edgedb";
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.EdgeDB 為 TypeScript 實現了一些程式碼生成工具。為了以型別安全的方式查詢我們新種子化的資料庫,我們將使用 @edgedb/generate 來程式碼生成 EdgeQL 查詢構建器。
bunx @edgedb/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 "edgedb";
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
}
]有關完整文件,請參閱 EdgeDB 文件。