Skip to Content
HeronJS v4 is now available.
Versionv4Data Sources

Data Sources

Datasource construction records configuration. Asynchronous initialization acquires the connections. Application lifecycle owns initialization and cleanup, including primary, replica and routing clients.

Configure a datasource

import 'reflect-metadata'; import {DataSourceClient, DataSourceDriver, type DataSourceConfig} from '@heronjs/common'; import {SqlDataSource, SqlSource} from '@heronjs/core'; const config: DataSourceConfig = { client: DataSourceClient.SQL, config: () => { const password = process.env.POSTGRES_PASSWORD; if (!password) throw new Error('POSTGRES_PASSWORD is required'); return { driver: DataSourceDriver.POSTGRES, host: 'localhost', port: 5432, user: 'app', password, database: 'app', pooling: {min: 1, max: 10}, }; }, }; @SqlSource(config) export class PostgresDatabase extends SqlDataSource {}

Install the database driver used by your configuration. SQL requires Knex 3.2.10 or newer; use mysql2 for MySQL/MariaDB. Pools validate connections on each checkout. Native constructors and the old datasource base classes are no longer public APIs.

Registration and use

Declare datasource classes in @Module({databases: [PostgresDatabase]}). app.start() or app.listen() initializes sources before dependent providers and owns shutdown. Standalone sources require explicit initialize() and dispose().

Import the SQL feature to register its typed acquisition functions. Native clients are available through disposable leases:

import {sql} from '@heronjs/core'; // Run after application startup in its application context. await using connection = await sql({source: PostgresDatabase}); const rows = await connection.value('records').select('id');

Finish queries, transactions and streams before releasing the lease. Disposal releases acquisition tracking; the application owns the pool.

Scoped datasource views

Use source.with({tenant: 'tenant-a'}) to create an isolated SQL, MongoDB, or CQL datasource view. The view shares the owner’s managed runtime, and disposing it does not close the owner. Scope options are copied when the view is created. DataSourceScopeOptions describes the options and ScopedDataSource describes the shared contract.

Named and tenant datasources

Set name in datasource configuration for named registration. A sole source is the default; multiple sources require an explicit default for default lookup. datasource({source: Source, tenant: tenant}) returns an isolated tenant view without mutating its owner. await sql({source: Source, tenant: tenant}) acquires its tenant query builder. Replicas remain configured through cluster.secondaries; set writable: true only on nodes intended to accept writes.

See resource hooks for selection and ambiguity rules, and MongoDB & CQL for optional drivers, managed connections and tenant leases.

Register with DataSources

@DataSources([PostgresDatabase]) accepts current SQL, MongoDB, and CQL datasource classes, as does @Module({databases: [...]}). Import DataSources from @heronjs/common. This support is available since 4.6.2; legacy classes can retain their explicit generic decorator calls.

Last updated on