Skip to content
Learni
View all tutorials
Bases de données

How to Configure EdgeDB with TypeScript in 2026

Lire en français

Introduction

EdgeDB is a modern relational database that combines PostgreSQL power with an advanced type system and an intuitive query language called EdgeQL. In 2026, its integration with TypeScript enables automatic generation of strict types, eliminating runtime data errors. This tutorial walks you through a complete, production-ready setup. You will learn how to structure your schema, run migrations, and interact efficiently from a Node.js application. The progressive approach ensures a solid understanding of core concepts before tackling complex use cases.

Prerequisites

  • Node.js 20+ and npm
  • TypeScript 5.5+
  • EdgeDB CLI installed (edgedb --version)
  • Basic knowledge of TypeScript and relational databases

Initialize the EdgeDB Project

terminal
mkdir edgedb-typescript-demo
cd edgedb-typescript-demo
edgedb project init --server-instance edgedb_demo

This command creates an EdgeDB project linked to a local instance. The edgedb folder is generated automatically and contains the schema and migrations.

Define the Main Schema

edgedb/default.esdl
module default {
  type User {
    required property name -> str;
    required property email -> str {
      constraint exclusive;
    }
    required property created_at -> datetime {
      default := datetime_current();
    }
    multi link posts -> Post;
  }

  type Post {
    required property title -> str;
    required property content -> str;
    required link author -> User;
  }
}

The schema defines two types linked by a relation. The constraint exclusive keyword ensures email uniqueness. Links enable typed and verified relationships.

Create and Apply the Migration

terminal
edgedb migration create
edgedb migration apply

EdgeDB automatically generates a migration file from the schema. The apply command updates the database safely and in a versioned manner.

Install the TypeScript Client

terminal
npm install edgedb
npm install --save-dev @types/node typescript

The edgedb package provides the official client with automatic type generation from the EdgeDB schema.

Configure the Typed Client

src/edgedb.ts
import { createClient } from 'edgedb';

export const client = createClient({
  dsn: process.env.EDGEDB_DSN,
  tlsSecurity: 'strict',
});

The client is configured with a DSN and strict TLS security. Types will be regenerated via edgedb generate.

Typed EdgeQL Query

src/queries.ts
import { client } from './edgedb';

const getUserWithPosts = async (email: string) => {
  return await client.querySingle<
    { name: string; posts: Array<{ title: string }> }
  >(`
    SELECT User {
      name,
      posts: { title }
    }
    FILTER .email = <str>$email
    LIMIT 1
  `, { email });
};

The query uses typed parameters and returns objects whose structure is verified by TypeScript. This prevents data structure errors.

Best Practices

  • Always regenerate types after schema changes with edgedb generate
  • Use explicit transactions for critical operations
  • Prefer parameterized queries to avoid injections
  • Store the DSN in environment variables
  • Systematically version migration files

Common Mistakes to Avoid

  • Forgetting to regenerate types after a schema change
  • Using raw queries without typing
  • Ignoring unique constraints in the schema
  • Not enabling TLS in production

Going Further

Deepen your EdgeDB expertise with our Learni courses dedicated to modern databases and TypeScript.