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.
$
npx @edgedb/generate <generator> [options]
$
yarn run -B generate <generator> [options]
$
pnpm exec generate <generator> [options]
$
deno run \
--allow-all \
--unstable \
https://deno.land/x/edgedb/generate.ts <generator> [options]
$
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. | |
queries |
Scans your project for *.edgeql files and generates functions that allow you to execute these queries in a typesafe way. | |
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. |
Connection
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.
Targets
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.
|
Generate TypeScript files ( |
|
Generate TypeScript files ( |
|
Generate |
|
Generate JavaScript with CommonJS syntax and and |
|
Generate TypeScript files with Deno-style ESM imports |