Skip to content

Model collision when creating multiple apps #1056

Closed

Description

@ritch

I ran into this in the following scenario

function createClient(url) {
  var client = loopback();
  var ds = client.dataSource('remote', {
    connector: 'remote',
    url: url
  });

  // these models point to the 'remote' datasource
  boot(client, {
    appRootDir: pathToMyLoopBackApp
  });
}

createClient(myUrl).models.Parent.create({name: 'foo'}, function(err, parent) {
  // when creating many clients, this might call the wrong model's
  // create method...
  parent.children.create({name: 'bar'});
});

I'm guessing the issue is that we have a singleton registry object. I'd like to move away from this and support both the existing singleton behavior as well as an app.registry. This means we have to juggle the interaction between the two... but it will come with multi app support in a single process.

/to @bajtos @raymondfeng
/cc @kraman @seanbrookes

Activity

  1. kraman commented on Jan 30, 2015

    @kraman
    Contributor

    +1

    Ive run into the same problem and had to drop down to using strong-remoting directly to get around it.

  2. bajtos commented on Jan 30, 2015

    @bajtos
    Member

    +1

    Although I don't consider this issue to be a bug.

  3. ritch commented on Feb 23, 2015

    @ritch
    MemberAuthor

    I've run into this again... Somehow I had loopback linked into a require('module').globalPaths directory causing the version of loopback used with boot() and server.js to be different. IMO this shouldn't break since there is really no conflict.

    I think the root issue is that loopback has some sort of state (the registry). If this was moved entirely to the app level (eg. app.registry) then loopback would never be in any state. It would only need to reference some registry / app.

    The real blocker to moving to this style is that we expose models via the loopback core module (eg. loopback.User). These models have the same issue that loopback itself does... they have some state (eg. Model.dataSource and Model.settings). Models should also have no state directly linked to their constructors / functions. Instead we should store all state in the app.registry.

    This leaves default or initial state. We need somewhere to store the defaults or initial state for things like the User model. I haven't dug too deep into this yet... but perhaps something like Model.boot(registry) makes sense. This way boot() is a series of calls giving each model a chance to modify the registry. It might allow us to remove any state from the actual models themselves.

    @bajtos @raymondfeng

  4. bajtos commented on Feb 24, 2015

    @bajtos
    Member

    This leaves default or initial state. We need somewhere to store the defaults or initial state for things like the User model. I haven't dug too deep into this yet... but perhaps something like Model.boot(registry) makes sense. This way boot() is a series of calls giving each model a chance to modify the registry. It might allow us to remove any state from the actual models themselves.

    IMO we don't need Model.boot because app.model(MyModel) already sets MyModel.app = app and emits attached event.

    User.setup = function() {
      // ...
      this.on('attached', function(app) {
        // initialize app-specific settings
      });
    }

    Another option is to modify loopback.createModel to fill MyModel.loopback = loopback. This will serve as a workaround until we implement per-app registry.

  5. bajtos commented on Mar 12, 2015

    @bajtos
    Member

    Related: loopbackio/loopback-datasource-juggler#119 - "The model names have to be unique."

  6. ritch commented on Mar 13, 2015

    @ritch
    MemberAuthor

    @kraman @bajtos @chandadharap and I had an offline chat about this and here was essentially our outcome:

      1. implement per app registry (inherit modelbuilder entries from global)
      • 1.a Model.registry set when you create the model.
      1. find and replace references to the registry interface (eg. loopback.getModelByType() => Model.registry.getModelByType())
      1. loopback boot should use the new app.registry api
      • 3.b boot should also recreate all built in models
  7. bajtos commented on Mar 13, 2015

    @bajtos
    Member

    Some clarification:

    1. implement per app registry (inherit modelbuilder entries from global)

    Only Model and PersistedModel should be copied to the per-app registry, no other models are inherited. It is not possible to use a global model in an app, because each model can be attached to single application (and datasource) only.

    We may consider implementing a check that will refuse to add Model and PersistedModel to an app or a datasource.

  8. ritch commented on Mar 13, 2015

    @ritch
    MemberAuthor

    So essentially we would be adding a new Model property:

    /**
     * ...
     * @prop {Registry} Model.registry The registry the `Model` was added to.
     *
     */

    The Model.registry will be set by loopback.createModel() and app.model() to the correct registry. The "correct" registry will be determined by a loopback flag that allows you to specify to use perAppRegistries.

    When perAppRegistries is true each app object will create its own registry. Most likely this will be available (when set) as app.registry.

  9. ritch commented on Mar 13, 2015

    @ritch
    MemberAuthor

    Also... perAppRegistries should default to false in 2.0 and set to true in 3.0. Perhaps we could set it to true in the generated code as well? @bajtos

  10. bajtos commented on Mar 16, 2015

    @bajtos
    Member

    Also... perAppRegistries should default to false in 2.0 and set to true in 3.0. Perhaps we could set it to true in the generated code as well? @bajtos

    I'd rather wait a bit before setting it to true in the generated code. Let's get more confidence that the local registries work, before we make it a default for new projects. Unfortunately the code coverage of LoopBack unit-tests is not good enough to give us that confidence, thus we need to test the feature in a number of real applications.

  11. bajtos commented on Mar 16, 2015

    @bajtos
    Member

    Let's discuss the rest in #1212.

  12. self-assigned this
    on Mar 24, 2015
  13. added and removed on Mar 24, 2015
  14. ghost removed on Mar 24, 2015
  15. added and removed on Apr 2, 2015
  16. ritch commented on Apr 3, 2015

    @ritch
    MemberAuthor

    You can get around this now by using the new flag loopback({localRegistry: true})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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