TypeScript / JavaScript

Engine

Engine class and methods for the common JavaScript and TypeScript API.

Examples that omit setup use the page reference context, which supplies engine, manifestJson, policySource and the binding imports.

The reference below follows the same class → member order as the JavaScript API. The overview explains runtime selection; the full program shows these methods together. See EngineOptions for creation options.

Class: Engine

The Engine is the root of the JavaScript resource tree. Use Engine.create() to initialize it. The private constructor() in the TypeScript declaration means applications cannot call new Engine(); limits is a read-only Limits snapshot. The native Node and WebAssembly entry points share this class.

TypeScript · classSince v0.15.0
Engine
The root owner of independent rulesets and their descendants. Create it with Engine.create(); close releases its full resource tree.
Creation and access

No public constructor.

  • factory
    Engine.create()

    Initialize a fresh engine for the selected runtime and profile.

Attributes
limitsReadonly<Limits>readonly field
Build limits for this engine profile.
Methods
Protocols
  • using engineLeave the scope to close this Engine, all of its rulesets and their sessions, results and input buffers.
Example
console.log('max predicates:', engine.limits.maxPredicates)

Factories

Engine.create

TypeScript · static methodSince v0.15.0
Engine.create
Engine.create(
options?: EngineOptions
): Promise<Engine>
Initialize a fresh engine for the selected runtime and profile.
Arguments
Runtime profile and optional WASM asset resolver.
Return value
Promise<Engine>

Resolves to a new Engine. Rejects unsupported platforms, missing assets or initialization failures; no runtime fallback is selected.

Example
import { Engine } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
  console.log('max predicates:', engine.limits.maxPredicates)
} finally {
  engine.close()
}
Outputv0.27.0, small profile
max predicates: 128

Methods

Each method below has its own signature, arguments, return behavior and stable link.

Engine.close

TypeScript · methodSince v0.15.0
Engine.close
Engine.close(): void
Release this resource and its owned descendants. Closing is idempotent.
Return value
void

Returns nothing. Repeated closing succeeds; operational calls after closing fail with INVALID_STATE.

Example
{
  const engine = await Engine.create({ profile: 'small' })
  try {
    // Closing also releases this engine's rulesets, inputs and sessions.
  } finally { engine.close() }
}
console.log('engine: closed')

Engine.loadInlineRuleset

TypeScript · methodSince v0.15.0
Engine.loadInlineRuleset
Engine.loadInlineRuleset(
domain: string,
id: string,
source: string
): Ruleset
Compile one source policy against an already registered domain.
Arguments
domainstring
Name of an already registered domain.
idstring
Policy identifier within the ruleset.
sourcestring
Datalog source text; quoted policy atoms must be declared.
Return value
Ruleset

Returns Ruleset. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
  engine.registerDomain('documents', [
    Predicate.edb('user', 1), Predicate.idbQuery('allow', 1),
  ], [])
  const source = 'allow(User) :- user(User).'
  const ruleset = engine.loadInlineRuleset('documents', 'documents.main', source)
  try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }
} finally { engine.close() }
Outputv0.27.0, small profile
policies: 1

Engine.loadManifest

TypeScript · methodSince v0.15.0
Engine.loadManifest
Engine.loadManifest(
input: ManifestInput,
options?: ManifestOptions
): Ruleset
Validate and load enabled policies from a manifest, including source hashes and permissions.
Arguments
Native path or in-memory manifest with its policy source bundle.
Explicit manifest permissions; omitted flags remain disabled.
Return value
Ruleset

Returns Ruleset. Closed or incompatible resource ownership and invalid inputs throw; engine failures include a copied diagnostic.

Example
// The manifest contains the SHA-256 of this exact policySource.
const ruleset = engine.loadManifest({
  text: manifestJson,
  policies: [{ policyId: 'documents.main', source: policySource }],
})
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }

Engine.registerDomain

TypeScript · methodSince v0.15.0
Engine.registerDomain
Engine.registerDomain(
name: string,
predicates: readonly Predicate[],
atoms?: readonly string[]
): void
Copy predicate declarations and allowed source atoms into the engine registry.
Arguments
namestring
Name of the predicate or export.
predicatesreadonly Predicate[]
Predicate declarations copied during registration.
atoms?readonly string[]
Permitted symbols appearing in policy source; runtime strings need no declaration.
Return value
void

Returns nothing on success. Invalid JavaScript inputs throw TypeError or RangeError; native failures carry MaelysDatalogError and a diagnostic.

Example
import { Engine, Predicate } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
  engine.registerDomain('documents', [
    Predicate.edb('user', 1),
    Predicate.idbQuery('allow', 1),
  ], [])
  console.log('domain: registered')
} finally {
  engine.close()
}
Outputv0.27.0, small profile
domain: registered

Protocols

using engine

TypeScript · protocolSince v0.15.0
using engine
Leave the scope to close this Engine, all of its rulesets and their sessions, results and input buffers.
Behavior

Exiting the using scope disposes the resource, including on exceptional exit. Cleanup delegates to close() and is idempotent.

Example
{
  using engine = await Engine.create({ profile: 'small' })
  console.log('max EDB facts:', engine.limits.maxEdbFacts)
}
Explicit form

Perform the same operations with explicit cleanup in a finally block.

{
const engine = await Engine.create({ profile: 'small' })
try {
  console.log('max EDB facts:', engine.limits.maxEdbFacts)
} finally {
  engine.close()
}
}

Interfaces

EngineOptions · ManifestOptions.

EngineOptions configures Engine.create().

EngineOptions

TypeScript · interfaceSince v0.15.0
EngineOptions
Select the size profile and optionally locate a matching WASM factory and binary.
Fields
profile?'small' | 'large'field
SMALL by default; LARGE has independently compiled bounds.
wasmFactory?(options: { locateFile?: (file: string) => string }) => Promise<WasmModule>field
Optional factory from the same artifact set as the selected binary.
wasmUrl?string | URLfield
Optional URL for the matching WASM binary.
Example
const options: EngineOptions = { profile: 'small' }
const configured = await Engine.create(options)
try {
  console.log('profile:', options.profile)
} finally { configured.close() }

The manifest declarations define the inputs and permissions for Engine.loadManifest().

ManifestOptions

TypeScript · interfaceSince v0.15.0
ManifestOptions
Explicit permissions for test-only policies and undeclared source atoms; hashes remain verified.
Fields
allowTestOnly?booleanfield
Permit policies marked test_only; disabled by default.
allowUndeclaredPolicyAtoms?booleanfield
Permit undeclared source atoms; disabled by default.
Example
const options: ManifestOptions = { allowTestOnly: true }
const ruleset = engine.loadManifest({
  text: manifestJson,
  policies: [{ policyId: 'documents.main', source: policySource }],
}, options)
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }

ManifestInput.

ManifestInput

TypeScript · typeSince v0.15.0
ManifestInput
Declaration
export type ManifestInput = { path: string; text?: never; policies?: never } |
  { text: string; policies: readonly { policyId: string; source: string }[]; path?: never };
Load a filesystem path in native Node, or a manifest and source bundle in either runtime.
Example
const input: ManifestInput = {
  text: manifestJson,
  policies: [{ policyId: 'documents.main', source: policySource }],
}
const ruleset = engine.loadManifest(input)
try { console.log('policies:', ruleset.policyCount) } finally { ruleset.close() }

Usage guides