Get Started with Drizzle and Gel

This guide assumes familiarity with:
  • tsx - package for running TypeScript files - read here
  • gel-js - package for querying your Gel database - read here

Drizzle has native support for Gel connections with the gel client.

This is the basic file structure of the project. In the src directory, we have table definition in index.ts. In drizzle folder there are generated Gel to Drizzle schema

📦 <project root>
 ├ 📂 drizzle
 ├ 📂 src
 │ └ 📜 index.ts
 ├ 📜 drizzle.config.ts
 ├ 📜 package.json
 └ 📜 tsconfig.json

Step 1 - Install and init Gel project

Linux or macOS

curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh

For more options, check gel docs

Once installed:

gel project init   

Step 2 - Define basic Gel schema

In dbschema/default.esdl file add a basic Gel schema

module default {
    TYPE test {
        name: str;
        required email: str;
        age: int16;
    }
}

Step 3 - Push Gel schema to the database

Generate Gel migration file:

gel migration create

Apply Gel migrations to the database

gel migration apply

Now you should have this file structure

📦 <project root>
 ├ 📂 dbschema
 │ ├ 📂 migrations
 │ ├ 📜 default.esdl
 │ └ 📜 scoping.esdl
 ├ 📂 src
 │ └ 📜 index.ts
 ├ 📜 drizzle.config.ts
 ├ 📜 edgedb.toml
 ├ 📜 package.json
 └ 📜 tsconfig.json

Step 4 - Install gel package

npm
yarn
pnpm
bun
npm i drizzle-orm gel
npm i -D drizzle-kit tsx

Step 5 - Setup Drizzle config file

Drizzle config - a configuration file that is used by Drizzle Kit and contains all the information about your database connection, migration folder and schema files.

Create a drizzle.config.ts file in the root of your project and add the following content:

drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  dialect: 'gel',
});

Step 6 - Pull Gel types to Drizzle schema

Pull your database schema:

npm
yarn
pnpm
bun
npx drizzle-kit pull

Here is an example of the generated schema.ts file:

// drizzle/schema.ts
import { gelTable, uniqueIndex, uuid, smallint, text } from "drizzle-orm/gel-core"
import { sql } from "drizzle-orm"

export const users = gelTable("users", {
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
	age: smallint(),
	email: text().notNull(),
	name: text(),
}, (table) => [
	uniqueIndex("a8c6061c-f37f-11ef-9249-0d78f6c1807b;schemaconstr").using("btree", table.id.asc().nullsLast().op("uuid_ops")),
]);

Step 6 - Connect Drizzle ORM to the database

Create a index.ts file in the src directory and initialize the connection:

import { drizzle } from "drizzle-orm/gel";
import { createClient } from "gel";

const gelClient = createClient();
const db = drizzle({ client: gelClient });

Step 7 - Query the database

src/index.ts
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/gel";
import { createClient } from "gel";
import { users } from "../drizzle/schema";

const gelClient = createClient();
const db = drizzle({ client: gelClient });

async function main() {
  const user: typeof users.$inferInsert = {
    name: "John",
    age: 30,
    email: "[email protected]",
  };

  await db.insert(users).values(user);
  console.log("New user created!");

  const usersResponse = await db.select().from(users);
  console.log("Getting all users from the database: ", usersResponse);
  /*
  const users: {
    id: number;
    name: string;
    age: number;
    email: string;
  }[]
  */

  await db
    .update(users)
    .set({
      age: 31,
    })
    .where(eq(users.email, user.email));
  console.log("User info updated!");

  await db.delete(users).where(eq(users.email, user.email));
  console.log("User deleted!");
}

main();

Step 8 - Run index.ts file

To run any TypeScript files, you have several options, but let’s stick with one: using tsx

You’ve already installed tsx, so we can run our queries now

Run index.ts script

npm
yarn
pnpm
bun
npx tsx src/index.ts
tips

We suggest using bun to run TypeScript files. With bun, such scripts can be executed without issues or additional settings, regardless of whether your project is configured with CommonJS (CJS), ECMAScript Modules (ESM), or any other module format. To run a script with bun, use the following command:

bun src/index.ts

If you don’t have bun installed, check the Bun installation docs