TypeScript / JavaScript
Full program
Common JavaScript and TypeScript API for native Node and WebAssembly.Usage and examples
One policy, seven facts, five queries
This is the same document-access case as the
C Stable full program and
Python full program: ownership or delegation
permits access unless the user is blocked. blocked/1 remains runtime EDB data.
Use the WASM import for a browser/module-worker bundle or for WASM in Node.
Change only the import to @maelys-dev/datalog/node to use native Node. The code
is valid JavaScript as well as TypeScript; a browser needs a bundler or an ESM
URL mapping for the package import.
import { Engine, Predicate, ExplanationKind } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
engine.registerDomain('documents', [
Predicate.edb('user', 1),
Predicate.edb('owns', 2),
Predicate.edb('delegated', 2),
Predicate.edb('blocked', 1),
Predicate.idb('can_read', 2),
Predicate.idbQuery('has_any_document', 1),
Predicate.idbQuery('allow', 2),
], [])
const source = [
'can_read(User, Doc) :-',
' owns(User, Doc) or delegated(User, Doc),',
' not(blocked(User)).',
'has_any_document(User) :- owns(User, _).',
'allow(User, Doc) :- user(User), can_read(User, Doc).',
'',
].join('\n')
const rules = engine.loadInlineRuleset('documents', 'documents.main', source)
const input = rules.edb()
input.addFacts([
{ predicate: 'user', terms: ['alice'] },
{ predicate: 'user', terms: ['bob'] },
{ predicate: 'user', terms: ['mallory'] },
{ predicate: 'owns', terms: ['alice', 'roadmap.pdf'] },
{ predicate: 'delegated', terms: ['bob', 'roadmap.pdf'] },
{ predicate: 'owns', terms: ['mallory', 'roadmap.pdf'] },
{ predicate: 'blocked', terms: ['mallory'] },
])
const session = rules.prepare({
explanations: ExplanationKind.TRUE | ExplanationKind.FALSE,
})
const result = session.solve(input)
try {
console.log(result.containsFact('allow', ['alice', 'roadmap.pdf']))
console.log(result.containsFact('allow', ['bob', 'roadmap.pdf']))
console.log(result.containsFact('allow', ['mallory', 'roadmap.pdf']))
console.log(result.containsFact('has_any_document', ['alice']))
console.log(result.containsFact('has_any_document', ['bob']))
console.log(result.explainTrue('allow', ['alice', 'roadmap.pdf']))
console.log(result.explainFalse('allow', ['mallory', 'roadmap.pdf']))
} finally {
result.close()
}
} finally {
engine.close()
}The five membership answers implied by this policy and input are:
| Query | Answer |
|---|---|
allow(alice, roadmap.pdf) | true |
allow(bob, roadmap.pdf) | true |
allow(mallory, roadmap.pdf) | false |
has_any_document(alice) | true |
has_any_document(bob) | false |
Bob has delegated access but owns no document. Mallory owns the document but is blocked. The policy has no quoted constants, so the atom list stays empty; user names and the document name arrive through the seven input facts.
Cleanup and a second request
The inner finally releases the result lease. The outer finally closes the
Engine and cascades over the remaining session, input and ruleset, including if
loading or solving failed. For a service that keeps the Engine open, reuse the
prepared session and call input.reset() before supplying the next request's
complete facts. Close the previous result before solving again on that session.
The solving guide shows that reuse pattern. The querying guide explains copied values, result-bound terms and explanation statuses; the manifest guide replaces inline loading with a verified file or memory bundle.