Skip to content

Sources

defineReferences takes a builder callback. Each entry becomes a named source usable in fields.

Sources can be configured in two modes:

batch — fetch by IDs

Fetches only requested IDs. Supports batching, inflight deduplication, and negative caching.

ts
import { defineReferences } from '@nimir/references';

type User = { id: string; email: string };

const references = defineReferences(c => ({
  User: c.source<User>({
    batch: async ids => fetchUsers(ids),
    // batchSize: 200,       (max IDs per batch call, default 200)
    // ttlMs: 60_000,        (cache TTL in ms, default 4 hours)
    // keyBy: u => u.id,     (ID extractor, default item.id)
    // cache: ReferenceCache.new(createMemoryCache()),
  }),
}));

Options

OptionTypeDefaultDescription
batch(ids: string[]) => Promise<T[]>Fetch function, receives batched IDs
batchSizenumber200Max IDs per batch call
ttlMsnumber14_400_000 (4h)Cache TTL in milliseconds
keyBy(item: T) => stringitem => item.idID extractor
cacheReferenceCache<T>in-memoryPersistent cache adapter

list — fetch all

Fetches a full collection and resolves from it. Refreshes on TTL expiry.

ts
const references = defineReferences(c => ({
  Role: c.source<Role>({
    list: async () => fetchAllRoles(),
    // ttlMs: 60_000,
    // keyBy: r => r.id,
    // cache: ReferenceCache.new(createMemoryCache()),
  }),
}));

Options

OptionTypeDefaultDescription
list() => Promise<T[]>Fetch function, returns full collection
ttlMsnumber14_400_000 (4h)Cache TTL in milliseconds
keyBy(item: T) => stringitem => item.idID extractor
cacheReferenceCache<T>in-memoryPersistent cache adapter