Skip to Content
HeronJS v4 is now available.
Versionv4MongoDB & CQL

MongoDB, Cassandra, and ScyllaDB

Datasources initialize connections, select tenants, and own cleanup, just like SQL. Install mongodb for MongoDB or cassandra-driver for Cassandra/ScyllaDB. The driver loads when a connection is acquired; importing core/common does not load optional database drivers.

Define and inject a datasource

import {Module, DataSource} from '@heronjs/common'; import {MongoDataSource, MongoSource, useMongo} from '@heronjs/core'; @MongoSource({name: 'records', uri: 'mongodb://127.0.0.1:27017', database: 'records', profile: 'main', revision: '1'}) class RecordsSource extends MongoDataSource {} class RecordsService { constructor(@DataSource('records') private readonly source: RecordsSource) {} async find(id: string) { await using connection = await useMongo({source: this.source}); return await connection.value.database.collection<{id: string; label: string}>('records').findOne({id}); } } @Module({databases: [RecordsSource], providers: [RecordsService]}) class RecordsModule {}

Heron awaits initialization before dependent providers initialize. Default, named, and class tokens resolve to the same owned singleton. Shutdown closes owned clients once. Constructors and decorators perform no network I/O or DDL.

For standalone use, pass the same configuration to new MongoDataSource(options) or new CqlDataSource(options), await initialize(), and dispose with await using or dispose(). SQL uses the same lifecycle. Hooks require an initialized source.

CQL connections

import {CqlSource, CqlDataSource, useCql} from '@heronjs/core'; @CqlSource({ name: 'records', backend: 'cassandra', contactPoints: ['127.0.0.1'], localDataCenter: 'datacenter1', keyspace: 'records', profile: 'main', revision: '1', configure: () => ({options: {socketOptions: {readTimeout: 5000}}}), }) class RecordsSource extends CqlDataSource {} async function find(source: RecordsSource, id: string) { await using connection = await useCql({source: source}); return await connection.value.client.execute('SELECT * FROM records WHERE id = ?', [id], {prepare: true}); }

Use backend: 'scylladb' for ScyllaDB. Create keyspaces explicitly before startup. Schema changes, queries, transactions, validation, and migrations belong to application code or its chosen database tooling.

Tenant selection and ownership

resolveTenant(tenant) resolves a trusted connection configuration, synchronously or asynchronously. A profile identifies credentials/TLS settings; change revision when those settings rotate. Keep each setup callback stable for a configuration.

MongoDB shares clients by profile, revision, URI, and setup callback identity; database views can share a client. CQL additionally keys connections by backend, contact points, datacenter, and keyspace, and never changes a client’s keyspace with USE. Supplied CQL clients must match their configured keyspace.

await using connection = await mongo({source: 'records', tenant: 'tenant-a'}); // connection.value exposes {client, database} for MongoDB. await using tables = await cql({source: 'records', tenant: 'tenant-a'}); // tables.value exposes {client} for Cassandra/ScyllaDB.

useMongo/mongo and useCql/cql accept {source, tenant?}, where source is an instance, class/token, or registered name; omit the argument for default selection. An unregistered string in source selects a tenant on the default source. source.with({tenant}) creates an isolated view; disposing the view never closes the owning source. A native lease keeps its connection alive and must be released before owner shutdown completes. Use clients only inside the lease’s lifetime.

configure() runs when a new client is created. It can return driver options, a supplied client, owned, or a createClient hook. Supplied clients remain caller-owned by default; framework-created clients are owned. MongoDB initialization connects and pings; CQL initialization connects. Failed or late initialization closes acquired owned clients.

NoSQL limits default to 20 clients, 300,000 ms idle TTL, 10,000 ms connection timeout, and 30,000 ms shutdown timeout. Active leases prevent idle/capacity eviction; shutdown waits for release up to its timeout. SQL keeps its existing clients until owner disposal, preserving in-memory SQLite state.

For the removed NoSQL model/repository/schema APIs, see NoSQL migration.

Last updated on