TypeScript / JavaScript

Querying guide

Examples and practical guidance for the JavaScript and TypeScript SolveResult API.

The SolveResult reference contains the complete class and method cards.

Usage and examples

Membership and enumeration

After solving the seven-fact document-access request:

CODE
const allowed = result.containsFact('allow', ['alice', 'roadmap.pdf'])
const denied = result.containsFact('allow', ['mallory', 'roadmap.pdf'])
const rows = result.enumeratePredicateFacts('allow', 2)

Membership returns a boolean. A missing fact is a normal false; an invalid predicate, arity or query permission throws. can_read/2 is deliberately internal in this example, so query allow/2 instead. Enumeration copies the resolved derived IDB rows of the requested authorized predicate.

Copied values and result-bound terms

OperationWhat it returnsAfter closing the result
enumeratePredicateFacts(name, arity)Arrays of strings, bigint integers and booleansCopies remain usable
enumerateRaw(name, arity)Arrays of ResultTerm objectsTerms can no longer be resolved
result.resolveTerm(term) or term.resolve()The resolved JavaScript valueRequires the term's original result to be open

A raw symbol's numeric identity belongs to one result. Do not reuse it with another result or send it to another worker as a portable symbol identifier. Resolve it while its owner is alive, then send the copied value. For JSON display, convert bigint values to decimal strings explicitly; choose an application schema if the receiver must distinguish those integers from ordinary symbol strings.

Explain a decision explicitly

CODE
const explanation = result.containsFact('allow', ['mallory', 'roadmap.pdf'])
  ? result.explainTrue('allow', ['mallory', 'roadmap.pdf'])
  : result.explainFalse('allow', ['mallory', 'roadmap.pdf'])
console.log(explanation)

Both methods return canonical engine text. Inspect the document's status: complete, truncated, not-derived and not-applicable have different meanings. A returned truncated document is not a thrown capacity error and is not a complete proof of absence. The authorization answer remains the membership query.

A symbol absent from the result can produce false from membership but NOT_FOUND from an explanation. Decide how the application presents that case; do not convert all explanation failures into a fabricated negative decision.

For what a witness or a bounded refutation promises, read Explanations. Its document reference defines statuses, limits and obstacle names.