chore: Improve relationship loading, include policies (#6321)

Use model where available in usePolicy
This commit is contained in:
Tom Moor
2023-12-28 12:51:33 -04:00
committed by GitHub
parent bd7d5c338d
commit 79764b1e64
16 changed files with 37 additions and 24 deletions

View File

@@ -32,26 +32,39 @@ export default abstract class Model {
}
/**
* Ensures all the defined relations for the model are in memory
* Ensures all the defined relations and policies for the model are in memory.
*
* @returns A promise that resolves when loading is complete.
*/
async loadRelations() {
async loadRelations(): Promise<any> {
const relations = getRelationsForModelClass(
this.constructor as typeof Model
);
if (!relations) {
return;
}
if (this.loadingRelations) {
return this.loadingRelations;
}
const promises = [];
for (const properties of relations.values()) {
const store = this.store.rootStore.getStoreForModelName(
properties.relationClassResolver().modelName
);
if ("fetch" in store) {
await store.fetch(this[properties.idKey]);
promises.push(store.fetch(this[properties.idKey]));
}
}
const policy = this.store.rootStore.policies.get(this.id);
if (!policy) {
promises.push(this.store.fetch(this.id, { force: true }));
}
this.loadingRelations = Promise.all(promises);
return await this.loadingRelations;
}
/**
@@ -175,4 +188,9 @@ export default abstract class Model {
}
protected persistedAttributes: Partial<Model> = {};
/**
* A promise that resolves when all relations have been loaded
*/
private loadingRelations: Promise<any[]> | undefined;
}