Skip to content

Commit 1cfae1a

Browse files
authored
Merge pull request #19 from alphamanuscript/add-partial-update
Adds partial update of entities
2 parents 9314d96 + 15a4004 commit 1cfae1a

File tree

5 files changed

+72
-16
lines changed

5 files changed

+72
-16
lines changed

packages/client/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ The entity above would be converted to:
7070
tags: []
7171
}
7272
```
73+
Dates are expanded as `text` for now and will correctly be expanded to `datetime` in future versions of `@surix/data-helpers`. Arrays are not supported for now.
74+
75+
76+
#### `project.entities.patch(entity: Entity)`
77+
78+
Updates a entity partially. I.e adds onto the already existing entity identified by the `_id` field.
79+
80+
```javascript
81+
const entity = {
82+
_id: 'someid',
83+
data: {
84+
phone: '0712345678'
85+
},
86+
tags: ['tag']
87+
}
88+
89+
const updatedEntity = await project.entities.patch(entity);
90+
```
91+
7392
Dates created with the `Date()` function are expanded as `text`. and those created with `new Date()` are correctly expanded to `datetime`.
7493

7594
#### `project.entities.put(entity: Entity)`

packages/client/src/project/entities.ts

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export function getProjectEntities (projectId: string, apiClient: AxiosInstance)
1313
const entities = res.data;
1414
return wrapEntityArray(entities);
1515
},
16+
async update(entity: Entity): Promise<WrappedEntity> {
17+
const _entity = { ...entity };
18+
const entityId = _entity._id;
19+
delete _entity._id;
20+
const expandedEntity = expandEntity(_entity);
21+
const res = await apiClient.patch<ApiEntity>(
22+
`/projects/${projectId}/entities/${entityId}`, expandedEntity);
23+
const returnedEntity = res.data;
24+
return wrapEntity(returnedEntity);
25+
},
1626
async put(entity: Entity): Promise<WrappedEntity> {
1727
const _entity = { ...entity }; // Make a shallow copy
1828
const entityId = _entity._id;

packages/client/src/project/tests/entities.test.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('Project Entities', () => {
8989
name: 'someone 1'
9090
},
9191
tags: []
92-
}
92+
};
9393
let apiClient: AxiosInstance;
9494
async function callMockCreate (entity: any): Promise<dataHelpers.WrappedEntity> {
9595
apiClient = api.getApiClient('http://baseurl', 'somekey');
@@ -102,7 +102,7 @@ describe('Project Entities', () => {
102102
}
103103
it('should call POST /projects/:pid/entities endpoint with entity', async () => {
104104
const entity = userEntity;
105-
const expectedEntity = dataHelpers.expandEntity(entity)
105+
const expectedEntity = dataHelpers.expandEntity(entity);
106106
await callMockCreate(entity);
107107
expect(apiClient.post).toHaveBeenCalledWith('/projects/project1/entities', expectedEntity);
108108
});
@@ -113,32 +113,58 @@ describe('Project Entities', () => {
113113
expect(entity).toMatchSnapshot();
114114
});
115115
});
116-
describe('put', () => {
116+
describe('patch', () => {
117117
const userEntity = {
118118
data: {
119119
name: 'someone 1'
120120
},
121121
tags: []
122-
}
123-
let apiClient: AxiosInstance;
124-
async function callMockPut (entity: any): Promise<dataHelpers.WrappedEntity> {
122+
};
123+
async function callMockUpdate (entity: any): Promise<dataHelpers.WrappedEntity> {
125124
apiClient = api.getApiClient('http://baseurl', 'somekey');
126-
jest.spyOn(apiClient, 'put').mockReturnValue(Promise.resolve({ data:
125+
jest.spyOn(apiClient, 'patch').mockReturnValue(Promise.resolve({ data:
127126
{ entity } }));
128127
jest.spyOn(dataHelpers, 'wrapEntity');
129128
const project = getProjectApi('project1', apiClient);
130-
const ent = await project.entities.put(entity);
129+
const ent = await project.entities.update(entity);
131130
return ent;
132131
}
133-
it('should call PUT /projects/:pid/entities/:eid endpoint with entity', async () => {
132+
it('should call PATCH /projects/:pid/entities/:eid endpoint with entity', async () => {
134133
const entity = { ...userEntity };
135-
entity['_id'] = 'entity1';
136-
const expectedEntity = dataHelpers.expandEntity(entity);
137-
const frozen = Object.freeze(entity);
138-
await callMockPut(frozen);
139-
expect(apiClient.put).toHaveBeenCalledWith(
140-
'/projects/project1/entities/entity1', expectedEntity);
134+
entity['_id'] = 'entity1';
135+
const expectedEntity = dataHelpers.expandEntity(entity);
136+
const frozen = Object.freeze(entity);
137+
await callMockUpdate(frozen);
138+
expect(apiClient.patch).toHaveBeenCalledWith(
139+
'/projects/project1/entities/entity1', expectedEntity);
141140
});
142141
});
142+
describe('put', () => {
143+
const userEntity = {
144+
data: {
145+
name: 'someone 1'
146+
},
147+
tags: []
148+
};
149+
let apiClient: AxiosInstance;
150+
async function callMockPut (entity: any): Promise<dataHelpers.WrappedEntity> {
151+
apiClient = api.getApiClient('http://baseurl', 'somekey');
152+
jest.spyOn(apiClient, 'put').mockReturnValue(Promise.resolve({ data:
153+
{ entity } }));
154+
jest.spyOn(dataHelpers, 'wrapEntity');
155+
const project = getProjectApi('project1', apiClient);
156+
const ent = await project.entities.put(entity);
157+
return ent;
158+
}
159+
it('should call PUT /projects/:pid/entities/:eid endpoint with entity', async () => {
160+
const entity = { ...userEntity };
161+
entity['_id'] = 'entity1';
162+
const expectedEntity = dataHelpers.expandEntity(entity);
163+
const frozen = Object.freeze(entity);
164+
await callMockPut(frozen);
165+
expect(apiClient.put).toHaveBeenCalledWith(
166+
'/projects/project1/entities/entity1', expectedEntity);
167+
});
168+
});
143169
});
144170
});

packages/client/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ProjectEntities {
1010
get(id: string): Promise<WrappedEntity>;
1111
query(query?: Query): Promise<WrappedEntity[]>;
1212
create(entity: Entity): Promise<WrappedEntity>;
13+
update(entity: Entity): Promise<WrappedEntity>;
1314
put(entity: Entity): Promise<WrappedEntity>;
1415
}
1516

packages/data-helpers/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const {
3737
} = require('@surix/data-helpers')
3838
```
3939

40-
### `expnadEntity`
40+
### `expandEntity`
4141

4242
Converts a user friendly version of an entity to the raw version of the entity.
4343
*Note: For now, dates are interpreted `text` type but will be updated to `datatime` in future*

0 commit comments

Comments
 (0)