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

Commit 424241d

Browse files
authored
refactor: Changes the FactoryConfig to have more defaults. (#14)
BREAKING CHANGE: Removes `collection` from `FactoryConfig`. Use `db` and `collectionName` instead.
1 parent 9b96d1b commit 424241d

14 files changed

+38
-41
lines changed

readme.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ export interface TodoEntity extends Entity {
2323

2424
```ts
2525
import factory from '@js-entity-repos/mongo/dist/factory';
26-
import connectToCollection from '@js-entity-repos/mongo/dist/utils/connectToCollection';
26+
import connectToDb from '@js-entity-repos/mongo/dist/utils/connectToDb';
2727
import parseFilterId from '@js-entity-repos/mongo/dist/utils/parseFilterId';
2828
import renameSortId from '@js-entity-repos/mongo/dist/utils/renameSortId';
2929

3030
const todosFacade = factory<TodoEntity>({
31-
collection: connectToCollection({
32-
collectionName: 'todos',
33-
dbName: 'todoapp',
34-
url: 'mongodb://localhost:27017',
35-
}),
31+
// Optional property. Defaults to entityName.
32+
collectionName: 'todos',
3633
// Optional property to convert an entity to a DB document. Defaults to "utils/constructIdDocument".
3734
constructDocument: ({ id, ...patch}) => {
3835
return { _id: id, ...patch };
@@ -48,7 +45,11 @@ const todosFacade = factory<TodoEntity>({
4845
// Optional property to convert an entity sort to a DB sort. Defaults to "utils/renameSortId".
4946
constructSort: (sort) => {
5047
return renameSortId(sort);
51-
}.
48+
},
49+
db: connectToDb({
50+
dbName: 'todoapp',
51+
url: 'mongodb://localhost:27017',
52+
}),
5253
// Optional property. Defaults to 10.
5354
defaultPaginationLimit: 10,
5455
entityName: 'todo',

src/FacadeConfig.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Entity from '@js-entity-repos/core/dist/types/Entity';
22
import Filter from '@js-entity-repos/core/dist/types/Filter';
33
import Sort from '@js-entity-repos/core/dist/types/Sort';
4-
import { Collection } from 'mongodb';
4+
import { Db } from 'mongodb';
55

66
export type Document = any;
77

88
export default interface FacadeConfig<E extends Entity> {
9-
readonly collection: () => Promise<Collection>;
10-
readonly defaultPaginationLimit: number;
11-
readonly entityName: string;
9+
readonly collectionName: string;
1210
readonly constructDocument: (patch: Partial<E>) => Document;
1311
readonly constructEntity: (document: Document) => E;
1412
readonly constructFilter: (filter: Filter<E>) => any;
1513
readonly constructSort: (sort: Sort<E>) => any;
14+
readonly db: () => Promise<Db>;
15+
readonly defaultPaginationLimit: number;
16+
readonly entityName: string;
1617
}

src/FactoryConfig.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Entity from '@js-entity-repos/core/dist/types/Entity';
22
import Filter from '@js-entity-repos/core/dist/types/Filter';
33
import Sort from '@js-entity-repos/core/dist/types/Sort';
4-
import { Collection } from 'mongodb';
4+
import { Db } from 'mongodb';
55

66
export type Document = any;
77

88
export default interface FactoryConfig<E extends Entity> {
9-
readonly collection: () => Promise<Collection>;
10-
readonly defaultPaginationLimit?: number;
11-
readonly entityName: string;
9+
readonly collectionName?: string;
1210
readonly constructDocument?: (patch: Partial<E>) => Document;
1311
readonly constructEntity?: (document: Document) => E;
1412
readonly constructFilter?: (filter: Filter<E>) => any;
1513
readonly constructSort?: (sort: Sort<E>) => any;
14+
readonly db: () => Promise<Db>;
15+
readonly defaultPaginationLimit?: number;
16+
readonly entityName: string;
1617
}

src/factory.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import facadeTest from '@js-entity-repos/core/dist/tests';
22
import { TestEntity } from '@js-entity-repos/core/dist/tests/utils/testEntity';
33
import factory from './factory';
4-
import connectToCollection from './utils/connectToCollection';
4+
import connectToDb from './utils/connectToDb';
55

66
facadeTest(factory<TestEntity>({
7-
collection: connectToCollection({
8-
collectionName: 'testentities',
7+
db: connectToDb({
98
dbName: 'js-entity-repos-mongo',
109
url: 'mongodb://localhost:27017',
1110
}),
12-
entityName: 'Test Entity',
11+
entityName: 'TestEntity',
1312
}));

src/factory.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import renameSortId from './utils/renameSortId';
1717

1818
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> => {
1919
const facadeConfig: FacadeConfig<E> = {
20+
collectionName: factoryConfig.entityName,
2021
constructDocument: constructIdDocument,
2122
constructEntity: constructIdEntity,
2223
constructFilter: parseFilterId,

src/functions/countEntities.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import FacadeConfig from '../FacadeConfig';
44

55
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
66
return async ({ filter = {} }) => {
7-
const collection = (await config.collection());
7+
const db = (await config.db());
8+
const collection = db.collection(config.collectionName);
89
const constructedFilter = config.constructFilter(filter);
910
const count = await collection.find(constructedFilter).count();
1011
return { count };

src/functions/createEntity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
88
return async ({ id, entity }) => {
9-
const collection = (await config.collection());
9+
const db = (await config.db());
10+
const collection = db.collection(config.collectionName);
1011
const document = config.constructDocument({ ...entity as any, id });
1112
const update = { $setOnInsert: document };
1213
const opts = { returnOriginal: false, upsert: true };

src/functions/getEntities.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
2121
};
2222
const defaultSort = { id: asc } as Sort<E>;
2323
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
24-
const collection = (await config.collection());
24+
const db = (await config.db());
25+
const collection = db.collection(config.collectionName);
2526

2627
const paginationFilter = createPaginationFilter(pagination, sort);
2728
const fullFilter = { $and: [filter, paginationFilter] };

src/functions/getEntity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import constructIdFilter from '../utils/constructIdFilter';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
88
return async ({ id, filter = {} }) => {
9-
const collection = (await config.collection());
9+
const db = (await config.db());
10+
const collection = db.collection(config.collectionName);
1011
const constructedFilter = constructIdFilter({ id, filter, config });
1112
const document = await collection.findOne(constructedFilter);
1213

src/functions/patchEntity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import constructIdFilter from '../utils/constructIdFilter';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
88
return async ({ id, patch, filter = {} }) => {
9-
const collection = (await config.collection());
9+
const db = (await config.db());
10+
const collection = db.collection(config.collectionName);
1011
const document = config.constructDocument({ ...patch as any, id });
1112
const update = { $set: document };
1213
const opts = { returnOriginal: false, upsert: false };

src/functions/removeEntities.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import FacadeConfig from '../FacadeConfig';
44

55
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
66
return async ({ filter = {} }) => {
7-
const collection = (await config.collection());
7+
const db = (await config.db());
8+
const collection = db.collection(config.collectionName);
89
const constructedFilter = config.constructFilter(filter);
910
await collection.remove(constructedFilter);
1011
};

src/functions/removeEntity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import constructIdFilter from '../utils/constructIdFilter';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
88
return async ({ id, filter = {} }) => {
9-
const collection = (await config.collection());
9+
const db = (await config.db());
10+
const collection = db.collection(config.collectionName);
1011
const constructedFilter = constructIdFilter({ id, filter, config });
1112
const { value } = await collection.findOneAndDelete(constructedFilter);
1213

src/functions/replaceEntity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import constructIdFilter from '../utils/constructIdFilter';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
88
return async ({ id, entity, filter = {} }) => {
9-
const collection = (await config.collection());
9+
const db = (await config.db());
10+
const collection = db.collection(config.collectionName);
1011
const opts = { returnOriginal: false, upsert: false };
1112
const constructedFilter = constructIdFilter({ id, filter, config });
1213
const { value } = await collection.findOneAndUpdate(constructedFilter, entity, opts);

src/utils/connectToCollection.ts

-14
This file was deleted.

0 commit comments

Comments
 (0)