Search
ctrl+/
Ask AI
ctrl+.
Light
Dark
System
Sign in

TypeScript

Install the client and optional (but recommended!) generator packages from npm using your package manager of choice.

npm
yarn
pnpm
bun
deno
Copy
$ 
npm install --save-prod gel          # database client
Copy
$ 
npm install --save-dev @gel/generate # generators
Copy
$ 
yarn add gel                 # database client
Copy
$ 
yarn add --dev @gel/generate # generators
Copy
$ 
pnpm add --save-prod gel          # database client
Copy
$ 
pnpm add --save-dev @gel/generate # generators
Copy
$ 
bun add gel                 # database client
Copy
$ 
bun add --dev @gel/generate # generators
Copy
$ 
deno add npm:gel                 # database client
Copy
$ 
deno add --dev npm:@gel/generate # generators

The gel package exposes a createClient function that can be used to create a new Client instance. This client instance manages a pool of connections to the database which it discovers automatically from either being in a gel project init directory or being provided connection details via Environment Variables. See the environment section of the connection reference for more details and options.

If you're using Gel Cloud to host your development instance, you can use the gel cloud login command to authenticate with Gel Cloud and then use the gel project init --server-instance <instance-name> command to create a local project-linked instance that is linked to an Gel Cloud instance. For more details, see the Gel Cloud guide.

Once you have a client instance, you can use the various query methods to execute queries. Each of these methods has an implied cardinality of the result, and if you're using TypeScript, you can provide a type parameter to receive a strongly typed result.

Copy
$ 
mkdir gel-js-example
Copy
$ 
cd gel-js-example
Copy
$ 
npm init -y
Copy
$ 
npm install gel
Copy
$ 
npm install --save-dev @gel/generate
Copy
$ 
npx gel project init --non-interactive
Copy
$ 
touch index.mjs
index.mjs
Copy
import { createClient } from "gel";
import assert from "node:assert";

const client = createClient(); // get connection details automatically

// Query always returns an array of result, even for single object queries
const queryResult = await client.query("select 1");
assert.equal(queryResult, [1]);

// querySingle will throw an error if the query returns more than one row
const singleQueryResult = await client.querySingle("select 1");
assert.equal(singleQueryResult, 1);

// queryRequired will throw an error if the query returns no rows
const requiredQueryResult = await client.queryRequired("select 1");
assert.equal(requiredQueryResult, 1);

// queryRequiredSingle will throw an error if
// - the query returns more than one row
// - the query returns no rows
const requiredSingleQueryResult = await client.queryRequiredSingle("select 1");
assert.equal(requiredSingleQueryResult, 1);

The @gel/generate npm package provides a set of generators that can make querying the database a bit more pleasant than manually constructing strings and passing explicit query element types to the query methods.

The interfaces generator will create TypeScript interfaces for the object types in your database.

Copy
$ 
npx @gel/generate interfaces
main.mts
Copy
import { createClient } from "gel";
import { Movie } from "./dbschema/interfaces";

const client = createClient();

const result = await client.query<Movie[]>(`
  select Movie {
    **,
    actors: { ** },
  };
`);

console.log(result);

The queries generator will create TypeScript functions for any EdgeQL queries defined in your project in separate .edgeql files.

get-movies.edgeql
Copy
select Movie {
  **,
  actors: { ** },
};
Copy
$ 
npx @gel/generate queries
main.mts
Copy
import { createClient } from "gel";
import { getMovies } from "./get-movies.query";

const client = createClient();

const result = await getMovies(client);

console.log(result);

The edgeql-js generator will create a fully type-safe query builder that you can use to write code-first queries in TypeScript. This is the recommended way to write dynamic queries and many people prefer it even for static queries.

Copy
$ 
npx @gel/generate edgeql-js
main.mts
Copy
import { createClient } from "gel";
import e from "./dbschema/edgeql-js";

const client = createClient();

const result = await e
  .params({ title: e.str }, (params) =>
    e.select(e.Movie, (m) => ({
      filter_single: e.op(m.title, "=", params.title),
      id: true,
      title: true,
      actors: { name: true },
    })),
  )
  .run(client, {
    title: "The Matrix",
  });

console.log(result);

If you haven't already done so, you can go through our quickstart tutorial to have a guided tour of using Gel as the data layer for a complex web application.

You will also find full reference information in this section of the documentation for the various generators and public APIs that the gel and @gel/generate packages provide.