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-clibun init を使用して新しいプロジェクトを作成します。
mkdir my-gel-app
cd my-gel-app
bun init -yGel 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")
> yindex.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
}, {
id: 2,
title: "The Matrix Reloaded",
releaseYear: 2003
}, {
id: 3,
title: "The Matrix Revolutions",
releaseYear: 2003
}
]完全なドキュメントについては、Gel ドキュメント を参照してください。