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
};

0 commit comments

Comments
 (0)