Skip to content

Commit c553f11

Browse files
bajtosraymondfeng
authored andcommitted
fix: multiple instances of the same repository class
Fix DefaultCrudRepository to re-use legacy juggler Models across all instances of the same Repository class. Before this change, each call of DefaultCrudRepository constructor was redefining the backing persisted model. This commit adds a caching mechanism to DefaultCrudRepository: when setting up a backing model, we check datasource's modelBuilder registry to find if the backing model was not already created by an older instance of the repository.
1 parent afa5511 commit c553f11

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

docs/site/Repositories.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ summary:
99
---
1010

1111
A Repository is a type of _Service_ that represents a collection of data within
12-
a DataSource.
12+
a DataSource. A repository class is a lightweight object, its instances can be
13+
created with low runtime overhead. Typically a new repository instance is
14+
created for each incoming request.
1315

1416
## Example Application
1517

packages/repository/src/repositories/legacy-juggler-bridge.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
NamedParameters,
1616
PositionalParameters,
1717
} from '../common-types';
18-
import {Entity} from '../model';
18+
import {Entity, ModelDefinition} from '../model';
1919
import {Filter, Where} from '../query';
2020
import {EntityCrudRepository} from './repository';
2121

@@ -88,7 +88,19 @@ export class DefaultCrudRepository<T extends Entity, ID>
8888
`Entity ${entityClass.name} must have at least one id/pk property.`,
8989
);
9090

91-
// Create an internal legacy Model attached to the datasource
91+
this.setupPersistedModel(definition);
92+
}
93+
94+
// Create an internal legacy Model attached to the datasource
95+
private setupPersistedModel(definition: ModelDefinition) {
96+
const dataSource = this.dataSource;
97+
98+
const model = dataSource.getModel(definition.name);
99+
if (model) {
100+
// The backing persisted model has been already defined.
101+
this.modelClass = model as typeof juggler.PersistedModel;
102+
return;
103+
}
92104

93105
// We need to convert property definitions from PropertyDefinition
94106
// to plain data object because of a juggler limitation

packages/repository/test/unit/repositories/legacy-juggler-bridge.unit.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ describe('DefaultCrudRepository', () => {
6767
await model.deleteAll();
6868
});
6969

70+
it('shares the backing PersistedModel across repo instances', () => {
71+
const model1 = new DefaultCrudRepository(Note, ds).modelClass;
72+
const model2 = new DefaultCrudRepository(Note, ds).modelClass;
73+
74+
expect(model1 === model2).to.be.true();
75+
});
76+
7077
it('implements Repository.create()', async () => {
7178
const repo = new DefaultCrudRepository(Note, ds);
7279
const note = await repo.create({title: 't3', content: 'c3'});

0 commit comments

Comments
 (0)