Overview
A DataSource
in LoopBack 4 is a named configuration for a Connector instancethat represents data in an external system. The Connector is used bylegacy-juggler-bridge
to power LoopBack 4 Repositories for Data operations.
Creating a DataSource
It is recommended to use the lb4 datasource
commandprovided by the CLI to generate a DataSource. The CLI will prompt for allnecessary connector information and create the following files:
${dataSource.dataSourceName}.datasource.json
containing the connectorconfiguration${dataSource.dataSourceName}.datasource.ts
containing a class extendingjuggler.DataSource
. This class can be used to override the defaultDataSource behaviour programaticaly. Note: The connector configuration storedin the.json
file is injected into this class usingDependency Injection.Both the above files are generated insrc/datasources/
directory by the CLI.It will also updatesrc/datasources/index.ts
to export the new DataSourceclass.
Example DataSource Class:
import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';
export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';
constructor(
@inject('datasources.config.db', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}