Fixed handling of unregistered resources
This commit is contained in:
parent
fc5f6f4e54
commit
dfaf8b552a
@ -112,7 +112,11 @@ export class AutoupdateService {
|
||||
|
||||
// Add the objects to the DataStore.
|
||||
for (const collection of Object.keys(autoupdate.changed)) {
|
||||
await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection]));
|
||||
if (this.modelMapper.isCollectionRegistered(collection)) {
|
||||
await this.DS.add(this.mapObjectsToBaseModels(collection, autoupdate.changed[collection]));
|
||||
} else {
|
||||
console.error(`Unregistered collection "${collection}". Ignore it.`);
|
||||
}
|
||||
}
|
||||
|
||||
await this.DS.flushToStorage(autoupdate.to_change_id);
|
||||
@ -130,9 +134,6 @@ export class AutoupdateService {
|
||||
*/
|
||||
private mapObjectsToBaseModels(collection: string, models: object[]): BaseModel[] {
|
||||
const targetClass = this.modelMapper.getModelConstructor(collection);
|
||||
if (!targetClass) {
|
||||
throw new Error(`Unregistered resource ${collection}`);
|
||||
}
|
||||
return models.map(model => new targetClass(model));
|
||||
}
|
||||
|
||||
|
@ -65,27 +65,39 @@ export class CollectionStringMapperService {
|
||||
}
|
||||
}
|
||||
|
||||
public isCollectionRegistered(collectionString: string): boolean {
|
||||
return !!this.collectionStringMapping[collectionString];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obj The object to get the model constructor from.
|
||||
* @returns the model constructor
|
||||
*/
|
||||
public getModelConstructor<M extends BaseModel>(obj: TypeIdentifier): ModelConstructor<M> {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor<M>;
|
||||
public getModelConstructor<M extends BaseModel>(obj: TypeIdentifier): ModelConstructor<M> | null {
|
||||
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][0] as ModelConstructor<M>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obj The object to get the view model constructor from.
|
||||
* @returns the view model constructor
|
||||
*/
|
||||
public getViewModelConstructor<M extends BaseViewModel>(obj: TypeIdentifier): ViewModelConstructor<M> {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor<M>;
|
||||
public getViewModelConstructor<M extends BaseViewModel>(obj: TypeIdentifier): ViewModelConstructor<M> | null {
|
||||
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][1] as ViewModelConstructor<M>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obj The object to get the repository from.
|
||||
* @returns the repository
|
||||
*/
|
||||
public getRepository<V extends BaseViewModel, M extends BaseModel>(obj: TypeIdentifier): BaseRepository<V, M> {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>;
|
||||
public getRepository<V extends BaseViewModel, M extends BaseModel>(
|
||||
obj: TypeIdentifier
|
||||
): BaseRepository<V, M> | null {
|
||||
if (this.isCollectionRegistered(this.getCollectionString(obj))) {
|
||||
return this.collectionStringMapping[this.getCollectionString(obj)][2] as BaseRepository<V, M>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user