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

Commit 417c4b7

Browse files
authored
fix(deps): Updates to core v7. (#14)
1 parent a8cdbe2 commit 417c4b7

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

package-lock.json

Lines changed: 3 additions & 3 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": "^6.0.2",
28+
"@js-entity-repos/core": "^7.1.0",
2929
"knex": "^0.14.2",
3030
"lodash": "^4.17.4"
3131
},

readme.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
1. Install it with `npm i @js-entity-repos/knex`.
66
1. For each entity you will need to do the following.
77
1. [Create an Entity interface](#entity-interface).
8-
1. [Create a factory config](#factory-config).
98
1. [Construct the facade](#construct-the-facade).
109
1. [Use the facade](https://github.com/js-entity-repos/core/blob/master/docs/facade.md).
1110

@@ -20,27 +19,27 @@ export interface TodoEntity extends Entity {
2019
}
2120
```
2221

23-
### Factory Config
22+
### Construct the Facade
2423

2524
```ts
26-
import FactoryConfig from '@js-entity-repos/knex/dist/FactoryConfig';
25+
import factory from '@js-entity-repos/knex/dist/factory';
2726
import connectToDb from '@js-entity-repos/knex/dist/utils/connectToDb';
2827

29-
const todoFactoryConfig: FactoryConfig<TodoEntity> = {
28+
const todosFacade = factory<TodoEntity>({
29+
// Optional property to convert an entity to a DB document. Defaults to the function below.
3030
constructDocument: (patch) => {
31-
// Optional property that converts an entity to a document for the database.
3231
return patch;
3332
},
33+
// Optional property to convert a DB document to an entity. Defaults to the function below.
3434
constructEntity: (document) => {
35-
// Optional property that converts a document from the database to an entity.
3635
return document;
3736
},
37+
// Optional property to convert an entity filter to a DB filter. Defaults to the function below.
3838
constructFilter: (filter) => {
39-
// Optional property that converts an entity filter to a filter for the DB.
4039
return filter;
4140
},
41+
// Optional property to convert an entity sort to a DB sort. Defaults to the function below.
4242
constructSort: (sort) => {
43-
// Optional property that converts an entity sort to a sort for the DB.
4443
return sort;
4544
}.
4645
db: connectToDb({
@@ -52,16 +51,10 @@ const todoFactoryConfig: FactoryConfig<TodoEntity> = {
5251
user: 'todouser',
5352
},
5453
}),
55-
defaultPaginationLimit: 100, // Optional property.
54+
// Optional property. Defaults to 10.
55+
defaultPaginationLimit: 10,
5656
entityName: 'todo',
57+
// Optional property. Defaults to the entityName.
5758
tableName: 'todos',
58-
};
59-
```
60-
61-
### Construct the Facade
62-
63-
```ts
64-
import factory from '@js-entity-repos/knex/dist/factory';
65-
66-
const todosFacade = factory(todoFactoryConfig);
59+
});
6760
```

src/FactoryConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export default interface FacadeConfig<E extends Entity> {
1313
readonly db: () => Promise<knex>;
1414
readonly defaultPaginationLimit?: number;
1515
readonly entityName: string;
16-
readonly tableName: string;
16+
readonly tableName?: string;
1717
}

src/factory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> =>
1717
constructEntity: (document) => document,
1818
constructFilter: (filter) => filter,
1919
constructSort: (sort) => sort,
20-
defaultPaginationLimit: 100,
20+
defaultPaginationLimit: 10,
21+
tableName: factoryConfig.entityName,
2122
...factoryConfig,
2223
};
2324
return {

src/functions/getEntities.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import GetEntities from '@js-entity-repos/core/dist/signatures/GetEntities';
22
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import Pagination from '@js-entity-repos/core/dist/types/Pagination';
4+
import { forward } from '@js-entity-repos/core/dist/types/PaginationDirection';
35
import Sort from '@js-entity-repos/core/dist/types/Sort';
6+
import SortOrder, { asc } from '@js-entity-repos/core/dist/types/SortOrder';
47
import createCursorFromEntity from '@js-entity-repos/core/dist/utils/createCursorFromEntity';
58
import createPaginationFilter from '@js-entity-repos/core/dist/utils/createPaginationFilter';
69
import { first, last, mapValues } from 'lodash';
@@ -12,21 +15,21 @@ const xor = (conditionA: boolean, conditionB: boolean) => {
1215
};
1316

1417
export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
15-
const defaultPagination = {
18+
const defaultPagination: Pagination = {
1619
cursor: undefined,
17-
forward: true,
20+
direction: forward,
1821
limit: config.defaultPaginationLimit,
1922
};
20-
const defaultSort = { id: true } as Sort<E>;
23+
const defaultSort = { id: asc } as Sort<E>;
2124
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
2225
const table = (await config.db()).table(config.tableName);
2326

2427
const paginationFilter = createPaginationFilter(pagination, sort);
2528
const fullFilter = { $and: [filter, paginationFilter] };
2629
const constructedFilter = config.constructFilter(fullFilter);
2730
const constructedSort = config.constructSort(sort);
28-
const knexSort = mapValues(constructedSort, (sortValue): string => {
29-
return !xor(pagination.forward, sortValue) ? 'asc' : 'desc';
31+
const knexSort = mapValues(constructedSort, (sortOrder: SortOrder): string => {
32+
return !xor(pagination.direction === forward, sortOrder === asc) ? 'asc' : 'desc';
3033
});
3134

3235
const filterQuery = filterEntities(table, constructedFilter);

0 commit comments

Comments
 (0)