Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This is a loopback 4 extension to connect to dynamic data sources runtime and maintaining old connections.

License

NotificationsYou must be signed in to change notification settings

sourcefuse/loopback4-dynamic-datasource

LoopBack

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.

Image of Architecture

Install

npm install loopback4-dynamic-datasource

Usage

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.

Feedback

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.

Contributing

Please readCONTRIBUTING.md for details on the process for submitting pull requests to us.

Code of conduct

Code of conduct guidelineshere.

License

MIT

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

Stars

Watchers

Forks

Packages

No packages published

Contributors8


[8]ページ先頭

©2009-2025 Movatter.jp