-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: multiple instances of the same repository class #1302
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,13 @@ describe('DefaultCrudRepository', () => { | |
await model.deleteAll(); | ||
}); | ||
|
||
it('shares the backing PersistedModel across repo instances', () => { | ||
const model1 = new DefaultCrudRepository(Note, ds).modelClass; | ||
const model2 = new DefaultCrudRepository(Note, ds).modelClass; | ||
|
||
expect(model1 === model2).to.be.true(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does deep assertion not work with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can. My experience is that equality assertion produces an error message that's rather unhelpful, because it it lists all model properties and properties of these properties. Because a model is attached to a datasource that has a reference to a model builder, eventually all models registered with the data source are printed in the diff. That's why I decided to use this form, because really all we want to see is whether the two models are the same V8 entity or not. |
||
}); | ||
|
||
it('implements Repository.create()', async () => { | ||
const repo = new DefaultCrudRepository(Note, ds); | ||
const note = await repo.create({title: 't3', content: 'c3'}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a flaw in this PR:
Repository
so that it can representmodelClass + dataSource
Repository
should have its own 3.x model classbindModel
instead ofmodel.attachTo()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
I disagree for two reasons:
In my proposal, we will have a single LB3 PersistedModel for each pair of LB4 Model + LB3 dataSource. If you attach the same LB4 Model to multiple dataSources, then we will have multiple LB3 PersistedModels, one for each dataSource.
Is there any specific problem you are concerned about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to know why we can't re-use the model class if it's already defined as proposed in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 3.x model classes maintain two states:
For example, a
Customer
model can only be attached tomongodb
ormysql
but we cannot have the same model definition to be used withmongodb
ormysql
unless we subclass it.In LB4, we subclass
Customer
for each repository instance to delegate to juggler model methods without interfering with other instances. For example:@bajtos Please note in LB 3.x, all data source instances share the same
modelBuilder
if it's fromloopback
module. See https://github.com/strongloop/loopback/blob/master/lib/registry.js#L362.If in LB4, we always create DataSource instances with its own modelBuilder, your PR should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's the case so far - in LB4, each DataSource has its own model builder.