Global hooks and resource ownership
Application startup (start() or listen()) installs hooks and noun acquisition functions on globalThis. HeronJS.create() returns the configurable application before initialization. After startup, applications use hooks directly; no manual hook installation is required. The useX hooks remain available, and the noun functions share their implementations and inferred types.
The stdio logging capability takes effect during application creation so setup diagnostics stay off stdout. Closing the application releases that logging protection even if it was never started.
import {HeronJS} from '@heronjs/core';
import {MongoDataSource, MongoSource} from '@heronjs/core';
import {Module} from '@heronjs/common';
@MongoSource({name: 'documents', uri: 'mongodb://localhost:27017', database: 'documents', profile: 'main', revision: '1'})
class Documents extends MongoDataSource {}
@Module({databases: [Documents]})
class AppModule {}
await using app = await HeronJS.create({module: AppModule});
await app.start();
using a = datasource({source: Documents});
await using b = datasource({source: Documents});
await using connection = await mongo({source: a.value});
await connection.value.database.collection('events').insertOne({kind: 'started'});datasource, cql and http are callable APIs, independent of the variable name. A synchronous borrowed lease supports both using a = datasource({source: Source}) and await using a = datasource({source: Source}). Native SQL/MongoDB/CQL acquisition is asynchronous: await using a = await cql({source: Source}). The first await waits for disposal; the initializer’s await waits for acquisition. Read the acquired resource through .value.
Datasource and tenant selection
| Call | Selection |
|---|---|
datasource() | Default registered datasource |
datasource({source: Source}) | Class/typed token, with inferred instance type |
datasource({source: 'documents'}) | Datasource registered as documents, when that name exists |
datasource({source: 'tenant-a'}) | Default datasource scoped to tenant-a, when no datasource has that name |
datasource({source: 'documents', tenant: 'tenant-a'}) | Named datasource scoped to tenant-a |
datasource({source: undefined, tenant: 'tenant-a'}) | Default datasource scoped to tenant-a, even if that tenant matches a datasource name |
datasource({source: Source, tenant: 'tenant-a'}) | Typed tenant view of the selected source |
An omitted or unregistered datasource name falls back to the default source. Set tenant explicitly in the options object. An unregistered string in source also selects that tenant on the default source. A sole registered datasource is the default; applications with multiple datasources must designate a default to use default lookup. Missing defaults fail with RESOURCE_UNAVAILABLE.
Tenant views share their owner’s managed pools and do not mutate the shared source. Their native client hooks and SQL repository operations use the selected tenant. MongoDB and CQL resolve tenant connection settings through resolveTenant; SQL uses the selected tenant database. Releasing a view does not close its application-owned datasource. A tenant view has the backend’s framework type, rather than promising subclass-specific behavior.
using selected = datasource({source: Documents, tenant: 'tenant-a'});
await using connection = await mongo({source: selected.value});
// Equivalent native acquisition, with no intermediate source handle:
await using named = await mongo({source: 'documents', tenant: 'tenant-a'});sql, mongo, cql and their useX hooks accept {source, tenant?}, with a datasource instance, typed class/token, or registered name in source; omit the argument for default selection. They verify that the selected source belongs to the requested backend. String/default source lookup returns unknown because an untyped name cannot establish a compile-time resource type; class/token lookup preserves inference. No caller-selected generic can invent a type for a string.
Globals and application context
Core/common typings include globals automatically. Importing @heronjs/core registers SQL, MongoDB, CQL, and cache hook definitions and declarations; protocol hooks come from their package roots. Definitions do not acquire engines or load optional drivers. Loaded features become global at application startup; a feature loaded later joins the active installation. Package imports alone do not install globals.
Globals resolve the current async application scope. Outside a scoped operation, the sole running application is the default. When multiple applications are running, an unscoped call fails with AMBIGUOUS_CONTEXT; it never picks the last-created application. HTTP callbacks establish their owning context automatically. Bootstrap and background work started there inherit their application scope. app.run(callback) remains available to explicitly select an application for external callbacks in multi-application processes.
Imported functions continue to be available after globals are removed, but resource lookup without a running application fails with NO_CONTEXT. Access through a closed application scope fails with CONTEXT_CLOSED.
Import @heronjs/common for ambient declarations. Applications need no separate globals import or manual installation.
| Acquisition function | Retained hook | Result and disposal |
|---|---|---|
container() | useContainer() | Borrowed container lookup; never closes the container |
datasource({source: Source}) | useDataSource({source: Source}) | Borrowed framework resource |
http() | useHttp() | Borrowed HTTP engine; never closes its listener |
rpc() | useRpc() | Borrowed router/plugin access |
await sql({source: Source}) | await useSql({source: Source}) | Query builder lease; releases acquisition tracking |
await mongo({source: Source}) | await useMongo({source: Source}) | MongoDB client/database lease |
await cql({source: Source}) | await useCql({source: Source}) | CQL client/keyspace lease |
cache() | useCache() | Borrowed cache engine |
mcp(context) / acp(context) | useMcp(context) / useAcp(context) | Borrowed active protocol context |
messaging(context) | useMessaging(context) | Borrowed broker delivery context |
Disposal
Access through a disposed lease fails with RESOURCE_RELEASED. Cleanup is idempotent, including failed asynchronous cleanup. Borrowed disposal does not call the native instance’s close, destroy or disconnect. Finish cursors, streams, subscriptions and transactions before releasing their client lease. A previously copied native reference cannot be revoked by the lease.
When returning asynchronous work from an await using scope, use return await work() so disposal waits for that work. Standard JavaScript disposal preserves both body and cleanup failures as SuppressedError. await using app = await HeronJS.create(...) owns application shutdown; resource leases only own their acquisition.