Retrieving Information

Once you've written a specification function, you can retrieve facts matching that specification. When you need those facts in a single snapshot, you will run a query. But when you need to be continuously informed about those facts, then you will start a watch.

Query

The function j.query returns facts matching a specification as an array. The first parameter is a starting fact. The second is a preposition, which you create by calling j.for with a specification function.

const posts = await j.query(person, j.for(postsByAuthor));

Try it

You can extend a preposition using .then. This takes another specification function which continues where the previous one left off.

const tags = await j.query(person, j
    .for(postsByAuthor)
    .then(tagsForPost));

Try it

Watch

While j.query is done as soon as it returns the results, you might want to keep watching the results, for example to update a user interface. Call j.watch and pass in two functions: the first is called when a fact is added, and the second when a fact is removed.

const postWatch = j.watch(person,
    j.for(publishedPostsByAuthor),
    addPostToList,
    removePostFromList);

function addPostToList(post) {
    ...
    return postListItem;
}

function removePostFromList(postListItem) {
    ...
}

Try it

You can chain watches together to continue loading details within a list.

const titleWatch = postWatch.watch(
    j.for(titlesForPost),
    setPostTitle);

function setPostTitle(postListItem, postTitle) {
    postListItem.text(postTitle.value);
}

When you are done, be sure to call stop on the top level.

postWatch.stop();

This stops the entire tree of watches so that no further updates are attempted.

If you are using a front-end framework such as React, you will probably use an adapter like Jinaga React rather than creating watches directly.

query: j.query ( starterFact , preposition ) watch: j.watch ( starterFact , preposition , resultAdded , resultRemoved )  (   watch  . Watches can be chained together as shown above, however usually each level is assigned to its own constant. This allows one to call the "stop"-method on each level independently: const level_0_Watch = j.watch ( starterFact , preposition , resultAdded , resultRemoved ) const level_x+1_Watch = level_x_Watch .watch ( preposition , resultAdded , resultRemoved ) preposition: j.for ( Function that returns a specification ) then  .

Continue With

Create an App