When we defined the Construction.Project
type, we used Jinaga.User
as one of its properties.
That made it a predecessor.
In this case, the meaning of that predecessor relationship is that the user created the project.
Once you have predecessors, you can query for facts that are related to them. These, as you might have guessed, are called successors.
Let's query for all projects that a user has created.
// Create a couple more projects.
const projectB = await j.fact(new Project(user, crypto.randomUUID()));
const projectC = await j.fact(new Project(user, crypto.randomUUID()));
const projectsCreatedByUser = model.given(User).match(u =>
u.successors(Project, p => p.creator)
);
const projects = await j.query(projectsCreatedByUser, user);
Let's break down that specification.
Start with the model that you built from all of your types.
Call given
to specify the type of the parameter.
Then use match
to write an expression that matches the facts you want.
The successors
method finds all successors of the requested type related to the given predecessor.
Provide a lambda that shows how the successors relate to the predecessor.