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

Commit e9ddb07

Browse files
authoredFeb 9, 2018
fix: Allows reconnections to be added. (#9)
BREAKING CHANGE: `collection` property in the factory config is now a function that returns the collection to allow for reconnections.
1 parent 62fa0d4 commit e9ddb07

13 files changed

+24
-18
lines changed
 

‎readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import connectToCollection from '@js-entity-repos/mongo/dist/utils/connectToColl
2828
import parseFilterId from '@js-entity-repos/mongo/dist/utils/parseFilterId';
2929
import renameSortId from '@js-entity-repos/mongo/dist/utils/renameSortId';
3030

31-
const todoFacadeConfig: FacadeConfig<TodoEntity> = {
31+
const todoFactoryConfig: FactoryConfig<TodoEntity> = {
3232
collection: connectToCollection({
3333
collectionName: 'todos',
3434
dbName: 'todoapp',
@@ -60,5 +60,5 @@ const todoFacadeConfig: FacadeConfig<TodoEntity> = {
6060
```ts
6161
import factory from '@js-entity-repos/mongo/dist/factory';
6262

63-
const todosFacade = factory(todoFacadeConfig);
63+
const todosFacade = factory(todoFactoryConfig);
6464
```

‎src/FacadeConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Collection } from 'mongodb';
66
export type Document = any;
77

88
export default interface FacadeConfig<E extends Entity> {
9-
readonly collection: Promise<Collection>;
9+
readonly collection: () => Promise<Collection>;
1010
readonly defaultPaginationLimit: number;
1111
readonly entityName: string;
1212
readonly constructDocument: (patch: Partial<E>) => Document;

‎src/FactoryConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Collection } from 'mongodb';
66
export type Document = any;
77

88
export default interface FactoryConfig<E extends Entity> {
9-
readonly collection: Promise<Collection>;
9+
readonly collection: () => Promise<Collection>;
1010
readonly defaultPaginationLimit?: number;
1111
readonly entityName: string;
1212
readonly constructDocument?: (patch: Partial<E>) => Document;

‎src/functions/countEntities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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 collection = (await config.collection());
88
const constructedFilter = config.constructFilter(filter);
99
const count = await collection.find(constructedFilter).count();
1010
return { count };

‎src/functions/createEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 collection = (await config.collection());
1010
const document = config.constructDocument({ ...entity as any, id });
1111
const update = { $setOnInsert: document };
1212
const opts = { returnOriginal: false, upsert: true };

‎src/functions/getEntities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
1818
};
1919
const defaultSort = { id: true } as Sort<E>;
2020
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
21-
const collection = (await config.collection);
21+
const collection = (await config.collection());
2222

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

‎src/functions/getEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 collection = (await config.collection());
1010
const constructedFilter = constructIdFilter({ id, filter, config });
1111
const document = await collection.findOne(constructedFilter);
1212

‎src/functions/patchEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 collection = (await config.collection());
1010
const document = config.constructDocument({ ...patch as any, id });
1111
const update = { $set: document };
1212
const opts = { returnOriginal: false, upsert: false };

‎src/functions/removeEntities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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 collection = (await config.collection());
88
const constructedFilter = config.constructFilter(filter);
99
await collection.remove(constructedFilter);
1010
};

‎src/functions/removeEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 collection = (await config.collection());
1010
const constructedFilter = constructIdFilter({ id, filter, config });
1111
const { value } = await collection.findOneAndDelete(constructedFilter);
1212

‎src/functions/replaceEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 collection = (await config.collection());
1010
const opts = { returnOriginal: false, upsert: false };
1111
const constructedFilter = constructIdFilter({ id, filter, config });
1212
const { value } = await collection.findOneAndUpdate(constructedFilter, entity, opts);

‎src/utils/connectToCollection.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { once } from 'lodash';
12
import { Collection } from 'mongodb';
23
import connectToDb, { Opts as DbConnectionOpts } from './connectToDb';
34

45
export interface Opts extends DbConnectionOpts {
56
readonly collectionName: string;
67
}
78

8-
export default async ({ collectionName, ...dbConnectionOpts }: Opts): Promise<Collection> => {
9-
const db = await connectToDb(dbConnectionOpts);
10-
return db.collection(collectionName);
9+
export default ({ collectionName, ...dbConnectionOpts }: Opts) => {
10+
return once(async (): Promise<Collection> => {
11+
const db = await connectToDb(dbConnectionOpts)();
12+
return db.collection(collectionName);
13+
});
1114
};

‎src/utils/connectToDb.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { once } from 'lodash';
12
import { Db, MongoClient } from 'mongodb';
23

34
export interface Opts {
45
readonly dbName: string;
56
readonly url: string;
67
}
78

8-
export default async ({ dbName, url }: Opts): Promise<Db> => {
9-
const client = await MongoClient.connect(url);
10-
return client.db(dbName);
9+
export default ({ dbName, url }: Opts) => {
10+
return once(async (): Promise<Db> => {
11+
const client = await MongoClient.connect(url);
12+
return client.db(dbName);
13+
});
1114
};

0 commit comments

Comments
 (0)
This repository has been archived.