- Notifications
You must be signed in to change notification settings - Fork6
This is a loopback 4 extension to connect to dynamic data sources runtime and maintaining old connections.
License
sourcefuse/loopback4-dynamic-datasource
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is a loopback 4 extension to connect dynamic datasources runtime. In todays world there can be a use case of multi tenant system in which we need the physicalseperation of the databses so in loopback we need to connect to those datasources runtime and maintain and reuse those connection.
npm install loopback4-dynamic-datasource
In order to use this component into your LoopBack application, please follow below steps.
- Add component to application.
this.component(Loopback4DynamicDatasourceComponent);
- Now add action provider to the action based sequence (This step is not required in case of middleware based sequence)
exportclassMySequenceimplementsSequenceHandler{constructor( ... @inject(DynamicDatasourceBindings.DYNAMIC_DATASOURCE_ACTION)privatereadonlysetupDatasource:SetupDatasourceFn, ...){}asynchandle(context:RequestContext){constrequestTime=Date.now();try{ ...awaitthis.setupDatasource(context); ...}catch(err){this.reject(context,err);}}}
- Now write a datasource identifier provider that is used to identify runtime which source we need to connect.In below example getting the identifier from the tenantId coming in query of the request.
import{DatasourceIdentifierFn}from'loopback4-dynamic-datasouce';exportclassCustomDatasourceIdentifierProviderimplementsProvider<DatasourceIdentifierFn>{constructor(){}value():DatasourceIdentifierFn{returnasync(requestCtx)=>{consttenantId=requestCtx.request.query['tenantId']asstring;returntenantId==null ?null :{id:tenantId};};}}
Now bind that provider in application.ts
this.bind(DynamicDatasourceBindings.DATASOURCE_IDENTIFIER_PROVIDER).toProvider(CustomDatasourceIdentifierProvider);
- Now write a datasource provider to get datasources runtime. these datasource will be created runtime on require basis
exportclassCustomDatasourceProviderimplementsProvider<DatasourceProviderFn>{constructor( @repository(TenantRepository)privatetenantRepo:TenantRepository,){}value():DatasourceProviderFn{returnasync(datasourceIdentifier)=>{return{pgdb:async()=>{consttenantData=awaitthis.tenantRepo.findById(datasourceIdentifier.id);returnnewjuggler.DataSource({ ...tenantData.dbConfig,});}}}}}
Now bind that provider in application.ts
this.bind(DynamicDatasourceBindings.DATASOURCE_PROVIDER).toProvider(CustomDatasourceProvider);
Note:- connector of following datasource should be present in package.json like in this example we are usingloopback-connector-postgresql
Now return of this provider is an object where you can give as many keys you want but that should return juggler.DatasourceThis is used as the intention of connecting multiple datasource for tenant.pgdb
this key is custom and it can be used as per your choice but your repository must use specified key in injection
exportclassUserRepositoryextendsDefaultCrudRepository<User,typeofUser.prototype.id,UserRelations>{constructor( @inject('datasources.pgdb')dataSource:JugglerDataSource,){super(User,dataSource);}}
That's all.
If you've noticed a bug or have a question or have a feature request,search the issue tracker to see if someone else in the community has already created a ticket.If not, go ahead andmake one!All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can.If you think this extension is useful, pleasestar it. Appreciation really helps in keeping this project alive.
Please readCONTRIBUTING.md for details on the process for submitting pull requests to us.
Code of conduct guidelineshere.
About
This is a loopback 4 extension to connect to dynamic data sources runtime and maintaining old connections.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.