• Stars
    star
    479
  • Rank 88,898 (Top 2 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official TypeScript/JS client library and query builder for EdgeDB

The official Node.js client library for EdgeDB

Build status NPM version Stars

Quickstart   •   Website   •   Docs   •   Discord   •   Twitter


The v1.0.0 version of this library was released on Oct 21, 2022. There are a number of new features and breaking changes, especially regarding the query builder workflow. Refer to the release notes for full information.

This is the official EdgeDB client library for JavaScript and TypeScript.

If you're just getting started with EdgeDB, we recommend going through the EdgeDB Quickstart first. This walks you through the process of installing EdgeDB, creating a simple schema, and writing some simple queries.

Requirements

  • Node.js 16+
    • We rely on global fetch being available, so you can bring your own polyfill and if you run Node 16, you'll need to run with the --experimental-fetch flag enabled.
  • For TypeScript users:
    • TypeScript 4.4+ is required
    • yarn add @types/node --dev

Installation

npm install edgedb      # npm users
yarn add edgedb         # yarn users

EdgeDB 2.x requires v0.21.0 or later

Packages

  • packages/driver: The edgedb client library.
  • packages/generate: The @edgedb/generate package. Implements code generators, including edgeql-js and queries.
  • packages/deno: The directory where the auto-generated deno.land/x/edgedb package is generated into. Both the driver and codegen tools are generated into this module.
  • packages/edgeql-js: A skeleton package that prints an informative error message when npx edgeql-js is executed without edgedb installed.

Basic usage

The examples below demonstrate only the most fundamental use cases for this library. Go to the complete documentation site. >

Create a client

A client is an instance of the Client class, which maintains a pool of connections to your database and provides methods for executing queries.

For TypeScript (and Node.js+ESM)

import * as edgedb from "edgedb";

const client = edgedb.createClient();

For Node.js (CommonJS)

const edgedb = require("edgedb");

const client = edgedb.createClient();

Configuring the connection

The call to edgedb.createClient() doesn't require arguments, as the library can determine how to connect to your database using the following mechanisms.

  1. For local development: initialize a project with the edgedb project init command. As long as the file is within a project directory, createClient will be able to auto-discover the connection information of the project's associated instance. For more information on projects, follow the Using projects guide.

  2. In production: configure the connection using environment variables. (This can also be used during local development if you prefer.) The easiest way is to set the EDGEDB_DSN variable; a DSN (also known as a "connection string") is a string of the form edgedb://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE.

For advanced cases, see the DSN specification and Reference > Connection Parameters.

Run a query

The remainder of the documentation assumes you are using ES module (import) syntax.

import * as edgedb from "edgedb";

const client = edgedb.createClient();
await client.query("select 2 + 2"); // => [4]

Note that the result is an array. The .query() method always returns an array, regardless of the result cardinality of your query. If your query returns zero or one elements, use the .querySingle() instead.

// empty set, zero elements
await client.querySingle("select <str>{}"); // => null

// one element
await client.querySingle("select 2 + 2"); // => 4

// one element
await client.querySingle(
  `select Movie { title }
  filter .id = <uuid>'2eb3bc76-a014-45dc-af66-2e6e8cc23e7e';`
); // => { title: "Dune" }

Generators

Install the @edgedb/generate package as a dev dependency to take advantage of EdgeDB's built-in code generators.

npm install @edgedb/generate  --save-dev      # npm users
yarn add @edgedb/generate --dev               # yarn users

Then run a generator with the following command:

$ npx @edgedb/generate <generator> [FLAGS]

The following <generator>s are currently supported:

  • queries: Generate typed functions from *.edgeql files
  • edgeql-js: Generate the query builder

queries

Run the following command to generate a source file for each *.edgeql system in your project.

$ npx @edgedb/generate queries

Assume you have a file called getUser.edgeql in your project directory.

// getUser.edgeql
select User {
  name,
  email
}
filter .email = <str>$email;

This generator will generate a getUser.query.ts file alongside it that exports a function called getUser.

import {createClient} from "edgedb";
import {myQuery} from "./myQuery.query";

const client = createClient();

const user = await myQuery(client, {name: "Timmy"});
user; // {name: string; email: string}

The first argument is a Client, the second is the set of parameters. Both the parameters and the returned value are fully typed.

edgeql-js (query builder)

The query builder lets you write queries in a code-first way. It automatically infers the return type of your queries.

To generate the query builder, install the edgedb package, initialize a project (if you haven't already), then run the following command:

$ npx @edgedb/generate edgeql-js

This will generate an EdgeQL query builder into the ./dbschema/edgeql-js directory, as defined relative to your project root.

For details on generating the query builder, refer to the complete documentation. Below is a simple select query as an example.

import { createClient } from "edgedb";
import e from "./dbschema/edgeql-js";

const client = createClient();
const query = e.select(e.Movie, (movie) => ({
  id: true,
  title: true,
  actors: { name: true },
  num_actors: e.count(movie.actors),
  filter_single: e.op(movie.title, "=", "Dune"),
}));

const result = await query.run(client);
result.actors[0].name; // => Timothee Chalamet

For details on using the query builder, refer to the full query builder docs.

Contribute

Contributing to this library requires a local installation of EdgeDB. Install EdgeDB from here or build it from source.

$ git clone [email protected]:edgedb/edgedb-js.git
$ cd edgedb-js
$ yarn                           # install dependencies
$ yarn workspaces run build      # build all packages
$ yarn workspaces run test       # run tests for all packages

License

edgedb-js is developed and distributed under the Apache 2.0 license.

More Repositories

1

edgedb

A graph-relational database with declarative schema, built-in migration system, and a next-generation query language
Python
12,123
star
2

edgedb-python

The official Python client library for EdgeDB
Python
349
star
3

imdbench

IMDBench — Realistic ORM benchmarking
Python
209
star
4

edgedb-rust

The official Rust binding for EdgeDB
Rust
198
star
5

edgedb-go

The official Go client library for EdgeDB
Go
152
star
6

edgedb-cli

The EdgeDB CLI
Rust
152
star
7

edgedb-elixir

Elixir client for EdgeDB
Elixir
85
star
8

edgedb-deno

EdgeDB driver for Deno
TypeScript
80
star
9

remix

A Remix stack for EdgeDB-backed applications
JavaScript
78
star
10

edgedb-net

The official .NET client library for EdgeDB
C#
77
star
11

edgedb-docker

Official Docker Image packaging for EdgeDB
Shell
73
star
12

easy-edgedb

An illustrated textbook designed to be a one-stop shop for learning EdgeDB
66
star
13

edgedb-examples

EdgeDB example projects for different stacks
TypeScript
55
star
14

edgedb-ui

The home of EdgeDB UI and all related shared UI components
TypeScript
47
star
15

rfcs

RFCs for major changes to EdgeDB
Python
34
star
16

edgedb-vim

EdgeDB + Vim
Python
31
star
17

awesome-edgedb

A curated list of resources related to EdgeDB.
30
star
18

edgedb-editor-plugin

EdgeDB plugin for Sublime Text, Atom, and VSCode
JavaScript
27
star
19

edgedb-dart

Dart
27
star
20

setup-edgedb

A GitHub Action to install EdgeDB and its CLI
TypeScript
18
star
21

edgedb-java

The official Java client library for EdgeDB
Java
17
star
22

mcu-sandbox

A movie schema sandbox for playing with EdgeDB and the EdgeQL query builder, pre-loaded with MCU data
TypeScript
15
star
23

metapkg

Multi-platform native package builder toolkit
Python
14
star
24

edgedb-deploy

EdgeDB Deployment Recipes and Scripts
Shell
9
star
25

edgedb-grafana-frontend

A frontend plugin for Grafana that fetches data from an HTTP endpoint responding to EdgeQL queries
TypeScript
8
star
26

edgedb-grafana-backend

Grafana backend plugin for EdgeDB
TypeScript
8
star
27

simpletodo

A preposterously simple todo app powered by EdgeDB
TypeScript
6
star
28

edgedb-wasm

Rust
5
star
29

homebrew-tap

Install nightly EdgeDB packages in Homebrew
Ruby
4
star
30

cla-bot

A GitHub bot and Web UI for managing contributor license agreements
TypeScript
4
star
31

website

A read-only mirror of the edgedb.com repo.
HTML
3
star
32

shared-client-testcases

3
star
33

action-release

A Github Action to automate package releases.
TypeScript
3
star
34

heroku-buildpack-edgedb

Run EdgeDB in a dyno along with your application.
Shell
2
star
35

libvirt-aws

Python
2
star
36

edgedb-js-lexer

Rust
1
star
37

edgedb-pkg

EdgeDB Release Packaging Toolkit
Dockerfile
1
star
38

chinook

A sample EdgeDb project seeded with chinook sample database
Python
1
star
39

packages-nix

Nix
1
star