Gel Node.js Client

2.2.0 · active · verified Tue Apr 21

The `gel` package is the official Node.js client library for interacting with Gel databases. It provides a robust, type-safe API for executing queries, managing transactions, and interacting with Gel's data model. The current stable version, as per the prompt, is 2.2.0, with frequent minor and patch releases, often in conjunction with `@gel/generate` and `@gel/ai` packages, indicating active development and rapid iteration. Key differentiators include strong TypeScript support, an ORM-less approach focusing on direct query execution, automatic connection discovery for local development, and extensive query generation capabilities through its companion tools. It's designed for Node.js environments (v18.0.0+) and also supports TypeScript projects (v4.4.0+).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic client creation, connection configuration with transaction options, and executing various query types (`query`, `querySingle`, `queryRequiredSingle`) to handle different result cardinalities gracefully.

import { createClient, IsolationLevel } from 'gel';

async function runGelQuery() {
  // Create a client instance. For local development, it auto-discovers
  // connection info from `gel project init`. In production, set GEL_DSN env var.
  const client = createClient().withTransactionOptions({
    isolation: IsolationLevel.PreferRepeatableRead,
  });

  try {
    // Run a simple scalar query, expecting a single numeric result
    const resultScalar = await client.querySingle<number>('select 2 + 2');
    console.log(`Scalar query result: ${resultScalar}`); // Expected: 4

    // Run a query potentially returning multiple rows or an empty set
    const resultEmptySet = await client.query<string>('select <str>{}');
    console.log(`Empty set query result: ${resultEmptySet}`); // Expected: []

    // Run a query guaranteed to return exactly one element, using a shape type
    const resultRequiredSingle = await client.queryRequiredSingle<{ message: string }>(
      "select { message := 'Hello Gel!' }"
    );
    console.log(`Required single query result: ${resultRequiredSingle.message}`); // Expected: "Hello Gel!"

  } catch (error) {
    console.error('Gel query failed:', error);
  } finally {
    await client.close(); // Remember to close the client connection to release resources
  }
}

runGelQuery();

view raw JSON →