зеркало из https://github.com/microsoft/paris.git
Value objects
This commit is contained in:
Родитель
0e68314e39
Коммит
cd8e7d2e98
|
@ -3,7 +3,9 @@
|
|||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/bundle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/definitions" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/src" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
</content>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { IIdentifiable } from "../models/identifiable.model";
|
||||
import { ModelEntity } from "./entity.config";
|
||||
import { ModelObjectValue } from "./object-value.config";
|
||||
import { EntityConfigBase } from "./entity-config.base";
|
||||
export interface DataEntityConstructor<T> extends DataEntityType {
|
||||
new (data?: IIdentifiable): T;
|
||||
new (data?: any): T;
|
||||
}
|
||||
export interface DataEntityType {
|
||||
new (data: IIdentifiable): any;
|
||||
entityConfig?: ModelEntity;
|
||||
objectValueConfig?: ModelObjectValue;
|
||||
valueObjectConfig?: EntityConfigBase;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
import { EntityFields } from "./entity-fields";
|
||||
import { Field } from "./entity-field";
|
||||
export declare abstract class EntityConfigBase {
|
||||
import { IIdentifiable } from "../models/identifiable.model";
|
||||
import { DataEntityConstructor } from "./data-entity.base";
|
||||
export declare class EntityConfigBase {
|
||||
entityConstructor: DataEntityConstructor<any>;
|
||||
singularName: string;
|
||||
pluralName: string;
|
||||
fields?: EntityFields;
|
||||
idProperty?: string;
|
||||
readonly fieldsArray: Array<Field>;
|
||||
constructor(config: IEntityConfigBase);
|
||||
values: ReadonlyArray<IIdentifiable>;
|
||||
private _valuesMap;
|
||||
private readonly valuesMap;
|
||||
constructor(config: IEntityConfigBase, entityConstructor: DataEntityConstructor<any>);
|
||||
getValueById(valueId: string | number): IIdentifiable;
|
||||
hasValue(valueId: string | number): boolean;
|
||||
}
|
||||
export interface IEntityConfigBase {
|
||||
singularName: string;
|
||||
pluralName: string;
|
||||
fields?: EntityFields;
|
||||
idProperty?: string;
|
||||
values?: Array<IIdentifiable>;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var immutability_1 = require("../services/immutability");
|
||||
var EntityConfigBase = /** @class */ (function () {
|
||||
function EntityConfigBase(config) {
|
||||
function EntityConfigBase(config, entityConstructor) {
|
||||
this.entityConstructor = entityConstructor;
|
||||
if (config.values) {
|
||||
config.values = config.values.map(function (valueConfig) { return new entityConstructor(valueConfig); });
|
||||
immutability_1.Immutability.freeze(config.values);
|
||||
}
|
||||
Object.assign(this, config);
|
||||
}
|
||||
Object.defineProperty(EntityConfigBase.prototype, "fieldsArray", {
|
||||
|
@ -11,6 +17,28 @@ var EntityConfigBase = /** @class */ (function () {
|
|||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(EntityConfigBase.prototype, "valuesMap", {
|
||||
get: function () {
|
||||
var _this = this;
|
||||
if (this._valuesMap === undefined) {
|
||||
if (!this.values)
|
||||
this._valuesMap = null;
|
||||
else {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(function (value) { return _this._valuesMap.set(value.id, value); });
|
||||
}
|
||||
}
|
||||
return this._valuesMap;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
EntityConfigBase.prototype.getValueById = function (valueId) {
|
||||
return this.valuesMap ? this.valuesMap.get(valueId) : null;
|
||||
};
|
||||
EntityConfigBase.prototype.hasValue = function (valueId) {
|
||||
return this.valuesMap ? this.valuesMap.has(valueId) : false;
|
||||
};
|
||||
return EntityConfigBase;
|
||||
}());
|
||||
exports.EntityConfigBase = EntityConfigBase;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { EntityConfigBase, IEntityConfigBase } from "./entity-config.base";
|
||||
import { ParisConfig } from "../config/paris-config";
|
||||
import { DataEntityConstructor } from "./data-entity.base";
|
||||
export declare class ModelEntity extends EntityConfigBase {
|
||||
endpoint: EntityConfigFunctionOrValue;
|
||||
loadAll?: boolean;
|
||||
|
@ -7,10 +8,10 @@ export declare class ModelEntity extends EntityConfigBase {
|
|||
baseUrl?: EntityConfigFunctionOrValue;
|
||||
allItemsProperty?: string;
|
||||
allItemsEndpoint?: string;
|
||||
constructor(config: EntityConfig);
|
||||
constructor(config: EntityConfig, entityConstructor: DataEntityConstructor<any>);
|
||||
}
|
||||
export interface EntityConfig extends IEntityConfigBase {
|
||||
endpoint: EntityConfigFunctionOrValue;
|
||||
endpoint?: EntityConfigFunctionOrValue;
|
||||
loadAll?: boolean;
|
||||
cache?: ModelEntityCacheConfig;
|
||||
baseUrl?: EntityConfigFunctionOrValue;
|
||||
|
|
|
@ -13,8 +13,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
var entity_config_base_1 = require("./entity-config.base");
|
||||
var ModelEntity = /** @class */ (function (_super) {
|
||||
__extends(ModelEntity, _super);
|
||||
function ModelEntity(config) {
|
||||
var _this = _super.call(this, config) || this;
|
||||
function ModelEntity(config, entityConstructor) {
|
||||
var _this = _super.call(this, config, entityConstructor) || this;
|
||||
_this.loadAll = false;
|
||||
_this.loadAll = config.loadAll === true;
|
||||
return _this;
|
||||
|
|
|
@ -4,7 +4,7 @@ var entity_config_1 = require("./entity.config");
|
|||
var entities_service_1 = require("../services/entities.service");
|
||||
function Entity(config) {
|
||||
return function (target) {
|
||||
var entity = new entity_config_1.ModelEntity(config);
|
||||
var entity = new entity_config_1.ModelEntity(config, target.prototype.constructor);
|
||||
target.entityConfig = entity;
|
||||
entities_service_1.entitiesService.addEntity(target, entity);
|
||||
};
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
import { EntityConfigBase, IEntityConfigBase } from "./entity-config.base";
|
||||
import { IIdentifiable } from "../models/identifiable.model";
|
||||
export declare class ModelObjectValue extends EntityConfigBase {
|
||||
values: ReadonlyArray<IIdentifiable>;
|
||||
private _valuesMap;
|
||||
constructor(config: ObjectValueConfig);
|
||||
getValueById(valueId: string | number): IIdentifiable;
|
||||
}
|
||||
export interface ObjectValueConfig extends IEntityConfigBase {
|
||||
values: ReadonlyArray<IIdentifiable>;
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var entity_config_base_1 = require("./entity-config.base");
|
||||
var immutability_1 = require("../services/immutability");
|
||||
var ModelObjectValue = /** @class */ (function (_super) {
|
||||
__extends(ModelObjectValue, _super);
|
||||
function ModelObjectValue(config) {
|
||||
var _this = _super.call(this, config) || this;
|
||||
if (config.values) {
|
||||
immutability_1.Immutability.freeze(_this.values);
|
||||
}
|
||||
return _this;
|
||||
}
|
||||
ModelObjectValue.prototype.getValueById = function (valueId) {
|
||||
var _this = this;
|
||||
if (!this.values)
|
||||
return null;
|
||||
if (!this._valuesMap) {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(function (value) { return _this._valuesMap.set(value.id, value); });
|
||||
}
|
||||
return this._valuesMap ? this._valuesMap.get(valueId) : null;
|
||||
};
|
||||
return ModelObjectValue;
|
||||
}(entity_config_base_1.EntityConfigBase));
|
||||
exports.ModelObjectValue = ModelObjectValue;
|
|
@ -1,3 +0,0 @@
|
|||
import { DataEntityType } from "./data-entity.base";
|
||||
import { ObjectValueConfig } from "./object-value.config";
|
||||
export declare function ObjectValue(config: ObjectValueConfig): (target: DataEntityType) => void;
|
|
@ -1,14 +0,0 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var object_value_config_1 = require("./object-value.config");
|
||||
var object_values_service_1 = require("../services/object-values.service");
|
||||
function ObjectValue(config) {
|
||||
return function (target) {
|
||||
if (config.values)
|
||||
config.values = config.values.map(function (valueConfig) { return new target.prototype.constructor(valueConfig); });
|
||||
var objectValueConfig = new object_value_config_1.ModelObjectValue(config);
|
||||
target.objectValueConfig = objectValueConfig;
|
||||
object_values_service_1.objectValuesService.addEntity(target, objectValueConfig);
|
||||
};
|
||||
}
|
||||
exports.ObjectValue = ObjectValue;
|
|
@ -7,8 +7,7 @@ export { DataTransformersService, DataTransformer } from "./services/data-transf
|
|||
export { ModelEntity, EntityConfig, ModelEntityCacheConfig } from "./entity/entity.config";
|
||||
export { entityFieldsService } from "./services/entity-fields.service";
|
||||
export { entitiesService } from "./services/entities.service";
|
||||
export { ModelObjectValue } from "./entity/object-value.config";
|
||||
export { EntityField } from "./entity/entity-field.decorator";
|
||||
export { ObjectValue } from "./entity/object-value.decorator";
|
||||
export { ValueObject } from "./entity/value-object.decorator";
|
||||
export { Entity } from "./entity/entity.decorator";
|
||||
export { ParisModule } from "./paris.module";
|
||||
|
|
|
@ -12,12 +12,10 @@ var entity_fields_service_1 = require("./services/entity-fields.service");
|
|||
exports.entityFieldsService = entity_fields_service_1.entityFieldsService;
|
||||
var entities_service_1 = require("./services/entities.service");
|
||||
exports.entitiesService = entities_service_1.entitiesService;
|
||||
var object_value_config_1 = require("./entity/object-value.config");
|
||||
exports.ModelObjectValue = object_value_config_1.ModelObjectValue;
|
||||
var entity_field_decorator_1 = require("./entity/entity-field.decorator");
|
||||
exports.EntityField = entity_field_decorator_1.EntityField;
|
||||
var object_value_decorator_1 = require("./entity/object-value.decorator");
|
||||
exports.ObjectValue = object_value_decorator_1.ObjectValue;
|
||||
var value_object_decorator_1 = require("./entity/value-object.decorator");
|
||||
exports.ValueObject = value_object_decorator_1.ValueObject;
|
||||
var entity_decorator_1 = require("./entity/entity.decorator");
|
||||
exports.Entity = entity_decorator_1.Entity;
|
||||
var paris_module_1 = require("./paris.module");
|
||||
|
|
|
@ -34,9 +34,10 @@ export declare class Repository<T extends IIdentifiable> implements IRepository
|
|||
* @param itemData
|
||||
* @returns {Observable<ModelData>}
|
||||
*/
|
||||
private getModelData(itemData);
|
||||
private static getModelData(itemData, entity, config, repositoryManagerService, entityConstructor);
|
||||
private static mapToEntityFieldIndex(entityFieldId, value);
|
||||
private getEntityItem(repository, itemData);
|
||||
private static getEntityItem(repository, itemData);
|
||||
private static getValueObjectItem(valueObjectType, data, repositoryManagerService, config?);
|
||||
getItemsDataSet(options?: DataSetOptions): Observable<DataSet<T>>;
|
||||
getItemById(itemId: string | number, allowCache?: boolean): Observable<T>;
|
||||
private setAllItems();
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var entity_config_1 = require("../entity/entity.config");
|
||||
var Observable_1 = require("rxjs/Observable");
|
||||
var Subject_1 = require("rxjs/Subject");
|
||||
var data_transformers_service_1 = require("../services/data-transformers.service");
|
||||
var cache_1 = require("../services/cache");
|
||||
var object_values_service_1 = require("../services/object-values.service");
|
||||
var value_objects_service_1 = require("../services/value-objects.service");
|
||||
var _ = require("lodash");
|
||||
var Repository = /** @class */ (function () {
|
||||
function Repository(entity, config, entityConstructor, dataStore, repositoryManagerService) {
|
||||
|
@ -55,7 +56,7 @@ var Repository = /** @class */ (function () {
|
|||
});
|
||||
Repository.prototype.createItem = function (itemData) {
|
||||
var _this = this;
|
||||
return this.getModelData(itemData)
|
||||
return Repository.getModelData(itemData, this.entity, this.config, this.repositoryManagerService, this.entityConstructor)
|
||||
.map(function (modelData) { return new _this.entityConstructor(modelData); });
|
||||
};
|
||||
Repository.prototype.createNewItem = function () {
|
||||
|
@ -67,32 +68,41 @@ var Repository = /** @class */ (function () {
|
|||
* @param itemData
|
||||
* @returns {Observable<ModelData>}
|
||||
*/
|
||||
Repository.prototype.getModelData = function (itemData) {
|
||||
var _this = this;
|
||||
var modelData = { id: itemData[this.entity.idProperty || this.config.entityIdProperty] }, subModels = [];
|
||||
this.entity.fields.forEach(function (entityField) {
|
||||
Repository.getModelData = function (itemData, entity, config, repositoryManagerService, entityConstructor) {
|
||||
var modelData = entity instanceof entity_config_1.ModelEntity ? { id: itemData[entity.idProperty || config.entityIdProperty] } : {}, subModels = [];
|
||||
entity.fields.forEach(function (entityField) {
|
||||
var propertyValue = entityField.data ? _.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
if (propertyValue === undefined || propertyValue === null) {
|
||||
modelData[entityField.id] = entityField.defaultValue || null;
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
var propertyRepository_1 = _this.repositoryManagerService.getRepository(entityField.type);
|
||||
var propertyRepository_1 = repositoryManagerService.getRepository(entityField.type);
|
||||
if (propertyRepository_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(_this, entityField.id);
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return _this.getEntityItem(propertyRepository_1, memberData); });
|
||||
getPropertyEntityValue$ = Observable_1.Observable.combineLatest.apply(_this, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getEntityItem(propertyRepository_1, memberData); });
|
||||
getPropertyEntityValue$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else {
|
||||
getPropertyEntityValue$ = _this.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
var objectValueType = object_values_service_1.objectValuesService.getEntityByType(entityField.type);
|
||||
if (objectValueType)
|
||||
modelData[entityField.id] = objectValueType.getValueById(propertyValue) || propertyValue;
|
||||
var valueObjectType_1 = value_objects_service_1.valueObjectsService.getEntityByType(entityField.type);
|
||||
if (valueObjectType_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getValueObjectItem(valueObjectType_1, memberData, repositoryManagerService, config); });
|
||||
getPropertyEntityValue$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else {
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
modelData[entityField.id] = entityField.isArray
|
||||
? propertyValue
|
||||
|
@ -104,9 +114,9 @@ var Repository = /** @class */ (function () {
|
|||
}
|
||||
});
|
||||
if (subModels.length) {
|
||||
return Observable_1.Observable.combineLatest.apply(this, subModels).map(function (propertyEntityValues) {
|
||||
return Observable_1.Observable.combineLatest.apply(Observable_1.Observable, subModels).map(function (propertyEntityValues) {
|
||||
propertyEntityValues.forEach(function (propertyEntityValue) { return Object.assign(modelData, propertyEntityValue); });
|
||||
return new _this.entityConstructor(modelData);
|
||||
return new entityConstructor(modelData);
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -117,9 +127,16 @@ var Repository = /** @class */ (function () {
|
|||
data[entityFieldId] = value;
|
||||
return data;
|
||||
};
|
||||
Repository.prototype.getEntityItem = function (repository, itemData) {
|
||||
Repository.getEntityItem = function (repository, itemData) {
|
||||
return Object(itemData) === itemData ? repository.createItem(itemData) : repository.getItemById(itemData);
|
||||
};
|
||||
Repository.getValueObjectItem = function (valueObjectType, data, repositoryManagerService, config) {
|
||||
// If the value object is one of a list of values, just set it to the model
|
||||
if (valueObjectType.hasValue(data))
|
||||
return Observable_1.Observable.of(valueObjectType.getValueById(data));
|
||||
return Repository.getModelData(data, valueObjectType, config, repositoryManagerService, valueObjectType.entityConstructor)
|
||||
.map(function (modelData) { return new valueObjectType.entityConstructor(modelData); });
|
||||
};
|
||||
Repository.prototype.getItemsDataSet = function (options) {
|
||||
var _this = this;
|
||||
return this.dataStore.get(this.entity.endpoint + "/" + (this.entity.allItemsEndpoint || ''), options, this.baseUrl)
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
import { EntitiesServiceBase } from "./entities.service.base";
|
||||
import { ModelObjectValue } from "../entity/object-value.config";
|
||||
export declare class ObjectValuesService extends EntitiesServiceBase<ModelObjectValue> {
|
||||
}
|
||||
export declare let objectValuesService: ObjectValuesService;
|
|
@ -1,22 +0,0 @@
|
|||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var entities_service_base_1 = require("./entities.service.base");
|
||||
var ObjectValuesService = /** @class */ (function (_super) {
|
||||
__extends(ObjectValuesService, _super);
|
||||
function ObjectValuesService() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
return ObjectValuesService;
|
||||
}(entities_service_base_1.EntitiesServiceBase));
|
||||
exports.ObjectValuesService = ObjectValuesService;
|
||||
exports.objectValuesService = new ObjectValuesService;
|
|
@ -33,6 +33,114 @@ function createCommonjsModule(fn, module) {
|
|||
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
||||
}
|
||||
|
||||
var immutability = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Immutability = /** @class */ (function () {
|
||||
function Immutability() {
|
||||
}
|
||||
Immutability.freeze = function (obj) {
|
||||
if (!Object.isFrozen(obj))
|
||||
Object.freeze(obj);
|
||||
if (Object(obj) === obj)
|
||||
Object.getOwnPropertyNames(obj).forEach(function (prop) { return Immutability.freeze(obj[prop]); });
|
||||
return obj;
|
||||
};
|
||||
Immutability.unfreeze = function (obj) {
|
||||
if (Object(obj) !== obj || obj instanceof Date || obj instanceof RegExp || obj instanceof Function)
|
||||
return obj;
|
||||
var unfrozenObj = Object.create(obj.constructor.prototype);
|
||||
Object.assign(unfrozenObj, obj);
|
||||
Object.getOwnPropertyNames(obj).forEach(function (prop) {
|
||||
unfrozenObj[prop] = Immutability.unfreeze(unfrozenObj[prop]);
|
||||
});
|
||||
return unfrozenObj;
|
||||
};
|
||||
return Immutability;
|
||||
}());
|
||||
exports.Immutability = Immutability;
|
||||
});
|
||||
|
||||
unwrapExports(immutability);
|
||||
|
||||
var entityConfig_base = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
var EntityConfigBase = /** @class */ (function () {
|
||||
function EntityConfigBase(config, entityConstructor) {
|
||||
this.entityConstructor = entityConstructor;
|
||||
if (config.values) {
|
||||
config.values = config.values.map(function (valueConfig) { return new entityConstructor(valueConfig); });
|
||||
immutability.Immutability.freeze(config.values);
|
||||
}
|
||||
Object.assign(this, config);
|
||||
}
|
||||
Object.defineProperty(EntityConfigBase.prototype, "fieldsArray", {
|
||||
get: function () {
|
||||
return this.fields ? Array.from(this.fields.values()) : [];
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(EntityConfigBase.prototype, "valuesMap", {
|
||||
get: function () {
|
||||
var _this = this;
|
||||
if (this._valuesMap === undefined) {
|
||||
if (!this.values)
|
||||
this._valuesMap = null;
|
||||
else {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(function (value) { return _this._valuesMap.set(value.id, value); });
|
||||
}
|
||||
}
|
||||
return this._valuesMap;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
EntityConfigBase.prototype.getValueById = function (valueId) {
|
||||
return this.valuesMap ? this.valuesMap.get(valueId) : null;
|
||||
};
|
||||
EntityConfigBase.prototype.hasValue = function (valueId) {
|
||||
return this.valuesMap ? this.valuesMap.has(valueId) : false;
|
||||
};
|
||||
return EntityConfigBase;
|
||||
}());
|
||||
exports.EntityConfigBase = EntityConfigBase;
|
||||
});
|
||||
|
||||
unwrapExports(entityConfig_base);
|
||||
|
||||
var entity_config = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
var ModelEntity = /** @class */ (function (_super) {
|
||||
__extends(ModelEntity, _super);
|
||||
function ModelEntity(config, entityConstructor) {
|
||||
var _this = _super.call(this, config, entityConstructor) || this;
|
||||
_this.loadAll = false;
|
||||
_this.loadAll = config.loadAll === true;
|
||||
return _this;
|
||||
}
|
||||
return ModelEntity;
|
||||
}(entityConfig_base.EntityConfigBase));
|
||||
exports.ModelEntity = ModelEntity;
|
||||
});
|
||||
|
||||
unwrapExports(entity_config);
|
||||
|
||||
var dataTransformers_service = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
@ -249,7 +357,7 @@ exports.EntitiesServiceBase = EntitiesServiceBase;
|
|||
|
||||
unwrapExports(entities_service_base);
|
||||
|
||||
var objectValues_service = createCommonjsModule(function (module, exports) {
|
||||
var valueObjects_service = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
|
@ -263,18 +371,18 @@ var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
|
|||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
var ObjectValuesService = /** @class */ (function (_super) {
|
||||
__extends(ObjectValuesService, _super);
|
||||
function ObjectValuesService() {
|
||||
var ValueObjectsService = /** @class */ (function (_super) {
|
||||
__extends(ValueObjectsService, _super);
|
||||
function ValueObjectsService() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
return ObjectValuesService;
|
||||
return ValueObjectsService;
|
||||
}(entities_service_base.EntitiesServiceBase));
|
||||
exports.ObjectValuesService = ObjectValuesService;
|
||||
exports.objectValuesService = new ObjectValuesService;
|
||||
exports.ValueObjectsService = ValueObjectsService;
|
||||
exports.valueObjectsService = new ValueObjectsService;
|
||||
});
|
||||
|
||||
unwrapExports(objectValues_service);
|
||||
unwrapExports(valueObjects_service);
|
||||
|
||||
var lodash = createCommonjsModule(function (module, exports) {
|
||||
/**
|
||||
|
@ -17370,6 +17478,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
|
||||
|
||||
|
||||
|
||||
var Repository = /** @class */ (function () {
|
||||
function Repository(entity, config, entityConstructor, dataStore, repositoryManagerService) {
|
||||
this.entity = entity;
|
||||
|
@ -17419,7 +17528,7 @@ var Repository = /** @class */ (function () {
|
|||
});
|
||||
Repository.prototype.createItem = function (itemData) {
|
||||
var _this = this;
|
||||
return this.getModelData(itemData)
|
||||
return Repository.getModelData(itemData, this.entity, this.config, this.repositoryManagerService, this.entityConstructor)
|
||||
.map(function (modelData) { return new _this.entityConstructor(modelData); });
|
||||
};
|
||||
Repository.prototype.createNewItem = function () {
|
||||
|
@ -17431,32 +17540,41 @@ var Repository = /** @class */ (function () {
|
|||
* @param itemData
|
||||
* @returns {Observable<ModelData>}
|
||||
*/
|
||||
Repository.prototype.getModelData = function (itemData) {
|
||||
var _this = this;
|
||||
var modelData = { id: itemData[this.entity.idProperty || this.config.entityIdProperty] }, subModels = [];
|
||||
this.entity.fields.forEach(function (entityField) {
|
||||
Repository.getModelData = function (itemData, entity, config, repositoryManagerService, entityConstructor) {
|
||||
var modelData = entity instanceof entity_config.ModelEntity ? { id: itemData[entity.idProperty || config.entityIdProperty] } : {}, subModels = [];
|
||||
entity.fields.forEach(function (entityField) {
|
||||
var propertyValue = entityField.data ? lodash.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
if (propertyValue === undefined || propertyValue === null) {
|
||||
modelData[entityField.id] = entityField.defaultValue || null;
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
var propertyRepository_1 = _this.repositoryManagerService.getRepository(entityField.type);
|
||||
var propertyRepository_1 = repositoryManagerService.getRepository(entityField.type);
|
||||
if (propertyRepository_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(_this, entityField.id);
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return _this.getEntityItem(propertyRepository_1, memberData); });
|
||||
getPropertyEntityValue$ = Observable.Observable.combineLatest.apply(_this, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getEntityItem(propertyRepository_1, memberData); });
|
||||
getPropertyEntityValue$ = Observable.Observable.combineLatest.apply(Observable.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else {
|
||||
getPropertyEntityValue$ = _this.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
var objectValueType = objectValues_service.objectValuesService.getEntityByType(entityField.type);
|
||||
if (objectValueType)
|
||||
modelData[entityField.id] = objectValueType.getValueById(propertyValue) || propertyValue;
|
||||
var valueObjectType_1 = valueObjects_service.valueObjectsService.getEntityByType(entityField.type);
|
||||
if (valueObjectType_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getValueObjectItem(valueObjectType_1, memberData, repositoryManagerService, config); });
|
||||
getPropertyEntityValue$ = Observable.Observable.combineLatest.apply(Observable.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else {
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository_1, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
modelData[entityField.id] = entityField.isArray
|
||||
? propertyValue
|
||||
|
@ -17468,9 +17586,9 @@ var Repository = /** @class */ (function () {
|
|||
}
|
||||
});
|
||||
if (subModels.length) {
|
||||
return Observable.Observable.combineLatest.apply(this, subModels).map(function (propertyEntityValues) {
|
||||
return Observable.Observable.combineLatest.apply(Observable.Observable, subModels).map(function (propertyEntityValues) {
|
||||
propertyEntityValues.forEach(function (propertyEntityValue) { return Object.assign(modelData, propertyEntityValue); });
|
||||
return new _this.entityConstructor(modelData);
|
||||
return new entityConstructor(modelData);
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -17481,9 +17599,16 @@ var Repository = /** @class */ (function () {
|
|||
data[entityFieldId] = value;
|
||||
return data;
|
||||
};
|
||||
Repository.prototype.getEntityItem = function (repository, itemData) {
|
||||
Repository.getEntityItem = function (repository, itemData) {
|
||||
return Object(itemData) === itemData ? repository.createItem(itemData) : repository.getItemById(itemData);
|
||||
};
|
||||
Repository.getValueObjectItem = function (valueObjectType, data, repositoryManagerService, config) {
|
||||
// If the value object is one of a list of values, just set it to the model
|
||||
if (valueObjectType.hasValue(data))
|
||||
return Observable.Observable.of(valueObjectType.getValueById(data));
|
||||
return Repository.getModelData(data, valueObjectType, config, repositoryManagerService, valueObjectType.entityConstructor)
|
||||
.map(function (modelData) { return new valueObjectType.entityConstructor(modelData); });
|
||||
};
|
||||
Repository.prototype.getItemsDataSet = function (options) {
|
||||
var _this = this;
|
||||
return this.dataStore.get(this.entity.endpoint + "/" + (this.entity.allItemsEndpoint || ''), options, this.baseUrl)
|
||||
|
@ -17710,127 +17835,6 @@ exports.RepositoryManagerService = RepositoryManagerService;
|
|||
|
||||
unwrapExports(repositoryManager_service);
|
||||
|
||||
var entityConfig_base = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var EntityConfigBase = /** @class */ (function () {
|
||||
function EntityConfigBase(config) {
|
||||
Object.assign(this, config);
|
||||
}
|
||||
Object.defineProperty(EntityConfigBase.prototype, "fieldsArray", {
|
||||
get: function () {
|
||||
return this.fields ? Array.from(this.fields.values()) : [];
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
return EntityConfigBase;
|
||||
}());
|
||||
exports.EntityConfigBase = EntityConfigBase;
|
||||
});
|
||||
|
||||
unwrapExports(entityConfig_base);
|
||||
|
||||
var entity_config = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
var ModelEntity = /** @class */ (function (_super) {
|
||||
__extends(ModelEntity, _super);
|
||||
function ModelEntity(config) {
|
||||
var _this = _super.call(this, config) || this;
|
||||
_this.loadAll = false;
|
||||
_this.loadAll = config.loadAll === true;
|
||||
return _this;
|
||||
}
|
||||
return ModelEntity;
|
||||
}(entityConfig_base.EntityConfigBase));
|
||||
exports.ModelEntity = ModelEntity;
|
||||
});
|
||||
|
||||
unwrapExports(entity_config);
|
||||
|
||||
var immutability = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Immutability = /** @class */ (function () {
|
||||
function Immutability() {
|
||||
}
|
||||
Immutability.freeze = function (obj) {
|
||||
if (!Object.isFrozen(obj))
|
||||
Object.freeze(obj);
|
||||
if (Object(obj) === obj)
|
||||
Object.getOwnPropertyNames(obj).forEach(function (prop) { return Immutability.freeze(obj[prop]); });
|
||||
return obj;
|
||||
};
|
||||
Immutability.unfreeze = function (obj) {
|
||||
if (Object(obj) !== obj || obj instanceof Date || obj instanceof RegExp || obj instanceof Function)
|
||||
return obj;
|
||||
var unfrozenObj = Object.create(obj.constructor.prototype);
|
||||
Object.assign(unfrozenObj, obj);
|
||||
Object.getOwnPropertyNames(obj).forEach(function (prop) {
|
||||
unfrozenObj[prop] = Immutability.unfreeze(unfrozenObj[prop]);
|
||||
});
|
||||
return unfrozenObj;
|
||||
};
|
||||
return Immutability;
|
||||
}());
|
||||
exports.Immutability = Immutability;
|
||||
});
|
||||
|
||||
unwrapExports(immutability);
|
||||
|
||||
var objectValue_config = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
|
||||
var ModelObjectValue = /** @class */ (function (_super) {
|
||||
__extends(ModelObjectValue, _super);
|
||||
function ModelObjectValue(config) {
|
||||
var _this = _super.call(this, config) || this;
|
||||
if (config.values) {
|
||||
immutability.Immutability.freeze(_this.values);
|
||||
}
|
||||
return _this;
|
||||
}
|
||||
ModelObjectValue.prototype.getValueById = function (valueId) {
|
||||
var _this = this;
|
||||
if (!this.values)
|
||||
return null;
|
||||
if (!this._valuesMap) {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(function (value) { return _this._valuesMap.set(value.id, value); });
|
||||
}
|
||||
return this._valuesMap ? this._valuesMap.get(valueId) : null;
|
||||
};
|
||||
return ModelObjectValue;
|
||||
}(entityConfig_base.EntityConfigBase));
|
||||
exports.ModelObjectValue = ModelObjectValue;
|
||||
});
|
||||
|
||||
unwrapExports(objectValue_config);
|
||||
|
||||
var entityField_decorator = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
@ -17852,24 +17856,22 @@ exports.EntityField = EntityField;
|
|||
|
||||
unwrapExports(entityField_decorator);
|
||||
|
||||
var objectValue_decorator = createCommonjsModule(function (module, exports) {
|
||||
var valueObject_decorator = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
|
||||
function ObjectValue(config) {
|
||||
function ValueObject(config) {
|
||||
return function (target) {
|
||||
if (config.values)
|
||||
config.values = config.values.map(function (valueConfig) { return new target.prototype.constructor(valueConfig); });
|
||||
var objectValueConfig = new objectValue_config.ModelObjectValue(config);
|
||||
target.objectValueConfig = objectValueConfig;
|
||||
objectValues_service.objectValuesService.addEntity(target, objectValueConfig);
|
||||
var valueObjectConfig = new entityConfig_base.EntityConfigBase(config, target.prototype.constructor);
|
||||
target.valueObjectConfig = valueObjectConfig;
|
||||
valueObjects_service.valueObjectsService.addEntity(target, valueObjectConfig);
|
||||
};
|
||||
}
|
||||
exports.ObjectValue = ObjectValue;
|
||||
exports.ValueObject = ValueObject;
|
||||
});
|
||||
|
||||
unwrapExports(objectValue_decorator);
|
||||
unwrapExports(valueObject_decorator);
|
||||
|
||||
var entity_decorator = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
|
@ -17878,7 +17880,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
|
||||
function Entity(config) {
|
||||
return function (target) {
|
||||
var entity = new entity_config.ModelEntity(config);
|
||||
var entity = new entity_config.ModelEntity(config, target.prototype.constructor);
|
||||
target.entityConfig = entity;
|
||||
entities_service.entitiesService.addEntity(target, entity);
|
||||
};
|
||||
|
@ -17965,11 +17967,9 @@ exports.entityFieldsService = entityFields_service.entityFieldsService;
|
|||
|
||||
exports.entitiesService = entities_service.entitiesService;
|
||||
|
||||
exports.ModelObjectValue = objectValue_config.ModelObjectValue;
|
||||
|
||||
exports.EntityField = entityField_decorator.EntityField;
|
||||
|
||||
exports.ObjectValue = objectValue_decorator.ObjectValue;
|
||||
exports.ValueObject = valueObject_decorator.ValueObject;
|
||||
|
||||
exports.Entity = entity_decorator.Entity;
|
||||
|
||||
|
@ -17983,11 +17983,10 @@ var main_3 = main.DataTransformersService;
|
|||
var main_4 = main.ModelEntity;
|
||||
var main_5 = main.entityFieldsService;
|
||||
var main_6 = main.entitiesService;
|
||||
var main_7 = main.ModelObjectValue;
|
||||
var main_8 = main.EntityField;
|
||||
var main_9 = main.ObjectValue;
|
||||
var main_10 = main.Entity;
|
||||
var main_11 = main.ParisModule;
|
||||
var main_7 = main.EntityField;
|
||||
var main_8 = main.ValueObject;
|
||||
var main_9 = main.Entity;
|
||||
var main_10 = main.ParisModule;
|
||||
|
||||
exports['default'] = main$1;
|
||||
exports.Repository = main_1;
|
||||
|
@ -17996,11 +17995,10 @@ exports.DataTransformersService = main_3;
|
|||
exports.ModelEntity = main_4;
|
||||
exports.entityFieldsService = main_5;
|
||||
exports.entitiesService = main_6;
|
||||
exports.ModelObjectValue = main_7;
|
||||
exports.EntityField = main_8;
|
||||
exports.ObjectValue = main_9;
|
||||
exports.Entity = main_10;
|
||||
exports.ParisModule = main_11;
|
||||
exports.EntityField = main_7;
|
||||
exports.ValueObject = main_8;
|
||||
exports.Entity = main_9;
|
||||
exports.ParisModule = main_10;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import {IIdentifiable} from "../models/identifiable.model";
|
||||
import {ModelEntity} from "./entity.config";
|
||||
import {ModelObjectValue} from "./object-value.config";
|
||||
import {EntityConfigBase} from "./entity-config.base";
|
||||
|
||||
export interface DataEntityConstructor<T> extends DataEntityType{
|
||||
new(data?:IIdentifiable): T
|
||||
new(data?:any): T
|
||||
}
|
||||
|
||||
export interface DataEntityType{
|
||||
new(data:IIdentifiable):any,
|
||||
entityConfig?:ModelEntity,
|
||||
objectValueConfig?:ModelObjectValue
|
||||
valueObjectConfig?:EntityConfigBase
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import {EntityFields} from "./entity-fields";
|
||||
import {Field} from "./entity-field";
|
||||
import {IIdentifiable} from "../models/identifiable.model";
|
||||
import {Immutability} from "../services/immutability";
|
||||
import {DataEntityConstructor} from "./data-entity.base";
|
||||
|
||||
export abstract class EntityConfigBase{
|
||||
export class EntityConfigBase{
|
||||
singularName:string;
|
||||
pluralName:string;
|
||||
fields?:EntityFields;
|
||||
|
@ -11,9 +14,38 @@ export abstract class EntityConfigBase{
|
|||
return this.fields ? Array.from(this.fields.values()) : [];
|
||||
}
|
||||
|
||||
constructor(config:IEntityConfigBase){
|
||||
values:ReadonlyArray<IIdentifiable>;
|
||||
|
||||
private _valuesMap:Map<string|number, IIdentifiable>;
|
||||
private get valuesMap():Map<string|number, IIdentifiable> {
|
||||
if (this._valuesMap === undefined) {
|
||||
if (!this.values)
|
||||
this._valuesMap = null;
|
||||
else {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(value => this._valuesMap.set(value.id, value));
|
||||
}
|
||||
}
|
||||
|
||||
return this._valuesMap;
|
||||
}
|
||||
|
||||
constructor(config:IEntityConfigBase, public entityConstructor:DataEntityConstructor<any>){
|
||||
if (config.values) {
|
||||
config.values = config.values.map(valueConfig => new entityConstructor(valueConfig));
|
||||
Immutability.freeze(config.values);
|
||||
}
|
||||
|
||||
Object.assign(this, config);
|
||||
}
|
||||
|
||||
getValueById(valueId:string|number):IIdentifiable{
|
||||
return this.valuesMap ? this.valuesMap.get(valueId) : null;
|
||||
}
|
||||
|
||||
hasValue(valueId:string|number):boolean{
|
||||
return this.valuesMap ? this.valuesMap.has(valueId) : false;
|
||||
}
|
||||
}
|
||||
|
||||
export interface IEntityConfigBase{
|
||||
|
@ -21,4 +53,5 @@ export interface IEntityConfigBase{
|
|||
pluralName:string,
|
||||
fields?:EntityFields,
|
||||
idProperty?:string,
|
||||
values?:Array<IIdentifiable>
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import {EntityConfigBase, IEntityConfigBase} from "./entity-config.base";
|
||||
import {ParisConfig} from "../config/paris-config";
|
||||
import {DataEntityConstructor} from "./data-entity.base";
|
||||
|
||||
export class ModelEntity extends EntityConfigBase{
|
||||
endpoint:EntityConfigFunctionOrValue;
|
||||
|
@ -9,15 +10,15 @@ export class ModelEntity extends EntityConfigBase{
|
|||
allItemsProperty?:string;
|
||||
allItemsEndpoint?:string;
|
||||
|
||||
constructor(config:EntityConfig){
|
||||
super(config);
|
||||
constructor(config:EntityConfig, entityConstructor:DataEntityConstructor<any>){
|
||||
super(config, entityConstructor);
|
||||
|
||||
this.loadAll = config.loadAll === true;
|
||||
}
|
||||
}
|
||||
|
||||
export interface EntityConfig extends IEntityConfigBase{
|
||||
endpoint:EntityConfigFunctionOrValue,
|
||||
endpoint?:EntityConfigFunctionOrValue,
|
||||
loadAll?:boolean,
|
||||
cache?:ModelEntityCacheConfig,
|
||||
baseUrl?:EntityConfigFunctionOrValue,
|
||||
|
|
|
@ -4,7 +4,7 @@ import {entitiesService} from "../services/entities.service";
|
|||
|
||||
export function Entity(config:EntityConfig){
|
||||
return (target:DataEntityType) => {
|
||||
let entity:ModelEntity = new ModelEntity(config);
|
||||
let entity:ModelEntity = new ModelEntity(config, target.prototype.constructor);
|
||||
target.entityConfig = entity;
|
||||
entitiesService.addEntity(target, entity);
|
||||
}
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
import {EntityConfigBase, IEntityConfigBase} from "./entity-config.base";
|
||||
import {IIdentifiable} from "../models/identifiable.model";
|
||||
import {Immutability} from "../services/immutability";
|
||||
|
||||
export class ModelObjectValue extends EntityConfigBase{
|
||||
values:ReadonlyArray<IIdentifiable>;
|
||||
|
||||
private _valuesMap:Map<string|number, IIdentifiable>;
|
||||
|
||||
constructor(config:ObjectValueConfig){
|
||||
super(config);
|
||||
|
||||
if (config.values) {
|
||||
Immutability.freeze(this.values);
|
||||
}
|
||||
}
|
||||
|
||||
getValueById(valueId:string|number):IIdentifiable{
|
||||
if (!this.values)
|
||||
return null;
|
||||
|
||||
if (!this._valuesMap) {
|
||||
this._valuesMap = new Map;
|
||||
this.values.forEach(value => this._valuesMap.set(value.id, value));
|
||||
}
|
||||
return this._valuesMap ? this._valuesMap.get(valueId) : null;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ObjectValueConfig extends IEntityConfigBase{
|
||||
values:ReadonlyArray<IIdentifiable>
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
import {DataEntityType} from "./data-entity.base";
|
||||
import {ModelObjectValue, ObjectValueConfig} from "./object-value.config";
|
||||
import {objectValuesService} from "../services/object-values.service";
|
||||
|
||||
export function ObjectValue(config:ObjectValueConfig){
|
||||
return (target:DataEntityType) => {
|
||||
if (config.values)
|
||||
config.values = config.values.map(valueConfig => new target.prototype.constructor(valueConfig));
|
||||
|
||||
let objectValueConfig:ModelObjectValue = new ModelObjectValue(config);
|
||||
target.objectValueConfig = objectValueConfig;
|
||||
objectValuesService.addEntity(target, objectValueConfig);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import {DataEntityType} from "./data-entity.base";
|
||||
import {valueObjectsService} from "../services/value-objects.service";
|
||||
import {EntityConfigBase, IEntityConfigBase} from "./entity-config.base";
|
||||
|
||||
export function ValueObject(config:IEntityConfigBase){
|
||||
return (target:DataEntityType) => {
|
||||
let valueObjectConfig:EntityConfigBase = new EntityConfigBase(config, target.prototype.constructor);
|
||||
target.valueObjectConfig = valueObjectConfig;
|
||||
valueObjectsService.addEntity(target, valueObjectConfig);
|
||||
}
|
||||
}
|
|
@ -7,8 +7,7 @@ export {DataTransformersService, DataTransformer} from "./services/data-transfor
|
|||
export {ModelEntity, EntityConfig, ModelEntityCacheConfig} from "./entity/entity.config";
|
||||
export {entityFieldsService} from "./services/entity-fields.service";
|
||||
export {entitiesService} from "./services/entities.service";
|
||||
export {ModelObjectValue} from "./entity/object-value.config";
|
||||
export {EntityField} from "./entity/entity-field.decorator";
|
||||
export {ObjectValue} from "./entity/object-value.decorator";
|
||||
export {ValueObject} from "./entity/value-object.decorator";
|
||||
export {Entity} from "./entity/entity.decorator";
|
||||
export {ParisModule} from "./paris.module";
|
||||
|
|
|
@ -12,12 +12,10 @@ import {DataSetOptions} from "../dataset/dataset-options";
|
|||
import {DataSet} from "../dataset/dataset";
|
||||
import {Index} from "../models/index";
|
||||
import {DataTransformersService} from "../services/data-transformers.service";
|
||||
import {Immutability} from "../services/immutability";
|
||||
import {DataCache, DataCacheSettings} from "../services/cache";
|
||||
import {BehaviorSubject} from "rxjs/BehaviorSubject";
|
||||
import {ModelObjectValue} from "../entity/object-value.config";
|
||||
import {objectValuesService} from "../services/object-values.service";
|
||||
import {valueObjectsService} from "../services/value-objects.service";
|
||||
import * as _ from "lodash";
|
||||
import {EntityConfigBase} from "../entity/entity-config.base";
|
||||
|
||||
export class Repository<T extends IIdentifiable> implements IRepository{
|
||||
save$:Observable<T>;
|
||||
|
@ -73,7 +71,7 @@ export class Repository<T extends IIdentifiable> implements IRepository{
|
|||
}
|
||||
|
||||
createItem(itemData:any):Observable<T>{
|
||||
return this.getModelData(itemData)
|
||||
return Repository.getModelData(itemData, this.entity, this.config, this.repositoryManagerService, this.entityConstructor)
|
||||
.map((modelData:ModelData) => new this.entityConstructor(modelData));
|
||||
}
|
||||
|
||||
|
@ -87,38 +85,50 @@ export class Repository<T extends IIdentifiable> implements IRepository{
|
|||
* @param itemData
|
||||
* @returns {Observable<ModelData>}
|
||||
*/
|
||||
private getModelData(itemData:Index):Observable<ModelData>{
|
||||
let modelData:ModelData = { id: itemData[this.entity.idProperty || this.config.entityIdProperty] },
|
||||
subModels:Array<Observable<{ [key:string]:any }>> = [];
|
||||
private static getModelData(itemData:Index, entity:EntityConfigBase, config:ParisConfig, repositoryManagerService:RepositoryManagerService, entityConstructor:DataEntityConstructor<Index>):Observable<Index>{
|
||||
let modelData:Index = entity instanceof ModelEntity ? { id: itemData[entity.idProperty || config.entityIdProperty] } : {},
|
||||
subModels:Array<Observable<Index>> = [];
|
||||
|
||||
this.entity.fields.forEach((entityField:Field) => {
|
||||
entity.fields.forEach((entityField:Field) => {
|
||||
let propertyValue:any = entityField.data ? _.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
|
||||
if (propertyValue === undefined || propertyValue === null){
|
||||
modelData[entityField.id] = entityField.defaultValue || null;
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
let propertyRepository:IRepository = this.repositoryManagerService.getRepository(entityField.type);
|
||||
let propertyRepository:IRepository = repositoryManagerService.getRepository(entityField.type);
|
||||
|
||||
if (propertyRepository){
|
||||
let getPropertyEntityValue$:Observable<Index>;
|
||||
let mapValueToEntityFieldIndex:(value:any) => Index = Repository.mapToEntityFieldIndex.bind(this, entityField.id);
|
||||
let mapValueToEntityFieldIndex:(value:any) => Index = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
|
||||
if (entityField.isArray){
|
||||
let propertyMembers$:Array<Observable<T>> = propertyValue.map((memberData:any) => this.getEntityItem(propertyRepository, memberData));
|
||||
getPropertyEntityValue$ = Observable.combineLatest.apply(this, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
let propertyMembers$:Array<Observable<any>> = propertyValue.map((memberData:any) => Repository.getEntityItem(propertyRepository, memberData));
|
||||
getPropertyEntityValue$ = Observable.combineLatest.apply(Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else{
|
||||
getPropertyEntityValue$ = this.getEntityItem(propertyRepository, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
let objectValueType:ModelObjectValue = objectValuesService.getEntityByType(entityField.type);
|
||||
let valueObjectType:EntityConfigBase = valueObjectsService.getEntityByType(entityField.type);
|
||||
|
||||
if (objectValueType)
|
||||
modelData[entityField.id] = objectValueType.getValueById(propertyValue) || propertyValue;
|
||||
if (valueObjectType) {
|
||||
let getPropertyEntityValue$:Observable<Index>;
|
||||
let mapValueToEntityFieldIndex:(value:any) => Index = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
|
||||
if (entityField.isArray){
|
||||
let propertyMembers$:Array<Observable<any>> = propertyValue.map((memberData:any) => Repository.getValueObjectItem(valueObjectType, memberData, repositoryManagerService, config));
|
||||
getPropertyEntityValue$ = Observable.combineLatest.apply(Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
else{
|
||||
getPropertyEntityValue$ = Repository.getEntityItem(propertyRepository, propertyValue).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
|
||||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
modelData[entityField.id] = entityField.isArray
|
||||
? propertyValue
|
||||
|
@ -131,9 +141,9 @@ export class Repository<T extends IIdentifiable> implements IRepository{
|
|||
});
|
||||
|
||||
if (subModels.length) {
|
||||
return Observable.combineLatest.apply(this, subModels).map((propertyEntityValues: Array<{ [index: string]: any }>) => {
|
||||
return Observable.combineLatest.apply(Observable, subModels).map((propertyEntityValues: Array<{ [index: string]: any }>) => {
|
||||
propertyEntityValues.forEach((propertyEntityValue: { [index: string]: any }) => Object.assign(modelData, propertyEntityValue));
|
||||
return new this.entityConstructor(modelData);
|
||||
return new entityConstructor(modelData);
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -146,10 +156,19 @@ export class Repository<T extends IIdentifiable> implements IRepository{
|
|||
return data;
|
||||
}
|
||||
|
||||
private getEntityItem(repository:IRepository, itemData:any):Observable<T>{
|
||||
private static getEntityItem(repository:IRepository, itemData:any):Observable<Index>{
|
||||
return Object(itemData) === itemData ? repository.createItem(itemData) : repository.getItemById(itemData);
|
||||
}
|
||||
|
||||
private static getValueObjectItem(valueObjectType:EntityConfigBase, data:any, repositoryManagerService:RepositoryManagerService, config?:ParisConfig):Observable<Index>{
|
||||
// If the value object is one of a list of values, just set it to the model
|
||||
if (valueObjectType.hasValue(data))
|
||||
return Observable.of(valueObjectType.getValueById(data));
|
||||
|
||||
return Repository.getModelData(data, valueObjectType, config, repositoryManagerService, valueObjectType.entityConstructor)
|
||||
.map((modelData:ModelData) => new valueObjectType.entityConstructor(modelData));
|
||||
}
|
||||
|
||||
getItemsDataSet(options?:DataSetOptions):Observable<DataSet<T>>{
|
||||
return this.dataStore.get(`${this.entity.endpoint}/${this.entity.allItemsEndpoint || ''}`, options, this.baseUrl)
|
||||
.map((rawDataSet:any) => {
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
import {EntitiesServiceBase} from "./entities.service.base";
|
||||
import {ModelObjectValue} from "../entity/object-value.config";
|
||||
|
||||
export class ObjectValuesService extends EntitiesServiceBase<ModelObjectValue>{}
|
||||
|
||||
export let objectValuesService = new ObjectValuesService;
|
|
@ -0,0 +1,6 @@
|
|||
import {EntitiesServiceBase} from "./entities.service.base";
|
||||
import {EntityConfigBase} from "../entity/entity-config.base";
|
||||
|
||||
export class ValueObjectsService extends EntitiesServiceBase<EntityConfigBase>{}
|
||||
|
||||
export let valueObjectsService = new ValueObjectsService;
|
Загрузка…
Ссылка в новой задаче