Skip to content

TypeScript API

Reference for the @pthm/melange TypeScript runtime client. Mirrors the Go API with TypeScript idioms. Ships with the type-safe generated client from melange generate client --runtime typescript.

Checker

import { Checker } from '@pthm/melange';
import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const checker = new Checker(pool);

Permission Checks

class Checker {
  check(subject: MelangeObject, relation: Relation, object: MelangeObject, contextualTuples?: ContextualTuple[]): Promise<Decision>
  checkWithContextualTuples(subject: MelangeObject, relation: Relation, object: MelangeObject, tuples: ContextualTuple[]): Promise<Decision>
}

check resolves to a Decision; decision.allowed is true if the subject has the relation on the object. checkWithContextualTuples has the Go signature, but the TypeScript client does not apply contextual tuples yet: it runs a plain check_permission and skips the cache. Use the Go Checker for contextual tuples.

List Operations

class Checker {
  listObjects(subject: MelangeObject, relation: Relation, objectType: ObjectType, page?: PageOptions): Promise<ListResult<string>>
  listSubjects(subjectType: ObjectType, relation: Relation, object: MelangeObject, page?: PageOptions): Promise<ListResult<string>>
}

ListResult is { items: string[]; nextCursor?: string }. Cursor-paginated. Pass page.after (the previous page’s nextCursor) to continue.

Explain

class Checker {
  explain(subject: MelangeObject, relation: Relation, object: MelangeObject, options?: ExplainOptions): Promise<Trace>
}

interface ExplainOptions {
  maxNodes?: number;  // 0 or absent defers to session GUC / built-in default
}

Returns the resolution tree for a check. See the Explaining Decisions guide for the trace structure.

const trace = await checker.explain(
  { type: 'user', id: 'alice' },
  'viewer',
  { type: 'document', id: '1' },
);

if (trace.result === true) {
  console.log('allowed via', trace.root.type);
}

Trace, TraceNode, NodeType, TupleRef, and SubjectRef type mirrors live in @pthm/melange (source: clients/typescript/src/trace.ts). JSON tags are snake_case to match the SQL columns.

Expand

class Checker {
  expand(object: MelangeObject, relation: Relation, options?: ExpandOptions): Promise<UsersetTree>
  expandRecursive(object: MelangeObject, relation: Relation, options?: ExpandOptions): Promise<string[]>
}

interface ExpandOptions {
  subjectType?: ObjectType;  // Melange extension: narrow Leaf.Users to one type
  maxLeaf?: number;          // Melange extension: cap Leaf.Users; 0 = unbounded (matches OpenFGA)
}

function flattenUsers(tree: UsersetTree): string[]

expand returns the OpenFGA-shaped UsersetTree, shallow by default (computed rewrites surface as Leaf.Computed pointers, TTUs as Leaf.TupleToUserset pointers). expandRecursive walks the pointer chains and returns the flat, deduplicated user list. flattenUsers collects every Leaf.Users entry from an already-returned tree without issuing additional queries.

See the Expanding Permissions guide for the tree structure and worked examples.

const tree = await checker.expand(
  { type: 'document', id: '1' },
  'viewer',
);
const users = flattenUsers(tree);
// users = ['user:alice', 'user:bob', 'group:eng#member', 'user:*']

UsersetTree, UsersetTreeNode, Leaf, Users, Computed, TupleToUserset, Difference, and Nodes type mirrors live in @pthm/melange (source: clients/typescript/src/expand.ts). Field names match OpenFGA’s openfgav1.UsersetTree proto so existing OpenFGA tooling deserialises the JSON without adapters.

Errors

Every Checker method that calls a public function maps two database errors (see Errors): SQLSTATE M2003, a relation the SQL API exposure leaves out, throws ValidationError with code 2000 (ErrorCodeValidation) and the database message, and a missing melange function (42883) throws MissingFunctionError. Both keep the driver error as cause. Other errors are thrown unchanged.

import { ValidationError, ErrorCodeValidation } from '@pthm/melange';

try {
  await checker.check(user, 'viewer', doc);
} catch (err) {
  if (err instanceof ValidationError && err.code === ErrorCodeValidation) {
    // e.g. relation "viewer" on type "document" is not exposed
  }
}

Caching

import { Checker, MemoryCache } from '@pthm/melange';

const cache = new MemoryCache(300_000); // TTL in milliseconds
const checker = new Checker(pool, { cache });

MemoryCache is the in-memory implementation. See Caching for the interface and custom backend guidance.

Generated Code

melange generate client --runtime typescript produces type-safe constants and factory functions, and the AccessibleFunctions and AccessibleByParentFunctions maps of the installed accessible function names. See Generated Code for details.

Calling SQL Directly

For flows outside the client (background jobs, scripts, ad-hoc queries), call the generated SQL functions from any PostgreSQL client:

import { Pool } from 'pg';
import type { Trace } from '@pthm/melange';

const { rows } = await pool.query<{ explain_permission: Trace }>(
  'SELECT explain_permission($1, $2, $3, $4, $5)',
  ['user', 'alice', 'viewer', 'document', '1']
);
const trace = rows[0].explain_permission;

See the SQL API Reference for all functions.

Next Steps