Search
ctrl/
Ask AI
Light
Dark
System

Generators

The @edgedb/generate package provides a set of code generation tools that are useful when developing an EdgeDB-backed applications with TypeScript/JavaScript.

To get started with generators, first initialize an EdgeDB project in the root of your application. Generators will look for an edgedb.toml file to determine the root of your application. See the Overview page for details on installing.

Generators work by connecting to the database to get information about the current state of the schema. Make sure you run the generators again any time the schema changes so that the generated code is in-sync with the current state of the schema.

Run a generator with the following command.

npm
yarn
pnpm
Deno
bun
Copy
$ 
npx @edgedb/generate <generator> [options]
Copy
$ 
yarn run -B generate <generator> [options]
Copy
$ 
pnpm exec generate <generator> [options]
Copy
$ 
  
  
  
deno run \
--allow-all \
--unstable \
https://deno.land/x/edgedb/generate.ts <generator> [options]
Copy
$ 
bunx @edgedb/generate <generator> [options]

The value of <generator> should be one of the following:

edgeql-js

Generates the query builder which provides a code-first way to write fully-typed EdgeQL queries with TypeScript. We recommend it for TypeScript users, or anyone who prefers writing queries with code.

docs

queries

Scans your project for *.edgeql files and generates functions that allow you to execute these queries in a typesafe way.

docs

interfaces

Introspects your schema and generates file containing TypeScript interfaces that correspond to each object type. This is useful for writing typesafe code to interact with EdgeDB.

docs

The generators require a connection to an active EdgeDB database. It does not simply read your local .esdl schema files. Generators rely on the database to introspect the schema and analyze queries. Doing so without a database connection would require implementing a full EdgeQL parser and static analyzer in JavaScript—which we don’t intend to do anytime soon.

Make sure your development database is up-to-date with your latest schema before running a generator!

If you’re using edgedb project init, the connection is automatically handled for you. Otherwise, you’ll need to explicitly pass connection information via environment variables or CLI flags, just like any other CLI command. See Client Libraries > Connection for guidance.

All generators look at your environment and guess what kind of files to generate (.ts vs .js + .d.ts) and what module system to use (CommonJS vs ES modules). You can override this with the --target flag.

--target ts

Generate TypeScript files (.ts)

--target mts

Generate TypeScript files (.mts) with extensioned ESM imports

--target esm

Generate .js with ESM syntax and .d.ts declaration files

--target cjs

Generate JavaScript with CommonJS syntax and and .d.ts declaration files

--target deno

Generate TypeScript files with Deno-style ESM imports

To see helptext for the @edgedb/generate command, run the following.

Copy
$ 
npx @edgedb/generate --help