EdgeDB TypeScript/JS Client​

This is the official EdgeDB client library for JavaScript and TypeScript. It’s the easiest way to connect to your database and execute queries from a Node.js or Deno backend.

Installation

To get started, install the edgedb module from NPM.

  1. npm install edgedb
  1. # using Yarn
  1. npm install edgedb

There are two components of this library: the driver and the query builder.

The driver​

The driver implements the core functionality required to establish a connection to your database and execute queries. If you prefer writing queries as strings, the driver API is all you need.

  1. const edgedb = require("edgedb");
  2. const client = edgedb.createClient();
  3. const query = `select "Hello world!";`;
  4. async function run(){
  5. const result = await client.query(query)
  6. console.log(result); // "Hello world!"
  7. }

If you’re not using TypeScript, you can skip straight to the Driver docs.

The query builder​

The EdgeDB query builder provides a code-first way to write fully-typed EdgeQL queries with TypeScript. We recommend it for TypeScript users—it’s awesome.

  1. import edgedb from "edgedb";
  2. import e from "./dbschema/edgeql-js"; // auto-generated code
  3. const client = edgedb.createClient();
  4. async function run(){
  5. const query = e.str("Hello world!");
  6. const result = await query.run(client)
  7. console.log(result); // "Hello world!"
  8. }

As you can see, you still use the edgedb module to instantiate a client, but you use the auto-generated query builder to write and execute your queries.

Why use the query builder?​

Type inference! If you’re using TypeScript, the result type of all queries is automatically inferred for you. For the first time, you don’t need an ORM to write strongly typed queries.

  1. const client = edgedb.createClient();
  2. const q1 = await e.str("Hello world!").run(client);
  3. // number
  4. const q2 = await e.set(1, 2, 3).run(client);
  5. // number[]
  6. const q3 = e.select(e.Movie, () => ({
  7. id: true,
  8. name: true
  9. }));
  10. // {id:string; name: string}[]

Auto-completion! You can write queries full autocompletion on EdgeQL keywords, standard library functions, and link/property names.

Type checking! In the vast majority of cases, the query builder won’t let you construct invalid queries. This eliminates an entire class of bugs and helps you write valid queries the first time.

Is it an ORM?​

Nope. There’s no “object-relational mapping” happening here—that’s all handled by EdgeDB itself.

The query builder itself is a comparatively thin wrapper over EdgeQL. We’ve designed the API such that the TypeScript representation of a query is structurally similar to the equivalent EdgeQL.

  1. select Movie {
  2. id,
  3. title,
  4. uppercase_title := str_upper(.title)
  5. }
  6. filter .title = "Iron Man"
  1. e.select(e.Movie, movie => ({
  2. id: true,
  3. title: true,
  4. uppercase_title := e.str_upper(movie.title),
  5. filter: e.op(movie.title, '=', 'Iron Man')
  6. });

More importantly, it gives you access to the full power of EdgeQL! The query builder can represent EdgeQL queries of arbitrary complexity.

By comparison, SQL-based ORMs are limited in what they can represent. Things like computed properties, SQL’s large standard library of functions, aggregations, transactions, and subqueries are rarely possible. But even for the simple stuff, we think the query builder API is more readable, compact, and intuitive than any ORM on the market.

How do I get started?​

The query builder not an alternative to the driver; the driver API is still needed to initialize a database client. We recommend reading the Driver docs first, then continuing on to the Query builder docs.