2

Recently I have a requirement to be able to implement a MeteorJS application capable of connecting to different databases according to the connected user.

The structure of the database is the same, only the information it contains changes

I tried connecting through DDP to another backend connected to the necessary database and also by using MongoInternals but, although the users log in correctly, the other collections continue to consult the database by default.

In the server code

const connection = DDP.connect(url)

Accounts.connection = Meteor.connection = connection

_.each(
      [
        "subscribe",
        "methods",
        "call",
        "apply",
        "status",
        "reconnect",
        "disconnect",
      ],
      function (name) {
        Meteor[name] = _.bind(Meteor.connection[name], Meteor.connection); // 55
      }
    );
ConfigApps = new Mongo.Collection("configapps")

Here the ConfigApps collection obtains the correct data from the database but from the Frontend the data from the other database is read

Or:

  var database = new MongoInternals.RemoteCollectionDriver(
      "mongodb://localhost:27017/spence-local"
    );

    ConfigApps = new Mongo.Collection("configapps", {
      _driver: database,
      _suppressSameNameError: true,
    });

    Meteor.users = new Mongo.Collection("users", {
      _driver: database,
      _suppressSameNameError: true,
    });

With the same results. User can connect but only one database is read for all querys in the application

Some information or clue would be great.

  • Does this answer your question? [Meteor - connect collections of multiple databases](https://stackoverflow.com/questions/33464238/meteor-connect-collections-of-multiple-databases) – Christian Fritz Mar 08 '21 at 22:08
  • Also see https://stackoverflow.com/questions/27762620/can-a-single-meteor-instance-listen-and-react-to-multiple-mongodb-databases. – Christian Fritz Mar 08 '21 at 22:08
  • I tried it but without success, MeteorJs does not modify from where it reads the collections at runtime and creating two connections at the same time and modifying all the publications would be expensive since it is a project of thousands and thousands of lines of code – Nico Bastias Mar 08 '21 at 22:26
  • 1
    yeah, in general I think this is a bad idea. It's just not what meteor was meant for. Are you sure you can't just merge the databases and have separate collections for different connected users instead? (or not even that, if possible) – Christian Fritz Mar 09 '21 at 01:04
  • Not even that, same product for different companies – Nico Bastias Mar 09 '21 at 02:04
  • Unfortunately Meteor is tailored for simple scheme, multitenancy being achieved by tenant fields at document level. Another approach consists in multiple instances of the same app, possibly with a shared user authentication portal if you absolutely want a single home. – ghybs Mar 09 '21 at 03:15
  • 1
    Even if it is for different companies, does not mean the separation has to happen at the database level. Per-document company IDs are fine. Otherwise, yes, like ghybs suggests, just make it separate meteor instances per customer -- e.g., using meteor-up, or docker directly. You are really trying to tie this together at the worst possible place in the stack. – Christian Fritz Mar 09 '21 at 15:18

0 Answers0