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

Commit d5c7247

Browse files
committed
perf(replaceEntity): Removes extraneous db query.
1 parent ac443ff commit d5c7247

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const tableName = 'testentities';
2323
before(async () => {
2424
const schema = (await db()).schema;
2525
await Promise.resolve(schema.dropTableIfExists(tableName));
26-
await Promise.resolve(schema.createTableIfNotExists(tableName, async (table) => {
26+
await Promise.resolve(schema.createTable(tableName, async (table) => {
2727
table.string('id').unique();
2828
table.string('stringProp');
2929
table.float('numberProp');

src/functions/replaceEntity.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';
12
import ReplaceEntity from '@js-entity-repos/core/dist/signatures/ReplaceEntity';
23
import Entity from '@js-entity-repos/core/dist/types/Entity';
34
import FacadeConfig from '../FacadeConfig';
45
import constructIdFilter from '../utils/constructIdFilter';
56
import filterEntities from '../utils/filterEntities';
6-
import getEntity from './getEntity';
77

88
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
99
return async ({ id, entity, filter = {} }) => {
1010
const table = (await config.db()).table(config.tableName);
1111
const document = config.constructDocument({ ...entity as any, id });
1212
const constructedFilter = constructIdFilter({ id, filter, config });
13-
await Promise.resolve(filterEntities(table, constructedFilter).update(document));
14-
return getEntity<E>(config)({ id, filter });
13+
const res = await Promise.resolve(filterEntities(table, constructedFilter).update(document));
14+
if (!res) {
15+
throw new MissingEntityError(config.entityName, id);
16+
}
17+
return { entity };
1518
};
1619
};

0 commit comments

Comments
 (0)