зеркало из https://github.com/microsoft/paris.git
Adding options for entities.
This commit is contained in:
Родитель
6228b48341
Коммит
5bd8b40867
|
@ -3,10 +3,10 @@ import { ModelEntity } from "./entity.config";
|
|||
import { EntityConfigBase } from "./entity-config.base";
|
||||
import { ModelBase } from "../models/model.base";
|
||||
export interface DataEntityConstructor<T extends ModelBase> extends DataEntityType {
|
||||
new (data?: any): T;
|
||||
new (data?: any, rawData?: any): T;
|
||||
}
|
||||
export interface DataEntityType {
|
||||
new (data?: EntityModelConfigBase): any;
|
||||
new (data?: EntityModelConfigBase, rawData?: any): any;
|
||||
entityConfig?: ModelEntity;
|
||||
valueObjectConfig?: EntityConfigBase;
|
||||
}
|
||||
|
|
|
@ -2,10 +2,12 @@ import { DataEntityType } from "./data-entity.base";
|
|||
export interface Field {
|
||||
id?: string;
|
||||
name?: string;
|
||||
data?: string;
|
||||
data?: "__self" | string | Array<string>;
|
||||
entity?: DataEntityType;
|
||||
type?: DataEntityType;
|
||||
defaultValue?: any;
|
||||
arrayOf?: DataEntityType;
|
||||
isArray?: boolean;
|
||||
required?: boolean;
|
||||
}
|
||||
export declare const FIELD_DATA_SELF = "__self";
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FIELD_DATA_SELF = "__self";
|
||||
|
|
|
@ -32,14 +32,14 @@ export declare class Repository<T extends EntityModelBase> implements IRepositor
|
|||
/**
|
||||
* Populates the item dataset with any sub @model. For example, if an ID is found for a property whose type is an entity,
|
||||
* the property's value will be an instance of that entity, for the ID, not the ID.
|
||||
* @param {Index} itemData
|
||||
* @param {Index} rawData
|
||||
* @param {EntityConfigBase} entity
|
||||
* @param {ParisConfig} config
|
||||
* @param {Paris} paris
|
||||
* @param {DataOptions} options
|
||||
* @returns {Observable<T extends EntityModelBase>}
|
||||
*/
|
||||
private static getModelData<T>(itemData, entity, config, paris, options?);
|
||||
private static getModelData<T>(rawData, entity, config, paris, options?);
|
||||
private static mapToEntityFieldIndex(entityFieldId, value);
|
||||
private static getEntityItem<U>(repository, itemData, options?);
|
||||
private static getValueObjectItem<U>(valueObjectType, data, paris, config?, options?);
|
||||
|
|
|
@ -3,6 +3,7 @@ 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 entity_field_1 = require("../entity/entity-field");
|
||||
var data_transformers_service_1 = require("../services/data-transformers.service");
|
||||
var cache_1 = require("../services/cache");
|
||||
var value_objects_service_1 = require("../services/value-objects.service");
|
||||
|
@ -65,19 +66,38 @@ var Repository = /** @class */ (function () {
|
|||
/**
|
||||
* Populates the item dataset with any sub @model. For example, if an ID is found for a property whose type is an entity,
|
||||
* the property's value will be an instance of that entity, for the ID, not the ID.
|
||||
* @param {Index} itemData
|
||||
* @param {Index} rawData
|
||||
* @param {EntityConfigBase} entity
|
||||
* @param {ParisConfig} config
|
||||
* @param {Paris} paris
|
||||
* @param {DataOptions} options
|
||||
* @returns {Observable<T extends EntityModelBase>}
|
||||
*/
|
||||
Repository.getModelData = function (itemData, entity, config, paris, options) {
|
||||
Repository.getModelData = function (rawData, entity, config, paris, options) {
|
||||
if (options === void 0) { options = data_options_1.defaultDataOptions; }
|
||||
var entityIdProperty = entity.idProperty || config.entityIdProperty, modelData = entity instanceof entity_config_1.ModelEntity ? { id: itemData[entityIdProperty] } : {}, subModels = [];
|
||||
var entityIdProperty = entity.idProperty || config.entityIdProperty, modelData = entity instanceof entity_config_1.ModelEntity ? { id: rawData[entityIdProperty] } : {}, subModels = [];
|
||||
var getModelDataError = new Error("Failed to create " + entity.singularName + ".");
|
||||
entity.fields.forEach(function (entityField) {
|
||||
var propertyValue = entityField.data ? _.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
var propertyValue;
|
||||
if (entityField.data) {
|
||||
if (entityField.data instanceof Array) {
|
||||
for (var i = 0, path = void 0; i < entityField.data.length && propertyValue === undefined; i++) {
|
||||
path = entityField.data[i];
|
||||
var value = path === entity_field_1.FIELD_DATA_SELF ? rawData : _.get(rawData, path);
|
||||
if (value !== undefined && value !== null)
|
||||
propertyValue = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
propertyValue = entityField.data === entity_field_1.FIELD_DATA_SELF ? rawData : _.get(rawData, entityField.data);
|
||||
}
|
||||
else
|
||||
propertyValue = rawData[entityField.id];
|
||||
if (propertyValue === undefined || propertyValue === null) {
|
||||
if (entityField.required) {
|
||||
getModelDataError.message = getModelDataError.message + (" Field " + entityField.id + " is required but it's " + propertyValue + ".");
|
||||
throw getModelDataError;
|
||||
}
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
|
@ -121,15 +141,17 @@ var Repository = /** @class */ (function () {
|
|||
}
|
||||
}
|
||||
});
|
||||
var model$;
|
||||
if (subModels.length) {
|
||||
return Observable_1.Observable.combineLatest.apply(Observable_1.Observable, subModels).map(function (propertyEntityValues) {
|
||||
model$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, subModels).map(function (propertyEntityValues) {
|
||||
propertyEntityValues.forEach(function (propertyEntityValue) { return Object.assign(modelData, propertyEntityValue); });
|
||||
var model;
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Couldn't create " + entity.singularName + ".", e);
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
propertyEntityValues.forEach(function (modelPropertyValue) {
|
||||
for (var p in modelPropertyValue) {
|
||||
|
@ -143,23 +165,21 @@ var Repository = /** @class */ (function () {
|
|||
modelValue.$parent = model;
|
||||
}
|
||||
});
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
return model;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var model = void 0;
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Couldn't create " + entity.singularName + ".", e);
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
return Observable_1.Observable.of(model);
|
||||
model$ = Observable_1.Observable.of(model);
|
||||
}
|
||||
return entity.readonly ? model$.map(function (model) { return Object.freeze(model); }) : model$;
|
||||
};
|
||||
Repository.mapToEntityFieldIndex = function (entityFieldId, value) {
|
||||
var data = {};
|
||||
|
@ -180,6 +200,7 @@ var Repository = /** @class */ (function () {
|
|||
Repository.prototype.getItemsDataSet = function (options, dataOptions) {
|
||||
var _this = this;
|
||||
if (dataOptions === void 0) { dataOptions = data_options_1.defaultDataOptions; }
|
||||
var getItemsDataSetError = new Error("Failed to get " + this.entity.pluralName + ".");
|
||||
return this.dataStore.get(this.entity.endpoint + "/" + (this.entity.allItemsEndpoint || ''), options, this.baseUrl)
|
||||
.map(function (rawDataSet) {
|
||||
var allItemsProperty = _this.entity.allItemsProperty || _this.config.allItemsProperty;
|
||||
|
@ -198,6 +219,9 @@ var Repository = /** @class */ (function () {
|
|||
count: dataSet.count,
|
||||
items: items
|
||||
});
|
||||
}).catch(function (error) {
|
||||
getItemsDataSetError.message = getItemsDataSetError.message + " Error: " + error.message;
|
||||
throw getItemsDataSetError;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -153,6 +153,14 @@ exports.ModelEntity = ModelEntity;
|
|||
|
||||
unwrapExports(entity_config);
|
||||
|
||||
var entityField = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FIELD_DATA_SELF = "__self";
|
||||
});
|
||||
|
||||
unwrapExports(entityField);
|
||||
|
||||
var dataTransformers_service = createCommonjsModule(function (module, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
@ -437,6 +445,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
|
||||
|
||||
|
||||
|
||||
var Repository = /** @class */ (function () {
|
||||
function Repository(entity, config, entityConstructor, dataStore, paris) {
|
||||
this.entity = entity;
|
||||
|
@ -494,27 +503,46 @@ var Repository = /** @class */ (function () {
|
|||
/**
|
||||
* Populates the item dataset with any sub @model. For example, if an ID is found for a property whose type is an entity,
|
||||
* the property's value will be an instance of that entity, for the ID, not the ID.
|
||||
* @param {Index} itemData
|
||||
* @param {Index} rawData
|
||||
* @param {EntityConfigBase} entity
|
||||
* @param {ParisConfig} config
|
||||
* @param {Paris} paris
|
||||
* @param {DataOptions} options
|
||||
* @returns {Observable<T extends EntityModelBase>}
|
||||
*/
|
||||
Repository.getModelData = function (itemData, entity, config, paris, options) {
|
||||
Repository.getModelData = function (rawData, entity, config, paris, options) {
|
||||
if (options === void 0) { options = data_options.defaultDataOptions; }
|
||||
var entityIdProperty = entity.idProperty || config.entityIdProperty, modelData = entity instanceof entity_config.ModelEntity ? { id: itemData[entityIdProperty] } : {}, subModels = [];
|
||||
entity.fields.forEach(function (entityField) {
|
||||
var propertyValue = entityField.data ? lodash.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
var entityIdProperty = entity.idProperty || config.entityIdProperty, modelData = entity instanceof entity_config.ModelEntity ? { id: rawData[entityIdProperty] } : {}, subModels = [];
|
||||
var getModelDataError = new Error("Failed to create " + entity.singularName + ".");
|
||||
entity.fields.forEach(function (entityField$$1) {
|
||||
var propertyValue;
|
||||
if (entityField$$1.data) {
|
||||
if (entityField$$1.data instanceof Array) {
|
||||
for (var i = 0, path = void 0; i < entityField$$1.data.length && propertyValue === undefined; i++) {
|
||||
path = entityField$$1.data[i];
|
||||
var value = path === entityField.FIELD_DATA_SELF ? rawData : lodash.get(rawData, path);
|
||||
if (value !== undefined && value !== null)
|
||||
propertyValue = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
propertyValue = entityField$$1.data === entityField.FIELD_DATA_SELF ? rawData : lodash.get(rawData, entityField$$1.data);
|
||||
}
|
||||
else
|
||||
propertyValue = rawData[entityField$$1.id];
|
||||
if (propertyValue === undefined || propertyValue === null) {
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
if (entityField$$1.required) {
|
||||
getModelDataError.message = getModelDataError.message + (" Field " + entityField$$1.id + " is required but it's " + propertyValue + ".");
|
||||
throw getModelDataError;
|
||||
}
|
||||
modelData[entityField$$1.id] = entityField$$1.isArray ? [] : entityField$$1.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
var propertyRepository_1 = paris.getRepository(entityField.type);
|
||||
var propertyRepository_1 = paris.getRepository(entityField$$1.type);
|
||||
if (propertyRepository_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField$$1.id);
|
||||
if (entityField$$1.isArray) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getEntityItem(propertyRepository_1, memberData, options); });
|
||||
getPropertyEntityValue$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
}
|
||||
|
@ -523,11 +551,11 @@ var Repository = /** @class */ (function () {
|
|||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
var valueObjectType_1 = valueObjects_service.valueObjectsService.getEntityByType(entityField.type);
|
||||
var valueObjectType_1 = valueObjects_service.valueObjectsService.getEntityByType(entityField$$1.type);
|
||||
if (valueObjectType_1) {
|
||||
var getPropertyEntityValue$ = void 0;
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField.id);
|
||||
if (entityField.isArray) {
|
||||
var mapValueToEntityFieldIndex = Repository.mapToEntityFieldIndex.bind(null, entityField$$1.id);
|
||||
if (entityField$$1.isArray) {
|
||||
if (propertyValue.length) {
|
||||
var propertyMembers$ = propertyValue.map(function (memberData) { return Repository.getValueObjectItem(valueObjectType_1, memberData, paris, config, options); });
|
||||
getPropertyEntityValue$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, propertyMembers$).map(mapValueToEntityFieldIndex);
|
||||
|
@ -541,24 +569,26 @@ var Repository = /** @class */ (function () {
|
|||
subModels.push(getPropertyEntityValue$);
|
||||
}
|
||||
else {
|
||||
modelData[entityField.id] = entityField.isArray
|
||||
modelData[entityField$$1.id] = entityField$$1.isArray
|
||||
? propertyValue
|
||||
? propertyValue.map(function (elementValue) { return dataTransformers_service.DataTransformersService.parse(entityField.type, elementValue); })
|
||||
? propertyValue.map(function (elementValue) { return dataTransformers_service.DataTransformersService.parse(entityField$$1.type, elementValue); })
|
||||
: []
|
||||
: dataTransformers_service.DataTransformersService.parse(entityField.type, propertyValue);
|
||||
: dataTransformers_service.DataTransformersService.parse(entityField$$1.type, propertyValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
var model$;
|
||||
if (subModels.length) {
|
||||
return Observable_1.Observable.combineLatest.apply(Observable_1.Observable, subModels).map(function (propertyEntityValues) {
|
||||
model$ = Observable_1.Observable.combineLatest.apply(Observable_1.Observable, subModels).map(function (propertyEntityValues) {
|
||||
propertyEntityValues.forEach(function (propertyEntityValue) { return Object.assign(modelData, propertyEntityValue); });
|
||||
var model;
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Couldn't create " + entity.singularName + ".", e);
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
propertyEntityValues.forEach(function (modelPropertyValue) {
|
||||
for (var p in modelPropertyValue) {
|
||||
|
@ -572,23 +602,21 @@ var Repository = /** @class */ (function () {
|
|||
modelValue.$parent = model;
|
||||
}
|
||||
});
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
return model;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var model = void 0;
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Couldn't create " + entity.singularName + ".", e);
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
return Observable_1.Observable.of(model);
|
||||
model$ = Observable_1.Observable.of(model);
|
||||
}
|
||||
return entity.readonly ? model$.map(function (model) { return Object.freeze(model); }) : model$;
|
||||
};
|
||||
Repository.mapToEntityFieldIndex = function (entityFieldId, value) {
|
||||
var data = {};
|
||||
|
@ -609,6 +637,7 @@ var Repository = /** @class */ (function () {
|
|||
Repository.prototype.getItemsDataSet = function (options, dataOptions) {
|
||||
var _this = this;
|
||||
if (dataOptions === void 0) { dataOptions = data_options.defaultDataOptions; }
|
||||
var getItemsDataSetError = new Error("Failed to get " + this.entity.pluralName + ".");
|
||||
return this.dataStore.get(this.entity.endpoint + "/" + (this.entity.allItemsEndpoint || ''), options, this.baseUrl)
|
||||
.map(function (rawDataSet) {
|
||||
var allItemsProperty = _this.entity.allItemsProperty || _this.config.allItemsProperty;
|
||||
|
@ -627,6 +656,9 @@ var Repository = /** @class */ (function () {
|
|||
count: dataSet.count,
|
||||
items: items
|
||||
});
|
||||
}).catch(function (error) {
|
||||
getItemsDataSetError.message = getItemsDataSetError.message + " Error: " + error.message;
|
||||
throw getItemsDataSetError;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
@ -676,14 +708,14 @@ var Repository = /** @class */ (function () {
|
|||
for (var propertyId in item) {
|
||||
if (item.hasOwnProperty(propertyId)) {
|
||||
var modelValue = void 0;
|
||||
var propertyValue = item[propertyId], entityField = this.entity.fields.get(propertyId);
|
||||
if (entityField) {
|
||||
var propertyRepository = this.paris.getRepository(entityField.type);
|
||||
var propertyValue = item[propertyId], entityField$$1 = this.entity.fields.get(propertyId);
|
||||
if (entityField$$1) {
|
||||
var propertyRepository = this.paris.getRepository(entityField$$1.type);
|
||||
if (propertyRepository)
|
||||
modelValue = propertyValue.id;
|
||||
else
|
||||
modelValue = dataTransformers_service.DataTransformersService.serialize(entityField.type, propertyValue);
|
||||
modelData[entityField.id] = modelValue;
|
||||
modelValue = dataTransformers_service.DataTransformersService.serialize(entityField$$1.type, propertyValue);
|
||||
modelData[entityField$$1.id] = modelValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ import {EntityConfigBase} from "./entity-config.base";
|
|||
import {ModelBase} from "../models/model.base";
|
||||
|
||||
export interface DataEntityConstructor<T extends ModelBase> extends DataEntityType{
|
||||
new(data?:any): T
|
||||
new(data?:any, rawData?:any): T
|
||||
}
|
||||
|
||||
export interface DataEntityType{
|
||||
new(data?:EntityModelConfigBase):any,
|
||||
new(data?:EntityModelConfigBase, rawData?:any):any,
|
||||
entityConfig?:ModelEntity,
|
||||
valueObjectConfig?:EntityConfigBase
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {DataEntityType} from "./data-entity.base";
|
||||
import {Field} from "./entity-field";
|
||||
import {entitiesService} from "../services/entities.service";
|
||||
import {entityFieldsService} from "../services/entity-fields.service";
|
||||
|
||||
export function EntityField(fieldConfig?:Field):PropertyDecorator {
|
||||
|
|
|
@ -3,10 +3,13 @@ import {DataEntityType} from "./data-entity.base";
|
|||
export interface Field{
|
||||
id?:string,
|
||||
name?:string,
|
||||
data?:string,
|
||||
data?:"__self" | string | Array<string>,
|
||||
entity?:DataEntityType,
|
||||
type?:DataEntityType,
|
||||
defaultValue?:any,
|
||||
arrayOf?:DataEntityType,
|
||||
isArray?:boolean
|
||||
isArray?:boolean,
|
||||
required?:boolean
|
||||
}
|
||||
|
||||
export const FIELD_DATA_SELF = "__self";
|
||||
|
|
|
@ -2,7 +2,7 @@ import {ModelEntity} from "../entity/entity.config";
|
|||
import {DataEntityConstructor} from "../entity/data-entity.base";
|
||||
import {Observable} from "rxjs/Observable";
|
||||
import {Subject} from "rxjs/Subject";
|
||||
import {Field} from "../entity/entity-field";
|
||||
import {Field, FIELD_DATA_SELF} from "../entity/entity-field";
|
||||
import {IRepository} from "./repository.interface";
|
||||
import {DataStoreService} from "../services/data-store.service";
|
||||
import {ParisConfig} from "../config/paris-config";
|
||||
|
@ -42,7 +42,7 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
private get cache(): DataCache<T> {
|
||||
if (!this._cache) {
|
||||
let cacheSettings: DataCacheSettings<T> = Object.assign({
|
||||
getter: (itemId: string | number) => this.getItemById(itemId, { allowCache: false })
|
||||
getter: (itemId: string | number) => this.getItemById(itemId, {allowCache: false})
|
||||
}, this.entity.cache);
|
||||
|
||||
this._cache = new DataCache<T>(cacheSettings);
|
||||
|
@ -72,7 +72,7 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
this.save$ = this._saveSubject$.asObservable();
|
||||
}
|
||||
|
||||
createItem(itemData: any, options:DataOptions = defaultDataOptions): Observable<T> {
|
||||
createItem(itemData: any, options: DataOptions = defaultDataOptions): Observable<T> {
|
||||
return Repository.getModelData(itemData, this.entity, this.config, this.paris);
|
||||
}
|
||||
|
||||
|
@ -83,22 +83,42 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
/**
|
||||
* Populates the item dataset with any sub @model. For example, if an ID is found for a property whose type is an entity,
|
||||
* the property's value will be an instance of that entity, for the ID, not the ID.
|
||||
* @param {Index} itemData
|
||||
* @param {Index} rawData
|
||||
* @param {EntityConfigBase} entity
|
||||
* @param {ParisConfig} config
|
||||
* @param {Paris} paris
|
||||
* @param {DataOptions} options
|
||||
* @returns {Observable<T extends EntityModelBase>}
|
||||
*/
|
||||
private static getModelData<T extends ModelBase>(itemData: Index, entity: EntityConfigBase, config: ParisConfig, paris: Paris, options:DataOptions = defaultDataOptions): Observable<T> {
|
||||
private static getModelData<T extends ModelBase>(rawData: Index, entity: EntityConfigBase, config: ParisConfig, paris: Paris, options: DataOptions = defaultDataOptions): Observable<T> {
|
||||
let entityIdProperty: string = entity.idProperty || config.entityIdProperty,
|
||||
modelData: Index = entity instanceof ModelEntity ? {id: itemData[entityIdProperty]} : {},
|
||||
modelData: Index = entity instanceof ModelEntity ? {id: rawData[entityIdProperty]} : {},
|
||||
subModels: Array<Observable<{ [index: string]: ModelBase | Array<ModelBase> }>> = [];
|
||||
|
||||
let getModelDataError:Error = new Error(`Failed to create ${entity.singularName}.`);
|
||||
|
||||
entity.fields.forEach((entityField: Field) => {
|
||||
let propertyValue: any = entityField.data ? _.get(itemData, entityField.data) : itemData[entityField.id];
|
||||
let propertyValue: any;
|
||||
if (entityField.data) {
|
||||
if (entityField.data instanceof Array) {
|
||||
for (let i = 0, path:string; i < entityField.data.length && propertyValue === undefined; i++) {
|
||||
path = entityField.data[i];
|
||||
let value:any = path === FIELD_DATA_SELF ? rawData : _.get(rawData, path);
|
||||
if (value !== undefined && value !== null)
|
||||
propertyValue = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
propertyValue = entityField.data === FIELD_DATA_SELF ? rawData : _.get(rawData, entityField.data);
|
||||
}
|
||||
else
|
||||
propertyValue = rawData[entityField.id];
|
||||
|
||||
if (propertyValue === undefined || propertyValue === null) {
|
||||
if (entityField.required) {
|
||||
getModelDataError.message = getModelDataError.message + ` Field ${entityField.id} is required but it's ${propertyValue}.`;
|
||||
throw getModelDataError;
|
||||
}
|
||||
modelData[entityField.id] = entityField.isArray ? [] : entityField.defaultValue || null;
|
||||
}
|
||||
else {
|
||||
|
@ -149,16 +169,19 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
}
|
||||
});
|
||||
|
||||
let model$:Observable<T>;
|
||||
|
||||
if (subModels.length) {
|
||||
return Observable.combineLatest.apply(Observable, subModels).map((propertyEntityValues: Array<ModelPropertyValue>) => {
|
||||
model$ = Observable.combineLatest.apply(Observable, subModels).map((propertyEntityValues: Array<ModelPropertyValue>) => {
|
||||
propertyEntityValues.forEach((propertyEntityValue: { [index: string]: any }) => Object.assign(modelData, propertyEntityValue));
|
||||
|
||||
let model:T;
|
||||
let model: T;
|
||||
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
} catch(e){
|
||||
console.error(`Couldn't create ${entity.singularName}.`, e);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
} catch (e) {
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
|
||||
propertyEntityValues.forEach((modelPropertyValue: ModelPropertyValue) => {
|
||||
|
@ -175,26 +198,23 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
}
|
||||
});
|
||||
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
|
||||
return model;
|
||||
});
|
||||
}
|
||||
else {
|
||||
let model:T;
|
||||
let model: T;
|
||||
|
||||
try {
|
||||
model = new entity.entityConstructor(modelData);
|
||||
} catch(e){
|
||||
console.error(`Couldn't create ${entity.singularName}.`, e);
|
||||
model = new entity.entityConstructor(modelData, rawData);
|
||||
} catch (e) {
|
||||
getModelDataError.message = getModelDataError.message + " Error: " + e.message;
|
||||
throw getModelDataError;
|
||||
}
|
||||
|
||||
if (entity.readonly)
|
||||
Object.freeze(model);
|
||||
|
||||
return Observable.of(model);
|
||||
model$ = Observable.of(model);
|
||||
}
|
||||
|
||||
return entity.readonly ? model$.map(model => Object.freeze(model)) : model$;
|
||||
}
|
||||
|
||||
private static mapToEntityFieldIndex(entityFieldId: string, value: ModelBase | Array<ModelBase>): ModelPropertyValue {
|
||||
|
@ -203,11 +223,11 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
return data;
|
||||
}
|
||||
|
||||
private static getEntityItem<U extends EntityModelBase>(repository: Repository<U>, itemData: any, options:DataOptions = defaultDataOptions): Observable<U> {
|
||||
private static getEntityItem<U extends EntityModelBase>(repository: Repository<U>, itemData: any, options: DataOptions = defaultDataOptions): Observable<U> {
|
||||
return Object(itemData) === itemData ? repository.createItem(itemData, options) : repository.getItemById(itemData, options);
|
||||
}
|
||||
|
||||
private static getValueObjectItem<U extends ModelBase>(valueObjectType: EntityConfigBase, data: any, paris: Paris, config?: ParisConfig, options:DataOptions = defaultDataOptions): Observable<U> {
|
||||
private static getValueObjectItem<U extends ModelBase>(valueObjectType: EntityConfigBase, data: any, paris: Paris, config?: ParisConfig, options: DataOptions = defaultDataOptions): Observable<U> {
|
||||
// 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));
|
||||
|
@ -215,7 +235,9 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
return Repository.getModelData(data, valueObjectType, config, paris, options);
|
||||
}
|
||||
|
||||
getItemsDataSet(options?: DataSetOptions, dataOptions:DataOptions = defaultDataOptions): Observable<DataSet<T>> {
|
||||
getItemsDataSet(options?: DataSetOptions, dataOptions: DataOptions = defaultDataOptions): Observable<DataSet<T>> {
|
||||
let getItemsDataSetError:Error = new Error(`Failed to get ${this.entity.pluralName}.`);
|
||||
|
||||
return this.dataStore.get(`${this.entity.endpoint}/${this.entity.allItemsEndpoint || ''}`, options, this.baseUrl)
|
||||
.map((rawDataSet: any) => {
|
||||
const allItemsProperty = this.entity.allItemsProperty || this.config.allItemsProperty;
|
||||
|
@ -237,13 +259,16 @@ export class Repository<T extends EntityModelBase> implements IRepository {
|
|||
count: dataSet.count,
|
||||
items: items
|
||||
});
|
||||
})
|
||||
}).catch((error:Error) => {
|
||||
getItemsDataSetError.message = getItemsDataSetError.message + " Error: " + error.message;
|
||||
throw getItemsDataSetError;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getItemById(itemId: string | number, options:DataOptions = defaultDataOptions): Observable<T> {
|
||||
if (this.entity.values){
|
||||
const entityValue:T = this.entity.getValueById(itemId);
|
||||
getItemById(itemId: string | number, options: DataOptions = defaultDataOptions): Observable<T> {
|
||||
if (this.entity.values) {
|
||||
const entityValue: T = this.entity.getValueById(itemId);
|
||||
if (entityValue)
|
||||
return Observable.of(entityValue);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче