Generation​

The query builder is auto-generated from your database schema.

Minimum requirements​

It’s possible to use the query builder with or without TypeScript. Some requirements apply to TypeScript users only.

  • Node.js 10+. Run node --version to see your current version. TypeScript users should also install Node.js typing: npm install @types/node

  • TypeScript 4.4+

  • Make sure the following compilerOptions exist in your tsconfig.json:

  1. ```
  2. // tsconfig.json
  3. {
  4. // ...
  5. "compilerOptions": {
  6. // ...
  7. "strict": true,
  8. "downlevelIteration": true,
  9. }
  10. }
  11. ```

Initialize a project​

Set up an EdgeDB project for your application. Follow the Quickstart for detailed instructions on installing the CLI, initializing a project, writing a basic schema, and executing your first migration.

Install the JavaScript client library​

Install the edgedb package from NPM.

  1. npm install edgedb # npm users
  1. yarn add edgedb # yarn users

Generate the query builder​

Generate the query builder with the following command.

  1. npx edgeql-js # npm users
  1. yarn edgeql-js # yarn users

You’ll see something like this.

  1. npx edgeql-js
  1. Detected tsconfig.json, generating TypeScript files.
  2. To override this, use the --target flag.
  3. Run `npx edgeql-js --help` for details.
  4. Generating query builder into ./dbschema/edgeql-js
  5. Connecting to EdgeDB instance...
  6. Introspecting database schema...
  7. Generation successful!

The npx edgeql-js establishes a connection to your database, introspects the current schema, and generates a bunch of files. By default, these files are written to the ./dbschema/edgeql-js directory, as defined relative to your project root. The project root is identified by scanning up the file system for a package.json.

Connection issue?

This command must be able to connect to a running EdgeDB instance. If you’re using edgedb project init, this is automatically handled for you. Otherwise, you’ll need to explicitly pass connection information, just like any other CLI command. See Client Libraries > Connection for guidance.

Version control​

The first time you run the command, you’ll be prompted to add the generated files to your .gitignore. Confirm this prompt, and a line will be automatically added to your .gitignore to exclude the generated files from Git.

  1. npx edgeql-js
  1. ...
  2. Checking the generated query builder into version control
  3. is NOT RECOMMENDED. Would you like to update .gitignore to ignore
  4. the query builder directory? The following line will be added:
  5. dbschema/edgeql-js
  6. [y/n] (leave blank for "y")

Importing​

Once the query builder is generated, it’s ready to use! Just import it and start building queries. Below is a full “Hello world” example.

  1. import * as edgedb from "edgedb";
  2. import e from "./dbschema/edgeql-js";
  3. const client = edgedb.createClient();
  4. async function run(){
  5. // declare a simple query
  6. const myQuery = e.str("Hello world!");
  7. // execute the expression
  8. const result = await myQuery.run(client);
  9. // print the result
  10. console.log(result); // "Hello world!"
  11. }

Configuring npx edgeql-js

The generation command is configurable in a number of ways.

--output-dir <path>

Sets the output directory for the generated files.

--target <ts|cjs|esm>

What type of files to generate.

ts

Generate TypeScript

cjs

Generate JavaScript with CommonJS (require/module.exports) syntax

esm

Generate JavaScript with ES Module (import/export) syntax

The default is determined according the the following simple algorithm:

  1. Check for a tsconfig.json in the project root. If it exists, use --target ts.

  2. Otherwise. check if package.json includes "type": "module". If so, use --target esm.

  3. Otherwise, use --target cjs.

--force-overwrite

To avoid accidental changes, you’ll be prompted to confirm whenever the --target has changed from the previous run. To avoid this prompt, pass --force-overwrite.

-h/--help

Prints full documentation.

The generator also supports all the connection flags supported by the EdgeDB CLI. These aren’t necessary when using a project or environment variables to configure a connection.