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

Commit ac443ff

Browse files
authored
fix(deps): Updates core to 6.0.2. (#7)
BREAKING CHANGE: The `filter`, `sort`, and `paginate` options are now optional. BREAKING CHANGE: The `upsertEntity` function has been removed due to filter issues. BREAKING CHANGE: The `overwriteEntity` function was renamed to `replaceEntity`. BREAKING CHANGE: Removes Id interface from all functions. BREAKING CHANGE: The `db` property in the factory config allows for reconnections.
1 parent edc68fc commit ac443ff

22 files changed

+1175
-1323
lines changed

package-lock.json

Lines changed: 962 additions & 1187 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"check-coverage": true
2626
},
2727
"dependencies": {
28-
"@js-entity-repos/core": "^4.1.2",
28+
"@js-entity-repos/core": "^6.0.2",
2929
"knex": "^0.14.2",
3030
"lodash": "^4.17.4"
3131
},

readme.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,45 @@
44
### Usage
55
1. Install it with `npm i @js-entity-repos/knex`.
66
1. For each entity you will need to do the following.
7-
1. [Create Id and Entity interfaces](#id-and-entity-interface).
8-
1. [Create a facade config](#facade-config).
9-
1. [Construct the facade with the config and interfaces](#calling-the-facade).
7+
1. [Create an Entity interfaces](#entity-interface).
8+
1. [Create a factory config](#factory-config).
9+
1. [Construct the facade](#construct-the-facade).
1010
1. [Use the facade](https://github.com/js-entity-repos/core/blob/master/docs/facade.md).
1111

12-
### Id and Entity Interface
12+
### Entity Interface
1313

1414
```ts
15-
export interface TodoId {
16-
readonly id: string;
17-
}
15+
import Entity from '@js-entity-repos/core/dist/types/Entity';
1816

19-
export interface TodoEntity extends TodoId {
17+
export interface TodoEntity extends Entity {
2018
readonly description: string;
2119
readonly completed: boolean;
2220
}
2321
```
2422

25-
### Facade Config
23+
### Factory Config
2624

2725
```ts
28-
import FacadeConfig from '@js-entity-repos/knex/dist/Config';
26+
import FactoryConfig from '@js-entity-repos/knex/dist/FactoryConfig';
2927
import connectToDb from '@js-entity-repos/knex/dist/utils/connectToDb';
3028

31-
const todoFacadeConfig: FacadeConfig = {
32-
constructDocument: (id, patch) => {
33-
// Converts an entity to a document for the database.
34-
return { ...patch, ...id }
29+
const todoFactoryConfig: FactoryConfig<TodoEntity> = {
30+
constructDocument: (patch) => {
31+
// Optional property that converts an entity to a document for the database.
32+
return patch;
3533
},
3634
constructEntity: (document) => {
37-
// Converts a document from the database to an entity.
35+
// Optional property that converts a document from the database to an entity.
3836
return document;
3937
},
38+
constructFilter: (filter) => {
39+
// Optional property that converts an entity filter to a filter for the DB.
40+
return filter;
41+
},
42+
constructSort: (sort) => {
43+
// Optional property that converts an entity sort to a sort for the DB.
44+
return sort;
45+
}.
4046
db: connectToDb({
4147
client: 'mysql',
4248
connection: {
@@ -46,6 +52,7 @@ const todoFacadeConfig: FacadeConfig = {
4652
user: 'todouser',
4753
},
4854
}),
55+
defaultPaginationLimit: 100, // Optional property.
4956
entityName: 'todo',
5057
tableName: 'todos',
5158
};
@@ -54,7 +61,7 @@ const todoFacadeConfig: FacadeConfig = {
5461
### Construct the Facade
5562

5663
```ts
57-
import facade from '@js-entity-repos/knex/dist/facade';
64+
import factory from '@js-entity-repos/knex/dist/factory';
5865

59-
const todosFacade = facade<TodoId, TodoEntity>(todoFacadeConfig);
66+
const todosFacade = factory(todoFactoryConfig);
6067
```

src/Config.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/FacadeConfig.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import Filter from '@js-entity-repos/core/dist/types/Filter';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
4+
import * as knex from 'knex';
5+
6+
export type Document = any;
7+
8+
export default interface FacadeConfig<E extends Entity> {
9+
readonly constructDocument: (patch: Partial<E>) => Document;
10+
readonly constructEntity: (document: Document) => E;
11+
readonly constructFilter: (filter: Filter<E>) => any;
12+
readonly constructSort: (sort: Sort<E>) => any;
13+
readonly db: () => Promise<knex>;
14+
readonly defaultPaginationLimit: number;
15+
readonly entityName: string;
16+
readonly tableName: string;
17+
}

src/FactoryConfig.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import Filter from '@js-entity-repos/core/dist/types/Filter';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
4+
import * as knex from 'knex';
5+
6+
export type Document = any;
7+
8+
export default interface FacadeConfig<E extends Entity> {
9+
readonly constructDocument?: (patch: Partial<E>) => Document;
10+
readonly constructEntity?: (document: Document) => E;
11+
readonly constructFilter?: (filter: Filter<E>) => any;
12+
readonly constructSort?: (sort: Sort<E>) => any;
13+
readonly db: () => Promise<knex>;
14+
readonly defaultPaginationLimit?: number;
15+
readonly entityName: string;
16+
readonly tableName: string;
17+
}

src/facade.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/facade.test.ts renamed to src/factory.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import facadeTest from '@js-entity-repos/core/dist/tests';
2-
import { TestEntity, TestId } from '@js-entity-repos/core/dist/tests/utils/testEntity';
2+
import { TestEntity } from '@js-entity-repos/core/dist/tests/utils/testEntity';
33
import { config } from 'dotenv';
44
import 'mocha'; // tslint:disable-line:no-import-side-effect
5-
import facade from './facade';
5+
import factory from './factory';
66
import connectToDb from './utils/connectToDb';
77
config();
88

@@ -21,18 +21,17 @@ const db = connectToDb({
2121
const tableName = 'testentities';
2222

2323
before(async () => {
24-
await Promise.resolve(db.schema.dropTableIfExists(tableName));
25-
await Promise.resolve(db.schema.createTableIfNotExists(tableName, async (table) => {
24+
const schema = (await db()).schema;
25+
await Promise.resolve(schema.dropTableIfExists(tableName));
26+
await Promise.resolve(schema.createTableIfNotExists(tableName, async (table) => {
2627
table.string('id').unique();
2728
table.string('stringProp');
2829
table.float('numberProp');
2930
table.boolean('booleanProp');
3031
}));
3132
});
3233

33-
facadeTest(facade<TestId, TestEntity>({
34-
constructDocument: (id, patch) => ({ ...patch, ...id }),
35-
constructEntity: (document) => document,
34+
facadeTest(factory<TestEntity>({
3635
db,
3736
entityName: 'Test Entity',
3837
tableName,

src/factory.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Facade from '@js-entity-repos/core/dist/Facade';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from './FacadeConfig';
4+
import FactoryConfig from './FactoryConfig';
5+
import countEntities from './functions/countEntities';
6+
import createEntity from './functions/createEntity';
7+
import getEntities from './functions/getEntities';
8+
import getEntity from './functions/getEntity';
9+
import patchEntity from './functions/patchEntity';
10+
import removeEntities from './functions/removeEntities';
11+
import removeEntity from './functions/removeEntity';
12+
import replaceEntity from './functions/replaceEntity';
13+
14+
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> => {
15+
const facadeConfig: FacadeConfig<E> = {
16+
constructDocument: (patch) => patch,
17+
constructEntity: (document) => document,
18+
constructFilter: (filter) => filter,
19+
constructSort: (sort) => sort,
20+
defaultPaginationLimit: 100,
21+
...factoryConfig,
22+
};
23+
return {
24+
countEntities: countEntities<E>(facadeConfig),
25+
createEntity: createEntity<E>(facadeConfig),
26+
getEntities: getEntities<E>(facadeConfig),
27+
getEntity: getEntity<E>(facadeConfig),
28+
patchEntity: patchEntity<E>(facadeConfig),
29+
removeEntities: removeEntities<E>(facadeConfig),
30+
removeEntity: removeEntity<E>(facadeConfig),
31+
replaceEntity: replaceEntity<E>(facadeConfig),
32+
};
33+
};

src/functions/countEntities.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import CountEntities from '@js-entity-repos/core/dist/signatures/CountEntities';
2-
import Config from '../Config';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from '../FacadeConfig';
34
import filterEntities from '../utils/filterEntities';
45

5-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): CountEntities<Entity> => {
6-
return async ({ filter }) => {
7-
const table = config.db.table(config.tableName);
6+
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
7+
return async ({ filter = {} }) => {
8+
const table = (await config.db()).table(config.tableName);
89
const [result] = await Promise.resolve(filterEntities(table, filter).count());
910
return { count: result['count(*)'] };
1011
};

src/functions/createEntity.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import ConflictingEntityError from '@js-entity-repos/core/dist/errors/ConflictingEntityError';
22
import CreateEntity from '@js-entity-repos/core/dist/signatures/CreateEntity';
3-
import Config from '../Config';
3+
import Entity from '@js-entity-repos/core/dist/types/Entity';
4+
import FacadeConfig from '../FacadeConfig';
45

56
const conflictErrorCode = 1062;
67

7-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): CreateEntity<Id, Entity> => {
8+
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
89
return async ({ id, entity }) => {
9-
const table = config.db.table(config.tableName);
10-
const document = config.constructDocument(id, entity);
10+
const table = (await config.db()).table(config.tableName);
11+
const document = config.constructDocument({ ...entity as any, id });
1112
try {
1213
await Promise.resolve(table.insert(document));
1314
} catch (err) {

src/functions/getEntities.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import GetEntities from '@js-entity-repos/core/dist/signatures/GetEntities';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
24
import createCursorFromEntity from '@js-entity-repos/core/dist/utils/createCursorFromEntity';
35
import createPaginationFilter from '@js-entity-repos/core/dist/utils/createPaginationFilter';
46
import { first, last, mapValues } from 'lodash';
5-
import Config from '../Config';
7+
import FacadeConfig from '../FacadeConfig';
68
import filterEntities from '../utils/filterEntities';
79

810
const xor = (conditionA: boolean, conditionB: boolean) => {
911
return (conditionA && !conditionB) || (!conditionA && conditionB);
1012
};
1113

12-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): GetEntities<Entity> => {
13-
return async ({ filter, sort, pagination }) => {
14-
const table = config.db.table(config.tableName);
14+
export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
15+
const defaultPagination = {
16+
cursor: undefined,
17+
forward: true,
18+
limit: config.defaultPaginationLimit,
19+
};
20+
const defaultSort = { id: true } as Sort<E>;
21+
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
22+
const table = (await config.db()).table(config.tableName);
1523

1624
const paginationFilter = createPaginationFilter(pagination, sort);
1725
const fullFilter = { $and: [filter, paginationFilter] };
18-
const knexSort = mapValues(sort, (sortValue: boolean) => {
26+
const constructedFilter = config.constructFilter(fullFilter);
27+
const constructedSort = config.constructSort(sort);
28+
const knexSort = mapValues(constructedSort, (sortValue): string => {
1929
return !xor(pagination.forward, sortValue) ? 'asc' : 'desc';
2030
});
2131

22-
const filterQuery = filterEntities(table, fullFilter);
32+
const filterQuery = filterEntities(table, constructedFilter);
2333
const sortQuery = Object.keys(knexSort).reduce((result, sortKey) => {
2434
return result.orderBy(sortKey, (knexSort as any)[sortKey]);
2535
}, filterQuery);

src/functions/getEntity.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';
22
import GetEntity from '@js-entity-repos/core/dist/signatures/GetEntity';
3-
import Config from '../Config';
3+
import Entity from '@js-entity-repos/core/dist/types/Entity';
4+
import FacadeConfig from '../FacadeConfig';
5+
import constructIdFilter from '../utils/constructIdFilter';
46
import filterEntities from '../utils/filterEntities';
57

6-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): GetEntity<Id, Entity> => {
7-
return async ({ id }) => {
8-
const table = config.db.table(config.tableName);
9-
const document = await Promise.resolve(filterEntities(table, id).first());
8+
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
9+
return async ({ id, filter = {} }) => {
10+
const table = (await config.db()).table(config.tableName);
11+
const constructedFilter = constructIdFilter({ id, filter, config });
12+
const document = await Promise.resolve(filterEntities(table, constructedFilter).first());
1013

1114
if (document === undefined || document === null) {
1215
throw new MissingEntityError(config.entityName, id);

src/functions/overwriteEntity.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/functions/patchEntity.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import PatchEntity from '@js-entity-repos/core/dist/signatures/PatchEntity';
2-
import Config from '../Config';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from '../FacadeConfig';
4+
import constructIdFilter from '../utils/constructIdFilter';
35
import filterEntities from '../utils/filterEntities';
46
import getEntity from './getEntity';
57

6-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): PatchEntity<Id, Entity> => {
7-
return async ({ id, patch }) => {
8-
const table = config.db.table(config.tableName);
9-
await Promise.resolve(filterEntities(table, id).update(patch));
10-
return getEntity<Id, Entity>(config)({ id });
8+
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
9+
return async ({ id, patch, filter = {} }) => {
10+
const table = (await config.db()).table(config.tableName);
11+
const document = config.constructDocument({ ...patch as any, id });
12+
const constructedFilter = constructIdFilter({ id, filter, config });
13+
await Promise.resolve(filterEntities(table, constructedFilter).update(document));
14+
return getEntity<E>(config)({ id, filter });
1115
};
1216
};

src/functions/removeEntities.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import RemoveEntities from '@js-entity-repos/core/dist/signatures/RemoveEntities';
2-
import Config from '../Config';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from '../FacadeConfig';
34
import filterEntities from '../utils/filterEntities';
45

5-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): RemoveEntities<Entity> => {
6-
return async ({ filter }) => {
7-
const table = config.db.table(config.tableName);
8-
await Promise.resolve(filterEntities(table, filter).delete());
6+
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
7+
return async ({ filter = {} }) => {
8+
const table = (await config.db()).table(config.tableName);
9+
const constructedFilter = config.constructFilter(filter);
10+
await Promise.resolve(filterEntities(table, constructedFilter).delete());
911
};
1012
};

0 commit comments

Comments
 (0)