Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit 14d827d

Browse files
committed
feat: Adds the upsert function.
1 parent 1e4e5bd commit 14d827d

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

docs/facade.md

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The facade contains common functions for storage and retrieval of entities from
1010
- [patchEntity](#patchentity)
1111
- [removeEntities](#removeentities)
1212
- [removeEntity](#removeentity)
13+
- [upsertEntity](#upsertentity)
1314

1415
The facade in this package is a TypeScript interface, but concrete implementations of the interface include:
1516

@@ -86,3 +87,11 @@ await facade.removeEntity({
8687
id: { id: 'example_id' },
8788
});
8889
```
90+
91+
### upsertEntity
92+
```ts
93+
await facade.upsertEntity({
94+
id: { id: 'example_id' },
95+
entity: { id: 'example_id', foo: 'bar' },
96+
});
97+
```

src/Facade.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import OverwriteEntity from './signatures/OverwriteEntity';
66
import PatchEntity from './signatures/PatchEntity';
77
import RemoveEntities from './signatures/RemoveEntities';
88
import RemoveEntity from './signatures/RemoveEntity';
9+
import UpsertEntity from './signatures/UpsertEntity';
910

1011
export default interface Facade<Id, Entity> {
1112
readonly getEntity: GetEntity<Id, Entity>;
@@ -16,4 +17,5 @@ export default interface Facade<Id, Entity> {
1617
readonly getEntities: GetEntities<Entity>;
1718
readonly countEntities: CountEntities<Entity>;
1819
readonly removeEntities: RemoveEntities<Entity>;
20+
readonly upsertEntity: UpsertEntity<Id, Entity>;
1921
}

src/signatures/UpsertEntity.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface Opts<Id, Entity> {
2+
readonly id: Id;
3+
readonly entity: Entity;
4+
}
5+
6+
export interface Result<Entity> {
7+
readonly entity: Entity;
8+
}
9+
10+
export type Signature<Id, Entity> = (opts: Opts<Id, Entity>) => Promise<Result<Entity>>;
11+
12+
export default Signature;

src/signatures/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import OverwriteEntity from './OverwriteEntity';
66
import PatchEntity from './PatchEntity';
77
import RemoveEntities from './RemoveEntities';
88
import RemoveEntity from './RemoveEntity';
9+
import UpsertEntity from './UpsertEntity';
910

1011
export type CountEntitiesSignature<Entity> = CountEntities<Entity>;
1112
export type CreateEntitySignature<Entity> = CreateEntity<Entity>;
@@ -15,3 +16,4 @@ export type OverwriteEntitySignature<Id, Entity> = OverwriteEntity<Id, Entity>;
1516
export type PatchEntitySignature<Id, Entity> = PatchEntity<Id, Entity>;
1617
export type RemoveEntitiesSignature<Entity> = RemoveEntities<Entity>;
1718
export type RemoveEntitySignature<Id> = RemoveEntity<Id>;
19+
export type UpsertEntitySignature<Id, Entity> = UpsertEntity<Id, Entity>;

src/tests/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import overwriteEntityTest from './overwriteEntity/test';
88
import patchEntityTest from './patchEntity/test';
99
import removeEntitiesTest from './removeEntities/test';
1010
import removeEntityTest from './removeEntity/test';
11+
import upsertEntityTest from './upsertEntity/test';
1112
import { TestEntity, TestId } from './utils/testEntity';
1213

1314
export const facadeTest = (facade: Facade<TestId, TestEntity>) => {
@@ -24,5 +25,6 @@ export const facadeTest = (facade: Facade<TestId, TestEntity>) => {
2425
countEntitiesTest(facade);
2526
getEntitiesTest(facade);
2627
removeEntitiesTest(facade);
28+
upsertEntityTest(facade);
2729
});
2830
};

src/tests/upsertEntity/test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'mocha'; // tslint:disable-line:no-import-side-effect
2+
import * as assert from 'power-assert';
3+
import Facade from '../../Facade';
4+
import { TestEntity, testEntity, testId, TestId } from '../utils/testEntity';
5+
6+
export default (facade: Facade<TestId, TestEntity>) => {
7+
describe('upsertEntity', () => {
8+
it('should create when identifier does not exist', async () => {
9+
const createdEntity = await facade.upsertEntity({ id: testId, entity: testEntity });
10+
const retrievedEntity = await facade.getEntity({ id: testId });
11+
assert.deepEqual(createdEntity, testEntity);
12+
assert.deepEqual(retrievedEntity, testEntity);
13+
});
14+
15+
it('should overwrite when identifier does exist', async () => {
16+
const testOverwrite: TestEntity = {
17+
...testId,
18+
booleanProp: false,
19+
numberProp: 2,
20+
stringProp: 'test_string_prop_overwrite',
21+
};
22+
await facade.createEntity({ entity: testEntity });
23+
const overwrittenEntity = await facade.upsertEntity({ id: testId, entity: testOverwrite });
24+
const retrievedEntity = await facade.getEntity({ id: testId });
25+
assert.deepEqual(overwrittenEntity, testOverwrite);
26+
assert.deepEqual(retrievedEntity, testOverwrite);
27+
});
28+
});
29+
};

0 commit comments

Comments
 (0)