diff --git a/CHANGELOG.md b/CHANGELOG.md index 5695028..a70aaf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,21 @@ For a human friendly description of what changed in each release, take a look at the [Releases](https://github.com/SierraSoftworks/Iridium/releases) page. +## Version [7.2.1](https://github.com/SierraSoftworks/Iridium/releases/tag/v7.2.1) +tag: **v7.2.1** + +```sh +npm install iridium@7.2.1 +``` + +### Changes + - [8bc77dd](https://github.com/SierraSoftworks/Iridium/commit/8bc77dd) 7.2.1 + - [f5e543f](https://github.com/SierraSoftworks/Iridium/commit/f5e543f) style: Improve error message for missing mapReduceOptions on instance + - [23a3d1a](https://github.com/SierraSoftworks/Iridium/commit/23a3d1a) Merge pull request #78 from RagibHasin/master + - [5eb054f](https://github.com/SierraSoftworks/Iridium/commit/5eb054f) Fix code quality with mapReduce + - [c79b2a4](https://github.com/SierraSoftworks/Iridium/commit/c79b2a4) Fix mapReduce reduce function return type, mapReduce parameter validation and add new tests + - [db0d44e](https://github.com/SierraSoftworks/Iridium/commit/db0d44e) doc: Update changelog + ## Version [7.2.0](https://github.com/SierraSoftworks/Iridium/releases/tag/v7.2.0) tag: **v7.2.0** diff --git a/doc b/doc index 613dd0d..e71feb5 160000 --- a/doc +++ b/doc @@ -1 +1 @@ -Subproject commit 613dd0d4cd8488b9150f55bda9db6e3ffff97b05 +Subproject commit e71feb500c6776868c760d4b0d5abbb451a35c65 diff --git a/lib/MapReduce.ts b/lib/MapReduce.ts index b630c60..38c65de 100644 --- a/lib/MapReduce.ts +++ b/lib/MapReduce.ts @@ -35,7 +35,7 @@ export interface MapFunction { * @param {Value[]} values The values to reduce */ export interface ReduceFunction { - (key: Key, values: Value[]): Value + (key: Key, values: Value[]): Value | Value[] | any } /** diff --git a/lib/Model.ts b/lib/Model.ts index c68fb21..c9d9817 100644 --- a/lib/Model.ts +++ b/lib/Model.ts @@ -271,7 +271,7 @@ export class Model { conditions = this._helpers.convertToDB(conditions); let cursor = this.collection.find(conditions); - + if(fields) cursor = cursor.project(fields); @@ -383,18 +383,18 @@ export class Model { if (cachedDocument) return cachedDocument; return new Bluebird((resolve, reject) => { let cursor = this.collection.find(conditions); - + if(options!.sort) cursor = cursor.sort(options!.sort!); - + if(typeof options!.skip === "number") cursor = cursor.skip(options!.skip!); - + cursor = cursor.limit(1); - + if(options!.fields) cursor = cursor.project(options!.fields!); - + return cursor.next((err, result) => { if (err) return reject(err); return resolve(result); @@ -573,7 +573,7 @@ export class Model { if (opts.multi) return this.collection.updateMany(conditions, changes, opts, callback); - + return this.collection.updateOne(conditions, changes, opts, callback) }) }).nodeify(callback); @@ -671,7 +671,7 @@ export class Model { if (err) return reject(err); return resolve(response.result.n); }); - + this.collection.deleteMany(conditions, options!, (err, response) => { if (err) return reject(err); return resolve(response.result.n); @@ -706,11 +706,12 @@ export class Model { * @param options Options used to configure how MongoDB runs the mapReduce operation on your collection. * @return A promise which completes when the mapReduce operation has written its results to the provided collection. */ - mapReduce(instanceType: InstanceImplementation, any> & { mapReduceOptions: MapReduceFunctions }, + mapReduce(instanceType: InstanceImplementation, any>, options: MapReduceOptions): Bluebird; - mapReduce(functions: (InstanceImplementation, any> & { mapReduceOptions: MapReduceFunctions }) | + mapReduce(functions: InstanceImplementation, any> | MapReduceFunctions, options: MapReduceOptions) { type fn = MapReduceFunctions; + type instance = InstanceImplementation, any> if ((functions).map) { return new Bluebird[]>((resolve, reject) => { @@ -725,10 +726,12 @@ export class Model { }) } else { - let instanceType = , any> & { mapReduceOptions: MapReduceFunctions }>functions; + let instanceType = functions; return new Bluebird((resolve, reject) => { if (options.out && options.out == "inline") return reject(new Error("Expected a non-inline mapReduce output mode for this method signature")); + if (!instanceType.mapReduceOptions) + return reject(new Error("Expected mapReduceOptions to be specified on the instance type")); let opts = options; let out : {[op: string]: string} = {}; out[(options.out)] = instanceType.collection; diff --git a/package.json b/package.json index 389f64f..6c5df9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iridium", - "version": "7.2.0", + "version": "7.2.1", "author": "Benjamin Pannell ", "description": "A custom lightweight ORM for MongoDB designed for power-users", "license": "MIT", diff --git a/test/MapReduce.ts b/test/MapReduce.ts index cbbf23d..c4191f6 100644 --- a/test/MapReduce.ts +++ b/test/MapReduce.ts @@ -44,6 +44,23 @@ class MapReducedInstance extends Iridium.Instance sum + val, 0); +}) +class DecoratedMapReducedInstance extends Iridium.Instance, MapReducedInstance>{ + static collection = "decoratedMapReduced"; + _id: string; + value: number; +} + +class NotMapReducedInstance extends Iridium.Instance, MapReducedInstance>{ + static collection = "notMapReduced"; + _id: string; + value: number; +} + describe("Model", () => { let core = new Iridium.Core({ database: "test" }); @@ -61,6 +78,14 @@ describe("Model", () => { ])); }); + it("should correctly map and reduce with model and decorator", () => { + let reducedModel = new Iridium.Model, DecoratedMapReducedInstance>(core, DecoratedMapReducedInstance); + let t = reducedModel.remove().then(() => model.mapReduce(DecoratedMapReducedInstance, { + out: "replace", query: { status: "A" } + }).then(() => reducedModel.find().toArray())); + return chai.expect(t).to.eventually.exist.and.have.length(2); + }); + it("should correctly map and reduce with model", () => { let reducedModel = new Iridium.Model, MapReducedInstance>(core, MapReducedInstance); let t = reducedModel.remove().then(() => model.mapReduce(MapReducedInstance, { @@ -97,5 +122,12 @@ describe("Model", () => { }); return chai.expect(t).to.eventually.be.rejected; }); + + it("should reject with no mapReduce info in Instance type", () => { + let t = model.mapReduce(NotMapReducedInstance, { + out: "replace", query: { status: "A" } + }); + return chai.expect(t).to.eventually.be.rejected; + }); }); }); \ No newline at end of file