-
Notifications
You must be signed in to change notification settings - Fork 1.1k
loopback-datasource-juggler datasource definition for setup method is missing #7115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think the setup() method your are trying to call is called inside the constructor. I think you are trying to dynamically specify parameters ? Can't see your code above but you have to options if that's what you want to achieve. new DataSource(yourDsname, Yoursettings) or even more if you see the code What I usually is to provide to my micro services environment variables with the connection property values such as: const config = {
name: 'session2',
connector: 'mysql',
url: '',
host: process.env.SESSION_DB_HOST,
port: process.env.SESSION_DB_PORT,
user: process.env.SESSION_DB_USER,
password: process.env.SESSION_DB_PASSWORD,
database: process.env.SESSION_DB
}; |
@andreyunugro , here is a complete example. import {inject, lifeCycleObserver, LifeCycleObserver, ValueOrPromise} from '@loopback/core';
import {AnyObject, juggler} from '@loopback/repository';
const config = {
name: 'demo',
connector: 'mysql',
url: '',
host: '',
port: 3306,
user: '',
password: '',
database: '',
};
function updateConfig(dsConfig: AnyObject) {
if (process.env.DEMO_DB_HOST) {
dsConfig.host = process.env.DEMO_DB_HOST;
dsConfig.port = +process.env.DEMO_DB_PORT!;
dsConfig.user = process.env.DEMO_DB_USER;
dsConfig.password = process.env.DEMO_DB_PASS;
dsConfig.database = process.env.DEMO_DB_NAME;
}
return dsConfig;
}
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('datasource')
export class DemoDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'demo';
static readonly defaultConfig = config;
constructor(
@inject('datasources.config.demo', {optional: true})
dsConfig: object = config,
) {
super(updateConfig(dsConfig));
}
/**
* Disconnect the datasource when application is stopped. This allows the
* application to be shut down gracefully.
*/
stop(): ValueOrPromise<void> {
return super.disconnect();
}
}
|
Yes, you are correct. Method setup() is called inside the constructor. Thank you for your example. My use case is: MongoDB Automatic Client-Side Field Level Encryption. The way I can do right now is using connect() method, and I have to call connect() manually. It will be great if I can use setup() method. Thank you. |
If the Redis connection suddenly breaks while the backend server is running, I naturally get an error as follows.
Is it possible to do something like just turn off/deactivate the Redis DataSource without getting this error? |
Steps to reproduce
At https://github.com/strongloop/loopback-datasource-juggler/blob/master/types/datasource.d.ts,
there is no definition for method setup, which is defined at:
https://github.com/strongloop/loopback-datasource-juggler/blob/06428247ad1cc48bce82d135f795fb3eaae28b33/lib/datasource.js#L382
class MyDataSource which extends juggler.Datasource
.super.setup()
inside method setup.Current Behavior
When I want to use

super.setup()
, there will be ts error: Property 'setup' does not exists on type 'Datasource'.Expected Behavior
No ts error when I want to use
super.setup()
inside setup method on my custom datasource class.Additional information
Setup method definition:
The text was updated successfully, but these errors were encountered: