Page Contents
A datasource can be created at runtime by creating an instance of
juggler.DataSource
. It requires a name for the datasource, the connector, and
the connection details.
import {juggler} from '@loopback/repository';
const dsName = 'bookstore-ds';
const bookDs = new juggler.DataSource({
name: dsName,
connector: require('loopback-connector-mongodb'),
url: 'mongodb://sysop:moon@localhost',
});
await bookDs.connect();
app.dataSource(bookDs, dsName);
For details about datasource options, refer to the DataSource documentation.
Attach the newly created datasource to the app by calling app.dataSource()
.
Note:
The app.datasource()
method is available only on application classes
with RepositoryMixin
applied.