EdgeDB 是一個圖形關聯資料庫,底層由 Postgres 驅動。它提供宣告式結構描述語言、遷移系統和物件導向查詢語言,此外還支援原始 SQL 查詢。它在資料庫層級解決了物件關聯對應問題,消除了在應用程式程式碼中使用 ORM 程式庫的需求。
首先,如果您尚未安裝 EdgeDB,請安裝 EdgeDB。
curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh
iwr https://ps1.edgedb.com -useb | iex
使用 bun init
建立一個新的專案。
mkdir my-edgedb-app
cd my-edgedb-app
bun init -y
我們將使用 EdgeDB CLI 為我們的專案初始化一個 EdgeDB 執行個體。這會在我們的專案根目錄中建立一個 edgedb.toml
檔案。
edgedb project init
No `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]
Y
Specify the name of EdgeDB instance to use with this project [default: my_edgedb_app]:
my_edgedb_app
Checking 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。
edgedb
edgedb> select 1 + 1;
2
edgedb> \quit
專案初始化完成後,我們可以定義結構描述。edgedb project init
命令已建立一個 dbschema/default.esdl
檔案來包含我們的結構描述。
dbschema
├── default.esdl
└── migrations
開啟該檔案並貼上以下內容。
module default {
type Movie {
required title: str;
releaseYear: int64;
}
};
然後產生並套用初始遷移。
edgedb migration create
Created /Users/colinmcd94/Documents/bun/fun/examples/my-edgedb-app/dbschema/migrations/00001.edgeql, id: m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq
edgedb migrate
Applied m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq (00001.edgeql)
套用我們的結構描述後,讓我們使用 EdgeDB 的 JavaScript 客戶端程式庫執行一些查詢。我們將安裝客戶端程式庫和 EdgeDB 的 codegen CLI,並建立一個 seed.ts
檔案。
bun add edgedb
bun add -D @edgedb/generate
touch 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.ts
Seeding complete.
EdgeDB 為 TypeScript 實作了許多程式碼產生工具。為了以型別安全的方式查詢我們新植入種子的資料庫,我們將使用 @edgedb/generate
來程式碼產生 EdgeQL 查詢建構器。
bunx @edgedb/generate edgeql-js
Generating 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 文件。