TypeScript / JavaScript
Registries and Predicates
Examples and practical guidance for the JavaScript and TypeScript Predicate API.The Predicate reference lists every constructor and factory method.
Usage and examples
Register a domain
A Predicate describes a name, an arity (the number of terms), and where its facts come from. A factory such as Predicate.edb() creates that description. Before loading a policy, create an Engine and pass the complete list of descriptions to engine.registerDomain(). This registers the vocabulary under a domain name; it does not add request facts or compile a policy.
This browser example uses the WASM entry point. With a bundler configured for the published package, it can run as a JavaScript module:
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
const predicates = [
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),
]
engine.registerDomain('documents', predicates)
} finally {
engine.close()
}blocked/1 is supplied with each request, so it is EDB. can_read/2 is derived inside a policy; allow/2 and has_any_document/1 are derived relations that callers may query. The optional third argument of registerDomain lists strings permitted as quoted atoms in policy source.
| Predicate factory | Role |
|---|---|
Predicate.edb(name, arity) | Application-supplied facts |
Predicate.edbQuery(name, arity) | Application-supplied facts that may also be queried |
Predicate.idb(name, arity) | Internal derived relation |
Predicate.idbQuery(name, arity) | Derived relation exposed to queries |
Predicate.policyFact(name, arity) | Facts written in policy source |
Predicate.policyFactQuery(name, arity) | Source facts exposed to queries |
Registry scope
Native Node. Engines that load the same native addon and profile in one Node process use a shared domain registry, including across worker threads. Here the second Engine loads a policy using the domain registered by the first:
import { Engine, Predicate } from '@maelys-dev/datalog/node'
const first = await Engine.create({ profile: 'small' })
try {
const second = await Engine.create({ profile: 'small' })
try {
first.registerDomain('demo', [
Predicate.edb('request', 1),
Predicate.idbQuery('allow', 1),
])
const ruleset = second.loadInlineRuleset(
'demo', 'demo.allow', 'allow(X) :- request(X).'
)
try {
console.log(ruleset.policyCount)
} finally {
ruleset.close()
}
} finally {
second.close()
}
} finally {
first.close()
}Closing first releases its resources but does not erase the process-wide domain declaration. Re-registering the same declaration is accepted; changing its meaning under the same name is rejected.
Browser WASM. By default, each Engine.create() creates its own WASM module and domain registry. Register a domain in each Engine that will load policies for it:
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const first = await Engine.create({ profile: 'small' })
try {
const second = await Engine.create({ profile: 'small' })
try {
const predicates = [
Predicate.edb('request', 1),
Predicate.idbQuery('allow', 1),
]
first.registerDomain('demo', predicates)
second.registerDomain('demo', predicates)
} finally {
second.close()
}
} finally {
first.close()
}The two WASM registries receive equivalent declarations, but each owns its copy. A policy loaded into either Engine uses that Engine's registered domain.