$ open source · GPL-3.0 — need a commercial license? imqueue.com →

BaseModel.toGraph() method

Builds a graph of this model's associations, following them transitively.

Signature:

static toGraph(graph?: Graph<typeof BaseModel>): Graph<typeof BaseModel>;

Parameters

Parameter

Type

Description

graph

Graph<typeof BaseModel>

(Optional) Graph to add to. A fresh one by default, and passing the same one is what makes the recursive calls accumulate into a single graph.

Returns:

Graph<typeof BaseModel>

The graph, so the outermost call can use it.

Remarks

Every association becomes an edge, and a many-to-many contributes two: this model to the through-model, and the through-model to the target. Each target is then walked in turn, and an association already present as an edge is not followed again, which is what stops a cycle from recursing forever.

The reason to have it is to find those cycles before a query does — a cycle in the graph is a query that can be asked to include itself.