@memberjunction/record-graph
Generalized record dependency graph traversal, relationship collection resolution, topological sorting, link value encoding, and graph models for MemberJunction.
Shared across:
- Version History: Snapshot creation, dependency tracking, rollback / restore topological ordering.
- Record Cloning: Deep copying of record graphs, subtype rows, child collections, and soft-link rewriting.
- Metadata Sync: Deterministic 4-tier collection resolution and schema composition.
Installation
Section titled “Installation”npm install @memberjunction/record-graphKey Components
Section titled “Key Components”1. DependencyGraphWalker
Section titled “1. DependencyGraphWalker”Walks an entity record’s relationships to discover all dependent and referenced records.
import { CompositeKey } from '@memberjunction/core';import { DependencyGraphWalker } from '@memberjunction/record-graph';
const walker = new DependencyGraphWalker(provider); // provider is optional; defaults to Metadata.Providerconst userKey = CompositeKey.FromID(userId);const rootNode = await walker.WalkDependents( 'MJ: Users', userKey, { MaxDepth: 3, RequireTrackRecordChanges: false, // Set false for cloning, true for Version History IncludeSubtypes: true, // Include IS-A subtype rows FollowHierarchies: true, // Recurse self-referencing hierarchy fields EdgePolicy: (candidate) => { if (candidate.TargetEntityName === 'MJ: Audit Logs') return 'Skip'; if (candidate.Kind === 'ForwardFK') return 'Reference'; return 'Deep'; }, }, contextUser);
const flatNodes = walker.FlattenTopological(rootNode);Traversal Rules & Discovery Modes
Section titled “Traversal Rules & Discovery Modes”- Reverse Walk (Owned Children): Curated One-To-Many relationships (
EntityRelationshipInfo) are walked with full discovery (both reverse and forward), discovering all dependent child records. - Forward Walk (Referenced Records): Foreign key fields are walked in
forward-onlymode so referenced entities (e.g., shared lookup tables) do not traverse their own children, preventing graph explosion. - Subtypes (
IncludeSubtypes): Discovers IS-A subtype rows viaFindISAChildEntities. - Hierarchies (
FollowHierarchies): Allows controlled recursion on fields whoseConfiguration.Hierarchy.IsHierarchyis true. - Soft Links (
IncludeSoftLinks): Traverses polymorphicEntityID/RecordIDpairs viaEntityIDFieldName. - Non-Curated Inbound FKs (
ListNonCuratedInbound): Discovers database foreign keys pointing to the record that lack a curatedEntityRelationship. - Edge Policy (
EdgePolicy): Pre-traversal callback returning'Deep','Reference', or'Skip'. - Cycle Prevention: Global
visitedset and entity-typeancestorStackto prevent infinite loops and backtracking.
2. Topological Sorting (sort.ts)
Section titled “2. Topological Sorting (sort.ts)”SortByEntityDependencyOrder<T>(items, getEntityID, provider?): Computes DAG levels from foreign keys and sorts parent entities before child entities. Handles cycles safely.SortEntitiesByDependency: Backward-compatible alias forSortByEntityDependencyOrder.SortNodesTopologically(root: DependencyNode): Breadth-first traversal flattening a dependency tree into an execution-safe array where parents precede children.
3. Link Value Encoding (links.ts)
Section titled “3. Link Value Encoding (links.ts)”ResolveLinkValue(kindOrIsSoft, targetKey): Resolves the value to write into a pointer or foreign key column:- Hard link (
'hard'orfalse): bare primary key value (targetKey.GetValueByIndex(0)). - Soft link (
'soft'ortrue): canonical prefixed polymorphic encoding (targetKey.ToRecordID()).
- Hard link (
4. Collection Resolver (collection-resolver.ts)
Section titled “4. Collection Resolver (collection-resolver.ts)”ResolveCollectionRelationship(entityInfo, colName)(resolveCollectionRelationshipis a deprecated alias): Resolves a collection name to its matching relationship on the entity using strict 4-tier rule strength:- Explicit collection name in
RelatedRecordCollectionJSON ("Name": "...") - Relationship
DisplayName RelatedEntityfull name- Stripped
RelatedEntityname (with singular/plural tolerance)
- Explicit collection name in
5. SQL & Key Utilities (sql.ts, keys.ts)
Section titled “5. SQL & Key Utilities (sql.ts, keys.ts)”SqlEquals,SqlContains,SqlIn,SqlNotIn: Safe SQL filter builders (they escape withEscapeSQLStringfrom@memberjunction/global).BuildCompositeKeyFromRecord,BuildPrimaryKeyForLoad,BuildIdKey: CompositeKey construction utilities.- The camelCase names (
sqlEquals,buildIdKey, …) andescapeSqlStringare deprecated aliases. UseEscapeSQLStringfrom@memberjunction/globaldirectly.