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

Commit 752ed85

Browse files
committed
fix(deps): Updates core to 6.0.2.
1 parent bc60e15 commit 752ed85

26 files changed

+255
-131
lines changed

package-lock.json

Lines changed: 8 additions & 9 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.1",
28+
"@js-entity-repos/core": "^6.0.2",
2929
"lodash": "^4.17.4",
3030
"mongodb": "^3.0.0"
3131
},

readme.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,61 @@
44
### Usage
55
1. Install it with `npm i @js-entity-repos/mongo`.
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 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/mongo/dist/Config';
26+
import FactoryConfig from '@js-entity-repos/mongo/dist/FactoryConfig';
2927
import connectToCollection from '@js-entity-repos/mongo/dist/utils/connectToCollection';
28+
import parseFilterId from '@js-entity-repos/mongo/dist/utils/parseFilterId';
29+
import renameSortId from '@js-entity-repos/mongo/dist/utils/renameSortId';
3030

31-
const todoFacadeConfig: FacadeConfig = {
31+
const todoFacadeConfig: FacadeConfig<TodoEntity> = {
3232
collection: connectToCollection({
3333
collectionName: 'todos',
3434
dbName: 'todoapp',
3535
url: 'mongodb://localhost:27017',
3636
}),
37-
constructDocument: (id, patch) => {
38-
// Converts an entity to a document for the database.
39-
return { ...patch, ...id };
37+
constructDocument: ({ id, ...patch}) => {
38+
// Optional property that converts an entity to a document for the database.
39+
return { _id: id, ...patch };
4040
},
4141
constructEntity: ({ _id, ...document }) => {
42-
// Converts a document from the database to an entity.
43-
return document;
42+
// Optional property that converts a document from the database to an entity.
43+
return { id: _id, ...document };
44+
},
45+
constructFilter: (filter) => {
46+
// Optional property that converts an entity filter to a filter for the DB.
47+
return parseFilterId(filter);
4448
},
49+
constructSort: (sort) => {
50+
// Optional property that converts an entity sort to a sort for the DB.
51+
return renameSortId(sort);
52+
}.
53+
defaultPaginationLimit: 100, // Optional property.
4554
entityName: 'todo',
4655
};
4756
```
4857

4958
### Construct the Facade
5059

5160
```ts
52-
import facade from '@js-entity-repos/mongo/dist/facade';
61+
import factory from '@js-entity-repos/mongo/dist/factory';
5362

54-
const todosFacade = facade<TodoId, TodoEntity>(todoFacadeConfig);
63+
const todosFacade = factory(todoFacadeConfig);
5564
```

src/Config.ts

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

src/FacadeConfig.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 { Collection } from 'mongodb';
5+
6+
export type Document = any;
7+
8+
export default interface FacadeConfig<E extends Entity> {
9+
readonly collection: Promise<Collection>;
10+
readonly defaultPaginationLimit: number;
11+
readonly entityName: string;
12+
readonly constructDocument: (patch: Partial<E>) => Document;
13+
readonly constructEntity: (document: Document) => E;
14+
readonly constructFilter: (filter: Filter<E>) => any;
15+
readonly constructSort: (sort: Sort<E>) => any;
16+
}

src/FactoryConfig.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 { Collection } from 'mongodb';
5+
6+
export type Document = any;
7+
8+
export default interface FactoryConfig<E extends Entity> {
9+
readonly collection: Promise<Collection>;
10+
readonly defaultPaginationLimit?: number;
11+
readonly entityName: string;
12+
readonly constructDocument?: (patch: Partial<E>) => Document;
13+
readonly constructEntity?: (document: Document) => E;
14+
readonly constructFilter?: (filter: Filter<E>) => any;
15+
readonly constructSort?: (sort: Sort<E>) => any;
16+
}

src/facade.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import facadeTest from '@js-entity-repos/core/dist/tests';
2-
import { TestEntity, TestId } from '@js-entity-repos/core/dist/tests/utils/testEntity';
3-
import facade from './facade';
2+
import { TestEntity } from '@js-entity-repos/core/dist/tests/utils/testEntity';
3+
import factory from './factory';
44
import connectToCollection from './utils/connectToCollection';
55

6-
facadeTest(facade<TestId, TestEntity>({
6+
facadeTest(factory<TestEntity>({
77
collection: connectToCollection({
88
collectionName: 'testentities',
99
dbName: 'js-entity-repos-mongo',
1010
url: 'mongodb://localhost:27017',
1111
}),
12-
constructDocument: (id, patch) => {
13-
return { ...patch, ...id };
14-
},
15-
constructEntity: ({ _id, ...document }) => {
16-
return document;
17-
},
1812
entityName: 'Test Entity',
1913
}));

src/factory.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import constructIdDocument from './utils/constructIdDocument';
14+
import constructIdEntity from './utils/constructIdEntity';
15+
import parseFilterId from './utils/parseFilterId';
16+
import renameSortId from './utils/renameSortId';
17+
18+
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> => {
19+
const facadeConfig: FacadeConfig<E> = {
20+
constructDocument: constructIdDocument,
21+
constructEntity: constructIdEntity,
22+
constructFilter: parseFilterId,
23+
constructSort: renameSortId,
24+
defaultPaginationLimit: 100,
25+
...factoryConfig,
26+
};
27+
return {
28+
countEntities: countEntities<E>(facadeConfig),
29+
createEntity: createEntity<E>(facadeConfig),
30+
getEntities: getEntities<E>(facadeConfig),
31+
getEntity: getEntity<E>(facadeConfig),
32+
patchEntity: patchEntity<E>(facadeConfig),
33+
removeEntities: removeEntities<E>(facadeConfig),
34+
removeEntity: removeEntity<E>(facadeConfig),
35+
replaceEntity: replaceEntity<E>(facadeConfig),
36+
};
37+
};

src/functions/countEntities.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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

4-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): CountEntities<Entity> => {
5-
return async ({ filter }) => {
5+
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
6+
return async ({ filter = {} }) => {
67
const collection = (await config.collection);
7-
const count = await collection.find(filter).count();
8+
const constructedFilter = config.constructFilter(filter);
9+
const count = await collection.find(constructedFilter).count();
810
return { count };
911
};
1012
};

0 commit comments

Comments
 (0)