Loopback-next: Datasource Switcher

Created on 3 Jul 2019  路  4Comments  路  Source: strongloop/loopback-next

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!

question

Most helpful comment

@sbacem same as your situation here.
My current solution is: every time I get the user information, I will create a new datasource.
I found that this really doesn't make much sense, maybe I should connect directly using Node.js Mongodb Driver.

All 4 comments

Conceptually before going toward any solution, this sounds like a bad idea to even contemplate. First off, how are you storing the passwords assuming they constitute part of the login? If the passwords are stored without hashing them you've created a vulnerability by design(if your initial database is compromised, then all are). If you do hash the passwords, how do you propose to use them to login?

Also probably best to ask questions like this on stackoverflow.com and tag as loopback.

I have same pb i would update name database dynamically
for exemple

if user1 is logged wil be get data from db1
if user2 is logged wil be get data from db2

I tried to update in namedb.datasource.ts dynamically and replaces namedb.datasource.ts but notwork.
Always use first connexion

@sbacem same as your situation here.
My current solution is: every time I get the user information, I will create a new datasource.
I found that this really doesn't make much sense, maybe I should connect directly using Node.js Mongodb Driver.

See this example on how to deal with multi-tenancy.

IMO it's not a good idea to be switching between datasources as this makes for a rather fragile system with many moving parts. There's a lot of potential pitfalls that may arise as a result.

My advise for new projects that have structured data would be to design a properly normalised SQL database, which won't require any datasource switching.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shadyanwar picture shadyanwar  路  3Comments

teambitcodeGIT picture teambitcodeGIT  路  3Comments

mhdawson picture mhdawson  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

dericgw picture dericgw  路  3Comments