Skip to Content
Athanor

Declare a node

A node is a record: identified, batched over one service, cached once per entity. Four things are load-bearing, and one row property makes tenant isolation possible.

A node is a record: identified, batched over one service, cached once per entity.

// server/athanor/nodes/vault.ts
import { Effect, Schema } from "effect";
import { VaultService } from "@athanor/custody/vault/service";
import { vaultTags } from "@athanor/custody/vault/tags";
import { Athanor } from "../alembic";

export const VaultSchema = Schema.TaggedStruct("Vault", {
  id: Schema.String,
  name: Schema.String,
  location: Schema.NullOr(Schema.String),
  provider: Schema.String,
});

export const VaultNode = Athanor.Node(VaultSchema, {
  load: (ids) =>
    VaultService.use((vaults) => vaults.byIds(ids)).pipe(
      Effect.map((rows) => new Map(rows.map((vault) => [vault.id, vault]))),
    ),
}).pipe(Athanor.Auth.Public, Athanor.Cache("hours", { tag: vaultTags.byId }));

Four things are load-bearing:

  • The typename comes from the schema's tag, not the binding name. It is a persisted identifier — it appears in global ids in URLs and in cache tags across deploys — so renaming VaultNode must stay a safe refactor.
  • load takes keys only. It structurally cannot see the request, which is what makes a shared cache entry safe: the entry is globalId → raw entity, viewer-independent.
  • Every node needs an access decision — Auth.Public or Auth.Authorize. It is a type-level requirement; an undecided node cannot be used.
  • Cache(life, { tag }) is the whole cache declaration. The tag builder should be the same string your domain already invalidates under, so hand-written updateTag sites and the graph agree during a migration.

Viewer-scoped nodes are cacheable

This surprises people. The entry is the raw entity and the gate runs per read, on top of it:

export const ClientNode = Athanor.Node(ClientSchema, {
  load: (ids) => ClientService.use((c) => c.clientsByIds(ids)).pipe(/* … */),
}).pipe(
  Athanor.Auth.Authorize((ctx, entity) => {
    const actor = ctx.viewer.actor;
    return (
      actor._tag === "User" &&
      String(actor.organizationId) === entity.organizationId
    );
  }),
  Athanor.Cache("minutes", { tag: clientTags.byId }),
);

The row must carry whatever the gate decides on. load cannot see the viewer, so organizationId has to be in the entity — otherwise tenant isolation has nowhere to live. That is the single most common mistake when adding a scoped node.

A denied read and an absent one are deliberately indistinguishable (node(id) returns null for both), so an always-available lookup is never an existence oracle.

On this page