Skip to content

Gel(旧 EdgeDB)は、内部で Postgres を使用するグラフリレーショナルデータベースです。宣言型スキーマ言語、マイグレーションシステム、オブジェクト指向クエリ言語を提供し、生 SQL クエリのサポートもあります。アプリケーションコード内の ORM ライブラリの必要性を排除し、データベース層でオブジェクトリレーショナルマッピングの問題を解決します。


まだインストールしていない場合は、まず Gel をインストール します。

sh
curl https://www.geldata.com/sh --proto "=https" -sSf1 | sh
sh
irm https://www.geldata.com/ps1 | iex
sh
brew install geldata/tap/gel-cli

bun init を使用して新しいプロジェクトを作成します。

sh
mkdir my-gel-app
cd my-gel-app
bun init -y

Gel CLI を使用して、プロジェクトの Gel インスタンスを初期化します。これにより、プロジェクトルートに gel.toml ファイルが作成されます。

sh
gel project init
txt
No `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 を開いてシンプルなクエリを実行しましょう。

sh
gel
gel> select 1 + 1;
txt
2

次に \quit を実行して REPL を終了します。

sh
gel> \quit

プロジェクトの初期化が完了したので、スキーマを定義できます。gel project init コマンドはすでにスキーマを含む dbschema/default.esdl ファイルを作成しています。

txt
dbschema
├── default.esdl
└── migrations

そのファイルを開き、次の内容を貼り付けます。

ts
module default {
  type Movie {
    required title: str;
    releaseYear: int64;
  }
};

次に、初期マイグレーションを生成して適用します。

sh
gel migration create
txt
Created /Users/colinmcd94/Documents/bun/fun/examples/my-gel-app/dbschema/migrations/00001.edgeql, id: m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq
sh
gel migrate
txt
Applied m1uwekrn4ni4qs7ul7hfar4xemm5kkxlpswolcoyqj3xdhweomwjrq (00001.edgeql)

スキーマが適用されたので、Gel の JavaScript クライアントライブラリを使用してクエリを実行しましょう。クライアントライブラリと Gel のコード生成 CLI をインストールし、seed.ts ファイルを作成します。

sh
bun add gel
bun add -D @gel/generate
touch seed.ts

次のコードを seed.ts に貼り付けます。

クライアントはデータベースに自動接続します。.execute() メソッドを使用して映画をいくつか挿入します。EdgeQL の for 式を使用して、このバルク挿入を単一の最適化されたクエリに変換します。

ts
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 で実行します。

sh
bun run seed.ts
txt
Seeding complete.

Gel は TypeScript 向けのコード生成ツールを多数実装しています。新しくシードされたデータベースを型安全にクエリするために、@gel/generate を使用して EdgeQL クエリビルダーをコード生成します。

sh
bunx @gel/generate edgeql-js
txt
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 クエリを記述できます。

ts
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 でファイルを実行すると、挿入した映画のリストが表示されます。

sh
bun run index.ts
txt
[
  {
    title: "The Matrix",
    releaseYear: 1999
  }, {
    id: 2,
    title: "The Matrix Reloaded",
    releaseYear: 2003
  }, {
    id: 3,
    title: "The Matrix Revolutions",
    releaseYear: 2003
  }
]

完全なドキュメントについては、Gel ドキュメント を参照してください。

Bun by www.bunjs.com.cn 編集