Gel Node.js Client
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
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` with the `gel` package, which is primarily designed for ES Modules (ESM) imports.fixChange `const gel = require('gel')` or `const { createClient } = require('gel')` to `import * as gel from 'gel'` or `import { createClient } from 'gel'`. Ensure your project is configured for ESM (e.g., by adding `"type": "module"` to your `package.json`). -
Error: Connection failed: Host unreachable
cause The Gel client could not establish a connection to the Gel database instance. This commonly indicates an incorrect Data Source Name (DSN), a non-running Gel instance, or network/firewall issues.fixVerify that your `GEL_DSN` environment variable is correctly configured, ensure your Gel database instance is running and accessible at the specified host/port, and check any local or cloud firewall rules. -
TypeError: Cannot read properties of null (reading 'property')
cause You are using `.querySingle()` which returns `T | null`, and attempting to access properties on the result without first checking if it's `null`.fixAlways check for `null` when using `.querySingle()`: `const user = await client.querySingle<{ name: string }>('select User { name } limit 1'); if (user) { console.log(user.name); } else { console.log('User not found'); }` -
Error: Query returned more than one element
cause You are using `.queryRequiredSingle()` for a query that returned zero or multiple elements, violating its guarantee of exactly one result.fixIf your query might return zero or many elements, use `.querySingle()` (for zero or one element, returning `T | null`) or `.query()` (for any number of elements, returning `T[]`) instead of `.queryRequiredSingle()`.
Warnings
- breaking The `gel` client library officially requires Node.js version 18.0.0 or higher. Earlier Node.js versions are not supported and may lead to runtime errors or unexpected behavior.
- breaking TypeScript users must ensure their project is configured for TypeScript 4.4 or higher to correctly interpret the client's shipped type definitions and generated code.
- gotcha The `.query()`, `.querySingle()`, and `.queryRequiredSingle()` methods have distinct behaviors regarding result cardinality. `.query()` always returns an array. `.querySingle()` returns `T | null` for zero or one elements. `.queryRequiredSingle()` guarantees exactly one `T` element, throwing an error if zero or multiple are returned.
- gotcha For production environments, the Gel client relies on the `GEL_DSN` environment variable for connection details. Auto-discovery only works for local development within a `gel project` initialized directory.
- breaking Underlying Gel server changes in version 6.0 introduced breaking changes to the embeddings and RAG endpoints. While the core client package (gel) itself didn't have direct breaking changes, users interacting with Gel's AI extensions via `@gel/ai` should consult its release notes and the Gel 6.0 server documentation for compatibility.
Install
-
npm install gel -
yarn add gel -
pnpm add gel
Imports
- createClient
const { createClient } = require('gel');import { createClient } from 'gel'; - Client
import { Client } from 'gel';import type { Client } from 'gel'; - IsolationLevel
const IsolationLevel = require('gel').IsolationLevel;import { IsolationLevel } from 'gel';
Quickstart
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();