Comparing version 5.0.0-2 to 5.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DatabaseClient = void 0; | ||
const events_1 = require("events"); | ||
@@ -4,0 +5,0 @@ const mongodb_1 = require("mongodb"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.After = exports.Before = exports.Collection = void 0; | ||
const Types_1 = require("./Types"); | ||
@@ -12,3 +13,3 @@ /** | ||
function Collection(props) { | ||
return function (target) { | ||
return (target) => { | ||
Reflect.defineMetadata(Types_1.COLLECTION_KEY, props, target.prototype); | ||
@@ -36,3 +37,3 @@ }; | ||
function Before(...events) { | ||
return function (target, name, descriptor) { | ||
return (target, name, descriptor) => { | ||
for (const event of events) { | ||
@@ -66,3 +67,3 @@ const fns = Reflect.getMetadata(`${Types_1.PRE_KEY}_${event}`, target) || []; | ||
function After(...events) { | ||
return function (target, name, descriptor) { | ||
return (target, name, descriptor) => { | ||
for (const event of events) { | ||
@@ -69,0 +70,0 @@ const fns = Reflect.getMetadata(`${Types_1.POST_KEY}_${event}`, target) || []; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Deferred = void 0; | ||
/** | ||
@@ -4,0 +5,0 @@ * Deferred object based on MDN spec |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./Repository")); | ||
__export(require("./Decorators")); | ||
__export(require("./Types")); | ||
__export(require("./DatabaseClient")); | ||
__export(require("./Deferred")); | ||
exports.Db = void 0; | ||
__exportStar(require("./Repository"), exports); | ||
__exportStar(require("./Decorators"), exports); | ||
__exportStar(require("./Types"), exports); | ||
__exportStar(require("./DatabaseClient"), exports); | ||
__exportStar(require("./Deferred"), exports); | ||
var mongodb_1 = require("mongodb"); | ||
exports.Db = mongodb_1.Db; | ||
Object.defineProperty(exports, "Db", { enumerable: true, get: function () { return mongodb_1.Db; } }); | ||
//# sourceMappingURL=index.js.map |
import { Collection, DeleteWriteOpResultObject } from 'mongodb'; | ||
import { CollectionProps, DBSource, Document, FindRequest, UpdateByIdRequest, UpdateRequest, RepoOperation } from './Types'; | ||
import { CollectionProps, DBSource, Document, FindRequest, UpdateByIdRequest, UpdateRequest, RepoOperation, EventOptions } from './Types'; | ||
export declare class MongoRepository<DOC, DTO = DOC> { | ||
@@ -10,2 +10,3 @@ dbSource: DBSource; | ||
* @param {DBSource} dbSource Your MongoDB connection | ||
* @param opts Collection initialize options | ||
* @memberof MongoRepository | ||
@@ -26,3 +27,3 @@ */ | ||
* @param {string[]} ids | ||
* @returns {Promise<T[]>} | ||
* @returns {Promise<DOC[]>} | ||
* @memberof MongoRepository | ||
@@ -43,6 +44,6 @@ */ | ||
* @param {FindRequest} [req={ conditions: {} }] | ||
* @returns {Promise<T[]>} | ||
* @returns {Promise<PROJECTION[]>} | ||
* @memberof MongoRepository | ||
*/ | ||
find(req?: FindRequest): Promise<DOC[]>; | ||
find<PROJECTION = DOC>(req?: FindRequest): Promise<PROJECTION[]>; | ||
/** | ||
@@ -111,6 +112,6 @@ * Create a document of type T | ||
* @param {boolean} replace | ||
* @returns {T} | ||
* @returns {any} | ||
* @memberof MongoRepository | ||
*/ | ||
protected toggleId(document: any, replace: boolean): DOC; | ||
protected toggleId(document: any, replace: boolean): any; | ||
/** | ||
@@ -123,7 +124,8 @@ * Apply functions to a record based on the type of event | ||
* @param {*} newDocument The document to apply functions to | ||
* @param {*} oldDocument The original document before changes were applied | ||
* @returns {Promise<DOC>} DOC will be null for delete events | ||
* @param originalDocument The original document before changes were applied | ||
* @param opts Options for event | ||
* @returns {Promise<any>} | ||
* @memberof MongoRepository | ||
*/ | ||
protected invokeEvents(type: string, fns: RepoOperation[], newDocument: any, originalDocument?: any): Promise<any>; | ||
protected invokeEvents(type: string, fns: RepoOperation[], newDocument: any, originalDocument?: any, opts?: EventOptions): Promise<any>; | ||
/** | ||
@@ -130,0 +132,0 @@ * Return a collection |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MongoRepository = void 0; | ||
const mongodb_1 = require("mongodb"); | ||
const clone = require('rfdc')({ proto: true }); | ||
const Types_1 = require("./Types"); | ||
@@ -12,2 +14,3 @@ class MongoRepository { | ||
* @param {DBSource} dbSource Your MongoDB connection | ||
* @param opts Collection initialize options | ||
* @memberof MongoRepository | ||
@@ -37,3 +40,3 @@ */ | ||
* @param {string[]} ids | ||
* @returns {Promise<T[]>} | ||
* @returns {Promise<DOC[]>} | ||
* @memberof MongoRepository | ||
@@ -71,3 +74,3 @@ */ | ||
* @param {FindRequest} [req={ conditions: {} }] | ||
* @returns {Promise<T[]>} | ||
* @returns {Promise<PROJECTION[]>} | ||
* @memberof MongoRepository | ||
@@ -133,2 +136,5 @@ */ | ||
let newDocument = await collection.findOne(query); | ||
// flip flop ids back | ||
this.toggleId(newDocument, false); | ||
newDocument = await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.save], newDocument, originalDoc); | ||
// project new items | ||
@@ -138,6 +144,2 @@ if (newDocument) { | ||
} | ||
// flip flop ids back | ||
newDocument['id'] = id.toString(); | ||
delete newDocument['_id']; | ||
newDocument = await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.save], newDocument, originalDoc); | ||
return newDocument; | ||
@@ -204,7 +206,5 @@ } | ||
const collection = await this.collection; | ||
let document = await collection.findOne(conditions); | ||
document = this.toggleId(document, false); | ||
await this.invokeEvents(Types_1.PRE_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteOne], null, document); | ||
await this.invokeEvents(Types_1.PRE_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteOne], conditions); | ||
const deleteResult = await collection.deleteOne(conditions); | ||
await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteOne], null, document); | ||
await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteOne], deleteResult); | ||
return deleteResult; | ||
@@ -221,12 +221,5 @@ } | ||
const collection = await this.collection; | ||
const documents = (await collection.find(conditions).toArray()).map(document => { | ||
return this.toggleId(document, false); | ||
}); | ||
for (const document of documents) { | ||
await this.invokeEvents(Types_1.PRE_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteMany], null, document); | ||
} | ||
await this.invokeEvents(Types_1.PRE_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteMany], conditions); | ||
const deleteResult = await collection.deleteMany(conditions); | ||
for (const document of documents) { | ||
await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteMany], null, document); | ||
} | ||
await this.invokeEvents(Types_1.POST_KEY, [Types_1.RepoOperation.delete, Types_1.RepoOperation.deleteMany], deleteResult); | ||
return deleteResult; | ||
@@ -240,3 +233,3 @@ } | ||
* @param {boolean} replace | ||
* @returns {T} | ||
* @returns {any} | ||
* @memberof MongoRepository | ||
@@ -264,7 +257,18 @@ */ | ||
* @param {*} newDocument The document to apply functions to | ||
* @param {*} oldDocument The original document before changes were applied | ||
* @returns {Promise<DOC>} DOC will be null for delete events | ||
* @param originalDocument The original document before changes were applied | ||
* @param opts Options for event | ||
* @returns {Promise<any>} | ||
* @memberof MongoRepository | ||
*/ | ||
async invokeEvents(type, fns, newDocument, originalDocument) { | ||
async invokeEvents(type, fns, newDocument, originalDocument, opts) { | ||
var _a; | ||
// local options override global collection options | ||
if ((opts === null || opts === void 0 ? void 0 : opts.noClone) === false || ((opts === null || opts === void 0 ? void 0 : opts.noClone) === undefined && ((_a = this.options.eventOpts) === null || _a === void 0 ? void 0 : _a.noClone) !== true)) { | ||
// Dereference (aka clone) the target document(s) to | ||
// prevent down stream modifications in events on the original instance(s) | ||
newDocument = clone(newDocument); | ||
if (originalDocument) { | ||
originalDocument = clone(originalDocument); | ||
} | ||
} | ||
for (const fn of fns) { | ||
@@ -279,3 +283,3 @@ const events = Reflect.getMetadata(`${type}_${fn}`, this) || []; | ||
newDocument = event.bind(this)(newDocument, repoEventArgs); | ||
if (newDocument && typeof newDocument.then === 'function') { | ||
if (typeof newDocument.then === 'function') { | ||
newDocument = await newDocument; | ||
@@ -282,0 +286,0 @@ } |
@@ -19,2 +19,8 @@ import { Db, ObjectID, MongoClient, IndexOptions } from 'mongodb'; | ||
} | ||
export interface EventOptions { | ||
/** | ||
* Override cloning of event target reference(s) | ||
*/ | ||
noClone?: boolean; | ||
} | ||
export interface CollectionProps { | ||
@@ -26,2 +32,3 @@ name: string; | ||
indexes?: IndexDefinition[]; | ||
eventOpts?: EventOptions; | ||
} | ||
@@ -28,0 +35,0 @@ export interface IndexDefinition { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RepoOperation = exports.POST_KEY = exports.PRE_KEY = exports.COLLECTION_KEY = void 0; | ||
exports.COLLECTION_KEY = 'collection'; | ||
@@ -4,0 +5,0 @@ exports.PRE_KEY = 'pre'; |
{ | ||
"name": "mongtype", | ||
"version": "5.0.0-2", | ||
"version": "5.0.0", | ||
"description": "Mongo Repository for Node written in TypeScript", | ||
@@ -17,2 +17,3 @@ "main": "dist/index.js", | ||
"lint": "tslint ./{src,tests}/{,**/}*.ts", | ||
"lint:fix": "npm run lint -- --fix", | ||
"prettier": "prettier --write \"./{src,tests}/**/*.{js,ts,md}\"", | ||
@@ -38,6 +39,7 @@ "prettier:test": "prettier -c \"./{src,tests}/**/*.{js,ts,md}\"", | ||
"devDependencies": { | ||
"@swimlane/prettier-config-swimlane": "^2.0.0", | ||
"@swimlane/prettier-config-swimlane": "^3.0.3", | ||
"@types/chai": "^4.2.9", | ||
"@types/faker": "^4.1.9", | ||
"@types/mocha": "^7.0.1", | ||
"@types/mongodb": "^3.6.7", | ||
"@types/node": "^12.12.27", | ||
@@ -47,3 +49,3 @@ "@types/retry": "^0.12.0", | ||
"clone": "^2.1.2", | ||
"codacy-coverage": "^3.4.0", | ||
"codacy-coverage": "^3.2.0", | ||
"faker": "^4.1.0", | ||
@@ -53,15 +55,15 @@ "mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.19.1", | ||
"prettier": "~2.2.1", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^8.6.2", | ||
"tslint": "^6.1.1", | ||
"tslint": "^5.8.0", | ||
"tslint-config-security": "^1.16.0", | ||
"tslint-config-swimlane": "^4.0.0", | ||
"typescript": "^3.7.5" | ||
"tslint-config-swimlane": "^5.0.0", | ||
"typescript": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"@types/mongodb": "^3.3.16", | ||
"mongodb": "^3.5.5", | ||
"retry": "^0.12.0" | ||
"mongodb": "^3.6.4", | ||
"retry": "^0.12.0", | ||
"rfdc": "^1.2.0" | ||
}, | ||
@@ -68,0 +70,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
50362
881
0
22
+ Addedrfdc@^1.2.0
+ Addedrfdc@1.4.1(transitive)
- Removed@types/mongodb@^3.3.16
- Removed@types/bson@4.2.4(transitive)
- Removed@types/mongodb@3.6.20(transitive)
- Removed@types/node@22.8.6(transitive)
- Removedbson@6.9.0(transitive)
- Removedundici-types@6.19.8(transitive)
Updatedmongodb@^3.6.4