From 899a5e48bf7dba67e60922dac94a107cf6029736 Mon Sep 17 00:00:00 2001 From: Yossi Kolesnicov Date: Tue, 23 Oct 2018 13:19:56 +0300 Subject: [PATCH] Added parseData to related items. --- jest-setup.ts | 2 +- lib/api/repository/readonly-repository.ts | 1 + package.json | 2 +- test/api/relationship-repository.spec.ts | 13 ++++++++++++- test/mock/todo-list.relationships.ts | 2 +- test/mock/todo-tag.relationships.ts | 17 +++++++++++++++++ 6 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/mock/todo-tag.relationships.ts diff --git a/jest-setup.ts b/jest-setup.ts index 6c53263..5cde065 100644 --- a/jest-setup.ts +++ b/jest-setup.ts @@ -1,3 +1,3 @@ import 'reflect-metadata'; -global['Reflect'] = Reflect; +(global)['Reflect'] = Reflect; diff --git a/lib/api/repository/readonly-repository.ts b/lib/api/repository/readonly-repository.ts index 3947001..a39dcb4 100644 --- a/lib/api/repository/readonly-repository.ts +++ b/lib/api/repository/readonly-repository.ts @@ -204,6 +204,7 @@ export class ReadonlyRepository imple this.emitEntityHttpErrorEvent(err); throw err }), + map(data => this.entityBackendConfig.parseData ? this.entityBackendConfig.parseData(data) : data), mergeMap(data => this.createItem(data, dataOptions, query)) ); diff --git a/package.json b/package.json index bca802d..275df7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/paris", - "version": "1.3.4", + "version": "1.3.5", "description": "Library for the implementation of Domain Driven Design with TypeScript + RxJS", "repository": { "type": "git", diff --git a/test/api/relationship-repository.spec.ts b/test/api/relationship-repository.spec.ts index 357d058..3d94210 100644 --- a/test/api/relationship-repository.spec.ts +++ b/test/api/relationship-repository.spec.ts @@ -6,12 +6,15 @@ import {TodoList} from "../mock/todo-list.entity"; import {Observable} from "rxjs"; import {DataSet} from "../../lib/data_access/dataset"; import {RelationshipRepository} from "../../lib/api/repository/relationship-repository"; +import {Tag} from "../mock/tag.value-object"; +import {TodoTagRelationship} from "../mock/todo-tag.relationships"; describe('RelationshipRepository', () => { let paris: Paris, todoListItemsRepo:RelationshipRepository, getTodoListItems$:Observable>, - sourceTodoList:TodoList; + sourceTodoList:TodoList, + todoTagRepository:RelationshipRepository; setMockData({ items: [ @@ -33,6 +36,7 @@ describe('RelationshipRepository', () => { sourceTodoList = new TodoList({ id: 1 }); todoListItemsRepo = paris.getRelationshipRepository(TodoListItemsRelationship); getTodoListItems$ = paris.queryForItem(TodoListItemsRelationship, sourceTodoList); + todoTagRepository = paris.getRelationshipRepository(TodoTagRelationship); }); it('should return a DataSet of Todo for the TodoList', done => { @@ -48,4 +52,11 @@ describe('RelationshipRepository', () => { expect(todoListItemsRepo.getEndpointUrl({ where: { todoListId: 1 }})).toBe('/lists/1/items'); }); }); + + it('should parse data for a related item', done => { + todoTagRepository.getRelatedItem(new Todo({ id: 5 })).subscribe((tag) => { + expect(tag.color).toEqual('Purple'); + done(); + }); + }); }); diff --git a/test/mock/todo-list.relationships.ts b/test/mock/todo-list.relationships.ts index 2dfa2fa..be5c843 100644 --- a/test/mock/todo-list.relationships.ts +++ b/test/mock/todo-list.relationships.ts @@ -11,6 +11,6 @@ import {EntityRelationshipRepositoryType} from "../../lib/api/entity/entity-rela dataEntity: Todo, endpoint: (config: ParisConfig, query: DataQuery) => `lists/${(query.where).todoListId}/items`, foreignKey: 'listId', - allowedTypes: [RelationshipType.OneToMany], + allowedTypes: [RelationshipType.OneToMany] }) export class TodoListItemsRelationship implements EntityRelationshipRepositoryType {} diff --git a/test/mock/todo-tag.relationships.ts b/test/mock/todo-tag.relationships.ts new file mode 100644 index 0000000..470fd37 --- /dev/null +++ b/test/mock/todo-tag.relationships.ts @@ -0,0 +1,17 @@ +import {Todo} from "./todo.entity"; +import {EntityRelationship} from "../../lib/config/decorators/entity-relationship.decorator"; +import {ParisConfig} from "../../lib/config/paris-config"; +import {DataQuery} from "../../lib/data_access/data-query"; +import {RelationshipType} from "../../lib/config/relationship-type.enum"; +import {EntityRelationshipRepositoryType} from "../../lib/api/entity/entity-relationship-repository-type"; +import {Tag} from "./tag.value-object"; + +@EntityRelationship({ + sourceEntity: Todo, + dataEntity: Tag, + endpoint: (config: ParisConfig, query: DataQuery) => `todo/tag/${(query.where).todo}`, + foreignKey: 'todo', + allowedTypes: [RelationshipType.OneToOne], + parseData: data => ({ ...data, colorName: 'Purple' }) +}) +export class TodoTagRelationship implements EntityRelationshipRepositoryType {}