TypeScript / JavaScript
Session and solving guide
Examples and practical guidance for the JavaScript and TypeScript Session API.The Session reference contains the complete class and method cards.
Usage and examples
Prepare storage and reuse it across requests
The following fragment belongs inside the lifetime of the documents ruleset from the full program:
import { ExplanationKind, type Fact } from '@maelys-dev/datalog/wasm'
const input = rules.edb()
const session = rules.prepare({
policyIndex: 0,
capacities: { inputFacts: 7, derivedFacts: 16 },
explanations: ExplanationKind.TRUE | ExplanationKind.FALSE,
})
function evaluate(facts: Iterable<Fact>) {
input.reset()
input.addFacts(facts)
const result = session.solve(input)
try {
return {
allowed: result.containsFact('allow', ['alice', 'roadmap.pdf']),
rows: result.enumeratePredicateFacts('allow', 2),
}
} finally {
result.close()
}
}The returned booleans and resolved rows are copies. They remain usable after
result.close(). Keep one live result at most per session; close it before the
next solve. Distinct sessions can hold distinct results. A failed solve publishes
no partial result and leaves the EDB available for correction or reset.
Preparation options
| Option | Contract |
|---|---|
policyIndex | Selects one loaded policy; zero is the default |
capacities | Optional inputFacts, derivedFacts, symbols, textBytes; omitted fields keep profile defaults, zero is explicit |
requiredCapabilities | Requests feature admission before solving; unsupported requirements fail |
workLimit | Requests a backend work limit; the current reference backend rejects a nonzero limit |
explanations | Opt-in reusable workspace for TRUE, FALSE, or their bitwise combination |
Read session.capacities for the resolved quotas. Reserving explanation workspace
does not turn a query into an automatic explanation, nor does it remove string,
object and conversion allocations. Request the explanation explicitly when it
is needed.
rules.fingerprint, session.fingerprint and session.executionFingerprint
identify policy and execution configuration. The result exposes the matching
session and execution identities. An execution fingerprint excludes the input
facts; record those separately when an application needs request-level evidence.