зеркало из https://github.com/microsoft/paris.git
fixed a bug where relationships were broken if class names were mangled
This commit is contained in:
Родитель
a4fd2ed643
Коммит
fdba18e990
|
@ -15,6 +15,7 @@ export interface DataEntityType<TEntity extends ModelBase = any, TRawData = any,
|
|||
new(entityData?:any, rawData?:TRawData):TEntity,
|
||||
singularName?:string,
|
||||
pluralName?:string,
|
||||
forwardRefName?:string,
|
||||
entityConfig?:ModelEntity<TEntity, TRawData, TId>,
|
||||
valueObjectConfig?:EntityConfigBase,
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ export function Entity(config:EntityConfig<any, any, any, any>){
|
|||
target.entityConfig = entity;
|
||||
target.singularName = config.singularName;
|
||||
target.pluralName = config.pluralName;
|
||||
target.forwardRefName = config.forwardRefName;
|
||||
entitiesService.addEntity(target, entity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ export function ValueObject(config:IEntityConfigBase){
|
|||
let valueObjectConfig:EntityConfigBase = new EntityConfigBase(config, target.prototype.constructor);
|
||||
target.singularName = valueObjectConfig.singularName;
|
||||
target.pluralName = valueObjectConfig.pluralName;
|
||||
target.forwardRefName = valueObjectConfig.forwardRefName;
|
||||
target.valueObjectConfig = valueObjectConfig;
|
||||
valueObjectsService.addEntity(target, valueObjectConfig);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ const DEFAULT_VALUE_ID = "__default";
|
|||
export class EntityConfigBase<TEntity extends ModelBase = any, TRawData = any, TId extends EntityId = string> implements ModelConfig<TEntity, TRawData, TId>{
|
||||
singularName:string;
|
||||
pluralName:string;
|
||||
forwardRefName:string;
|
||||
fields?:EntityFields;
|
||||
idProperty?:keyof TRawData;
|
||||
readonly:boolean = false;
|
||||
|
@ -103,6 +104,12 @@ export interface IEntityConfigBase<TEntity extends ModelBase = any, TRawData = a
|
|||
*/
|
||||
pluralName:string,
|
||||
|
||||
/**
|
||||
* Unique name used to reference an entity
|
||||
* If not supplied, the singularName is used
|
||||
*/
|
||||
forwardRefName?:string,
|
||||
|
||||
/**
|
||||
* The property in the raw data used for the Entity's ID.
|
||||
*/
|
||||
|
|
|
@ -17,14 +17,14 @@ export abstract class EntitiesServiceBase<T extends EntityConfigBase, TRawData =
|
|||
return this._allEntities.get(dataEntityType) || this._allEntities.get(dataEntityType.prototype);
|
||||
}
|
||||
|
||||
getEntityByName(entitySingularName:string):T{
|
||||
return this._allEntitiesByName.get(entitySingularName);
|
||||
getEntityByName(forwardRefName:string):T{
|
||||
return this._allEntitiesByName.get(forwardRefName.replace(/\s/g, ""));
|
||||
}
|
||||
|
||||
addEntity(dataEntityType:DataEntityType, entity:T):T{
|
||||
if (!this._allEntities.has(dataEntityType)) {
|
||||
this._allEntities.set(dataEntityType, entity);
|
||||
this._allEntitiesByName.set(dataEntityType.name, entity);
|
||||
this._allEntitiesByName.set(dataEntityType.forwardRefName || dataEntityType.singularName.replace(/\s/g, ""), entity);
|
||||
}
|
||||
|
||||
entity.fields = this.getDataEntityTypeFields(dataEntityType);
|
||||
|
|
|
@ -53,6 +53,10 @@ export class Modeler {
|
|||
(entitiesService.getEntityByName(modelWithEntityOrString) ||
|
||||
valueObjectsService.getEntityByName(modelWithEntityOrString)) :
|
||||
(modelWithEntityOrString.entityConfig || modelWithEntityOrString.valueObjectConfig);
|
||||
if (!modelWithEntity){
|
||||
getModelDataError.message = `${getModelDataError.message} modelWith: Couldn't find '${modelWithEntity}'. Did you add a 'forwardRefName' to the corresponding entity config?`;
|
||||
throw getModelDataError;
|
||||
}
|
||||
return this.modelEntity<TConcreteEntity, TRawData>(
|
||||
rawData,
|
||||
modelWithEntity,
|
||||
|
|
15
lib/paris.ts
15
lib/paris.ts
|
@ -4,7 +4,10 @@ import { Observable, of, Subject, throwError } from "rxjs";
|
|||
import { catchError, map, mergeMap, switchMap, tap } from "rxjs/operators";
|
||||
import { ApiCallType } from "./api/api-calls/api-call.model";
|
||||
import { DataEntityType } from "./api/entity/data-entity.base";
|
||||
import { EntityRelationshipRepositoryType } from "./api/entity/entity-relationship-repository-type";
|
||||
import {
|
||||
EntityRelationshipRepositoryType,
|
||||
IEntityRelationshipRepositoryType
|
||||
} from "./api/entity/entity-relationship-repository-type";
|
||||
import { EntityErrorEvent, EntityErrorTypes } from "./api/events/entity-error.event";
|
||||
import { RemoveEntitiesEvent } from "./api/events/remove-entities.event";
|
||||
import { SaveEntityEvent } from "./api/events/save-entity.event";
|
||||
|
@ -34,7 +37,7 @@ import { AjaxRequest } from "rxjs/ajax";
|
|||
|
||||
export class Paris<TConfigData = any> {
|
||||
private readonly repositories:Map<DataEntityType, IRepository<ModelBase>> = new Map;
|
||||
private readonly relationshipRepositories:Map<string, IRelationshipRepository<ModelBase>> = new Map;
|
||||
private readonly relationshipRepositories:Map<IEntityRelationshipRepositoryType<ModelBase, ModelBase>, IRelationshipRepository<ModelBase>> = new Map;
|
||||
readonly modeler:Modeler;
|
||||
|
||||
readonly dataStore:DataStoreService;
|
||||
|
@ -106,14 +109,10 @@ export class Paris<TConfigData = any> {
|
|||
getRelationshipRepository<T extends ModelBase, U extends ModelBase>(relationshipConstructor:Function):RelationshipRepository<T, U>{
|
||||
const relationship:EntityRelationshipRepositoryType<T, U> = <EntityRelationshipRepositoryType<T, U>>relationshipConstructor;
|
||||
|
||||
const sourceEntityName:string = relationship.sourceEntityType.name,
|
||||
dataEntityName:string = relationship.dataEntityType.name,
|
||||
relationshipId:string = `${sourceEntityName}_${dataEntityName}`;
|
||||
|
||||
let repository:RelationshipRepository<T, U> = <RelationshipRepository<T, U>>this.relationshipRepositories.get(relationshipId);
|
||||
let repository:RelationshipRepository<T, U> = <RelationshipRepository<T, U>>this.relationshipRepositories.get(relationship);
|
||||
if (!repository) {
|
||||
repository = new RelationshipRepository<T, U>(relationship.sourceEntityType, relationship.dataEntityType, relationship.allowedTypes, this);
|
||||
this.relationshipRepositories.set(relationshipId, repository);
|
||||
this.relationshipRepositories.set(relationship, repository);
|
||||
}
|
||||
|
||||
return repository;
|
||||
|
|
|
@ -5,6 +5,7 @@ import {Animal} from "./thing.entity";
|
|||
@Entity({
|
||||
singularName: 'Dog',
|
||||
pluralName: 'Dogs',
|
||||
forwardRefName: 'DogEntity',
|
||||
endpoint: 'things',
|
||||
})
|
||||
export class Dog extends Animal {
|
||||
|
|
|
@ -50,7 +50,7 @@ export class Person extends Thing {
|
|||
endpoint: 'things',
|
||||
modelWith: (_, query) => {
|
||||
if (query && query.where && (<{ [index: string]: any }>query.where)['isDog']) {
|
||||
return 'Dog'
|
||||
return 'DogEntity'
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче