Me and my team will develop some applications that will communicate with other, this application will work basically like this: A front-end application built with react that communicates with an API. The way that this API will work is: each one of our clients will have an individual database, their users will access our front-end app, this app will locate in a Database that contains the information of, what is their enterprise and will return some connection information for them. After this, each user will have the information of what is their enterprise, and our API will take this info and connect in the database and take the data. My questions are that I don't know the best way to do this (instantiate a datasource in each request and disconnect after || after a user of enterprise x connects, I will make this connection open while are active users on that enterprise || other solution)? And I'm wrong trying to use lb4 for this scenario?
Asked
Active
Viewed 70 times
1 Answers
1
My situation is similar to yours, my team is using `Mongodb` 4.x and I assume you are using it.
Trouble
According to my understanding, your trouble is
- you need to dynamically use different
DataSource
in different requests. - The current
DataSource
address is unchanged after instantiation.
Solution with Mongodb
You need to create a class extends with juggler.DataSource
that can dynamically switch databases each time, then inject
this class into each controller
.
- loopback-datasource-juggler/types/datasource.d.ts
export declare class DataSource extends EventEmitter {
...
connector?: Connector; // <= can access `Mongodb Client` object
- create class extends with
juggler.DataSource
export class MyClient extends juggler.DataSource {
constructor(
@config()
settings: Config = config
) {
super(settings);
}
public async db(dbName: string): Promise<Database> {
if (!this.connected) await this.connect();
// return the `Mongodb DB` object
return this.connector!.client.db(dbName);
}
}
- inject
MyClient
class and use it in controller
export class TestController {
constructor(
@inject('my-client')
private myClient: MyClient,
) { }
@post('/test')
async test(){
let db = await this.myClient.db('db-name');
let col = db.collection('col-name');
// ...
}
}

Zhikai Xiong
- 357
- 2
- 9
-
Thank you! I don't understand one thing, forgive me for my lack of knowledge, but the type Database, @config() decorator and Config type (i suppose that comes from the same place of the @config decorator), where do I find those? I cannot locate them in the docs and don't know what they are and from where they come from. Can you explain it for me? – Hudison Aug 16 '19 at 18:14
-
Please see [this](https://github.com/strongloop/loopback-next/tree/master/examples/context) sample project, you can find related examples in `/src/configuration-injection.ts`. – Zhikai Xiong Aug 17 '19 at 05:10
-
type `Database` and `Config` is my custom type and you can define it according to your own needs. – Zhikai Xiong Aug 17 '19 at 05:25