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

Commit fad7978

Browse files
committed
fix(createEntity): Adds id option for conflict errors.
BREAKING CHANGE: createEntity requires id option.
1 parent 5f5c7ab commit fad7978

File tree

13 files changed

+36
-34
lines changed

13 files changed

+36
-34
lines changed

docs/functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ const { count } = await facade.countEntities({
2424
This package contains the [count entities tests](../src/tests/countEntities) and the [count entities signature](../src/signatures/CountEntities.ts) for this function.
2525

2626
### createEntity
27-
Creates a new entity using the `entity` option.
27+
Creates a new entity using the `entity` option if no entity exists that matches the `id` option.
2828

2929
```ts
3030
const { entity } = await facade.createEntity({
31+
id: { id: 'example_id' },
3132
entity: { id: 'example_id', foo: 'bar' },
3233
});
3334
```

src/Facade.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UpsertEntity from './signatures/UpsertEntity';
1010

1111
export default interface Facade<Id, Entity> {
1212
readonly getEntity: GetEntity<Id, Entity>;
13-
readonly createEntity: CreateEntity<Entity>;
13+
readonly createEntity: CreateEntity<Id, Entity>;
1414
readonly overwriteEntity: OverwriteEntity<Id, Entity>;
1515
readonly patchEntity: PatchEntity<Id, Entity>;
1616
readonly removeEntity: RemoveEntity<Id>;

src/signatures/CreateEntity.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
export interface Opts<Entity> {
1+
export interface Opts<Id, Entity> {
2+
readonly id: Id;
23
readonly entity: Entity;
34
}
45

56
export interface Result<Entity> {
67
readonly entity: Entity;
78
}
89

9-
export type Signature<Entity> = (opts: Opts<Entity>) => Promise<Result<Entity>>;
10+
export type Signature<Id, Entity> = (opts: Opts<Id, Entity>) => Promise<Result<Entity>>;
1011

1112
export default Signature;

src/signatures/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import RemoveEntity from './RemoveEntity';
99
import UpsertEntity from './UpsertEntity';
1010

1111
export type CountEntitiesSignature<Entity> = CountEntities<Entity>;
12-
export type CreateEntitySignature<Entity> = CreateEntity<Entity>;
12+
export type CreateEntitySignature<Id, Entity> = CreateEntity<Id, Entity>;
1313
export type GetEntitiesSignature<Entity> = GetEntities<Entity>;
1414
export type GetEntitySignature<Id, Entity> = GetEntity<Id, Entity>;
1515
export type OverwriteEntitySignature<Id, Entity> = OverwriteEntity<Id, Entity>;

src/tests/createEntity/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import * as assertRejects from 'assert-rejects';
22
import 'mocha'; // tslint:disable-line:no-import-side-effect
33
import ConflictingEntityError from '../../errors/ConflictingEntityError';
44
import Facade from '../../Facade';
5-
import { TestEntity, testEntity, TestId } from '../utils/testEntity';
5+
import { TestEntity, testEntity, testId, TestId } from '../utils/testEntity';
66

77
export default (facade: Facade<TestId, TestEntity>) => {
88
describe('createEntity', () => {
99
it('should not error when identifier does not exist', async () => {
10-
await facade.createEntity({ entity: testEntity });
10+
await facade.createEntity({ id: testId, entity: testEntity });
1111
});
1212

1313
it('should error when identifier does exist', async () => {
14-
await facade.createEntity({ entity: testEntity });
15-
const promise = facade.createEntity({ entity: testEntity });
14+
await facade.createEntity({ id: testId, entity: testEntity });
15+
const promise = facade.createEntity({ id: testId, entity: testEntity });
1616
await assertRejects(promise, ConflictingEntityError);
1717
});
1818
});

src/tests/getEntities/paginationTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Cursor from '../../types/Cursor';
55
import Filter from '../../types/Filter';
66
import Pagination from '../../types/Pagination';
77
import Sort from '../../types/Sort';
8-
import { TestEntity, testEntity, TestId } from '../utils/testEntity';
8+
import { TestEntity, testEntity, testId, TestId } from '../utils/testEntity';
99

1010
export default (facade: Facade<TestId, TestEntity>) => {
1111
const firstEntity = { ...testEntity, id: 'first_entity_id' };
@@ -14,8 +14,8 @@ export default (facade: Facade<TestId, TestEntity>) => {
1414
const filter: Filter<TestEntity> = {};
1515

1616
const createTestEntities = async () => {
17-
await facade.createEntity({ entity: firstEntity });
18-
await facade.createEntity({ entity: secondEntity });
17+
await facade.createEntity({ id: testId, entity: firstEntity });
18+
await facade.createEntity({ id: testId, entity: secondEntity });
1919
};
2020

2121
const paginate = (cursor: Cursor, forward: boolean) => {

src/tests/getEntities/sortTest.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Facade from '../../Facade';
44
import Filter from '../../types/Filter';
55
import Pagination from '../../types/Pagination';
66
import Sort from '../../types/Sort';
7-
import { TestEntity, testEntity, TestId } from '../utils/testEntity';
7+
import { TestEntity, testEntity, TestId, testId } from '../utils/testEntity';
88

99
export default (facade: Facade<TestId, TestEntity>) => {
1010
const firstEntity = { ...testEntity, stringProp: 'a', numberProp: 1 };
@@ -21,38 +21,38 @@ export default (facade: Facade<TestId, TestEntity>) => {
2121
};
2222

2323
it('should sort by one ascending property when entities are ordered', async () => {
24-
await facade.createEntity({ entity: firstEntity });
25-
await facade.createEntity({ entity: secondEntity });
24+
await facade.createEntity({ id: testId, entity: firstEntity });
25+
await facade.createEntity({ id: testId, entity: secondEntity });
2626
await assertSort([firstEntity, secondEntity], { stringProp: true });
2727
});
2828

2929
it('should sort by one ascending property when entities are unordered', async () => {
30-
await facade.createEntity({ entity: secondEntity });
31-
await facade.createEntity({ entity: firstEntity });
30+
await facade.createEntity({ id: testId, entity: secondEntity });
31+
await facade.createEntity({ id: testId, entity: firstEntity });
3232
await assertSort([firstEntity, secondEntity], { stringProp: true });
3333
});
3434

3535
it('should sort by one descending property when entities are ordered', async () => {
36-
await facade.createEntity({ entity: secondEntity });
37-
await facade.createEntity({ entity: firstEntity });
36+
await facade.createEntity({ id: testId, entity: secondEntity });
37+
await facade.createEntity({ id: testId, entity: firstEntity });
3838
await assertSort([secondEntity, firstEntity], { stringProp: false });
3939
});
4040

4141
it('should sort by one descending property when entities are unordered', async () => {
42-
await facade.createEntity({ entity: firstEntity });
43-
await facade.createEntity({ entity: secondEntity });
42+
await facade.createEntity({ id: testId, entity: firstEntity });
43+
await facade.createEntity({ id: testId, entity: secondEntity });
4444
await assertSort([secondEntity, firstEntity], { stringProp: false });
4545
});
4646

4747
it('should sort by two properties when ascending first and descending second', async () => {
48-
await facade.createEntity({ entity: firstEntity });
49-
await facade.createEntity({ entity: secondEntity });
48+
await facade.createEntity({ id: testId, entity: firstEntity });
49+
await facade.createEntity({ id: testId, entity: secondEntity });
5050
await assertSort([secondEntity, firstEntity], { stringProp: true, numberProp: false });
5151
});
5252

5353
it('should sort by two properties when descending first and ascending second', async () => {
54-
await facade.createEntity({ entity: firstEntity });
55-
await facade.createEntity({ entity: secondEntity });
54+
await facade.createEntity({ id: testId, entity: firstEntity });
55+
await facade.createEntity({ id: testId, entity: secondEntity });
5656
await assertSort([secondEntity, firstEntity], { stringProp: false, numberProp: true });
5757
});
5858
};

src/tests/getEntity/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
1313
});
1414

1515
it('should not error when identifier does exist', async () => {
16-
await facade.createEntity({ entity: testEntity });
16+
await facade.createEntity({ id: testId, entity: testEntity });
1717
const retrievedEntity = await facade.getEntity({ id: testId });
1818
assert.deepEqual(retrievedEntity, testEntity);
1919
});

src/tests/overwriteEntity/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
1919
numberProp: 2,
2020
stringProp: 'test_string_prop_overwrite',
2121
};
22-
await facade.createEntity({ entity: testEntity });
22+
await facade.createEntity({ id: testId, entity: testEntity });
2323
const overwrittenEntity = await facade.overwriteEntity({ id: testId, entity: testOverwrite });
2424
const retrievedEntity = await facade.getEntity({ id: testId });
2525
assert.deepEqual(overwrittenEntity, testOverwrite);

src/tests/patchEntity/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
2424
const testPatch: Partial<TestEntity> = {
2525
stringProp: 'test_string_prop_patch',
2626
};
27-
await facade.createEntity({ entity: testEntity });
27+
await facade.createEntity({ id: testId, entity: testEntity });
2828
await assertPatch(testPatch);
2929
});
3030

@@ -34,7 +34,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
3434
numberProp: 2,
3535
stringProp: 'test_string_prop_patch',
3636
};
37-
await facade.createEntity({ entity: testEntity });
37+
await facade.createEntity({ id: testId, entity: testEntity });
3838
await assertPatch(testPatch);
3939
});
4040
});

src/tests/removeEntity/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
1212
});
1313

1414
it('should not error when identifier does exist', async () => {
15-
await facade.createEntity({ entity: testEntity });
15+
await facade.createEntity({ id: testId, entity: testEntity });
1616
await facade.removeEntity({ id: testId });
1717
const promise = facade.getEntity({ id: testId });
1818
await assertRejects(promise, MissingEntityError);

src/tests/upsertEntity/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default (facade: Facade<TestId, TestEntity>) => {
1919
numberProp: 2,
2020
stringProp: 'test_string_prop_overwrite',
2121
};
22-
await facade.createEntity({ entity: testEntity });
22+
await facade.createEntity({ id: testId, entity: testEntity });
2323
const overwrittenEntity = await facade.upsertEntity({ id: testId, entity: testOverwrite });
2424
const retrievedEntity = await facade.getEntity({ id: testId });
2525
assert.deepEqual(overwrittenEntity, testOverwrite);

src/tests/utils/filterTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'mocha'; // tslint:disable-line:no-import-side-effect
22
import Facade from '../../Facade';
33
import Filter from '../../types/Filter';
4-
import { TestEntity, testEntity, TestId } from '../utils/testEntity';
4+
import { TestEntity, testEntity, TestId, testId } from '../utils/testEntity';
55

66
export type FilterAsserter = (filter: Filter<TestEntity>) => Promise<void>;
77

@@ -16,8 +16,8 @@ export const secondEntity = { ...testEntity, stringProp: 'b', numberProp: 2 };
1616

1717
export default (opts: Opts) => {
1818
const createTestEntities = async () => {
19-
await opts.facade.createEntity({ entity: firstEntity });
20-
await opts.facade.createEntity({ entity: secondEntity });
19+
await opts.facade.createEntity({ id: testId, entity: firstEntity });
20+
await opts.facade.createEntity({ id: testId, entity: secondEntity });
2121
};
2222

2323
it('should not filter when using no filter', async () => {

0 commit comments

Comments
 (0)