Skip to content

Datasource SwitcherΒ #3300

@b-abdallah

Description

@b-abdallah

I have First a global DataBase contain societyName and their databaseNames, each society have his databaseName,
when user want to login, first we check from the societyName, in the global DataBase, the databaseName, after that he connect with his Email and password stored in his selected database.
So I'm wanting to know, after i have the databaseName of this user, how to switch from global database to the database according to the databaseName of this user.

My idea, in db.datasources.ts, instead of having a fixed datasource from client-db.datasource.json, I try to recover the datasource dynamically according to the current-user, but it does not go well, the database does not change.
my code

import {inject} from '@loopback/core';
import {juggler } from '@loopback/repository';
//import * as config from './client-db.datasource.json'
import { binderService } from '../services/binder-service';


export class ClientDbDataSource extends juggler.DataSource {
  static dataSourceName = 'clientDb';
  constructor( 
    @inject('datasources.config.clientDb', {optional: true})
     dsConfig: object   = binderService.getUserDataSource(), // getUserDataSource() return the datasource of the user from a controller "who-i-am" 
  ) {
    super(dsConfig);
    }
}

Appreciate the guidance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions