You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functioncreateClient(url){varclient=loopback();vards=client.dataSource('remote',{connector: 'remote',url: url});// these models point to the 'remote' datasourceboot(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.
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.
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.
Another option is to modify loopback.createModel to fill MyModel.loopback = loopback. This will serve as a workaround until we implement per-app registry.
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.
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.
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.
Activity
kraman commentedon Jan 30, 2015
+1
Ive run into the same problem and had to drop down to using strong-remoting directly to get around it.
bajtos commentedon Jan 30, 2015
+1
Although I don't consider this issue to be a bug.
ritch commentedon Feb 23, 2015
I've run into this again... Somehow I had
loopbacklinked into arequire('module').globalPathsdirectory causing the version of loopback used withboot()andserver.jsto 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.dataSourceandModel.settings). Models should also have no state directly linked to their constructors / functions. Instead we should store all state in theapp.registry.This leaves default or initial state. We need somewhere to store the defaults or initial state for things like the
Usermodel. I haven't dug too deep into this yet... but perhaps something likeModel.boot(registry)makes sense. This wayboot()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
bajtos commentedon Feb 24, 2015
IMO we don't need
Model.bootbecauseapp.model(MyModel)already setsMyModel.app = appand emitsattachedevent.Another option is to modify
loopback.createModelto fillMyModel.loopback = loopback. This will serve as a workaround until we implement per-app registry.bajtos commentedon Mar 12, 2015
Related: loopbackio/loopback-datasource-juggler#119 - "The model names have to be unique."
ritch commentedon Mar 13, 2015
@kraman @bajtos @chandadharap and I had an offline chat about this and here was essentially our outcome:
Model.registryset when you create the model.loopback.getModelByType()=>Model.registry.getModelByType())bajtos commentedon Mar 13, 2015
Some clarification:
Only
ModelandPersistedModelshould 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.
ritch commentedon Mar 13, 2015
So essentially we would be adding a new
Modelproperty:The
Model.registrywill be set byloopback.createModel()andapp.model()to the correct registry. The "correct" registry will be determined by aloopbackflag that allows you to specify to useperAppRegistries.When
perAppRegistriesistrueeachappobject will create its own registry. Most likely this will be available (when set) asapp.registry.ritch commentedon Mar 13, 2015
Also...
perAppRegistriesshould default tofalsein2.0and set totruein3.0. Perhaps we could set it to true in the generated code as well? @bajtosbajtos commentedon Mar 16, 2015
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.
bajtos commentedon Mar 16, 2015
Let's discuss the rest in #1212.
ritch commentedon Apr 3, 2015
You can get around this now by using the new flag
loopback({localRegistry: true})