Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed May 29, 2017
2 parents 5a6639c + fdc71d1 commit bad4b0d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<a style="border-radius: 2px; padding: 4px 8px; background: rgb(64, 120, 192); color: #fff;" href="https://github.com/SierraSoftworks/Iridium/releases/tag/v7.2.1">tag: **v7.2.1**</a>

```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)
<a style="border-radius: 2px; padding: 4px 8px; background: rgb(64, 120, 192); color: #fff;" href="https://github.com/SierraSoftworks/Iridium/releases/tag/v7.2.0">tag: **v7.2.0**</a>

Expand Down
2 changes: 1 addition & 1 deletion doc
Submodule doc updated 44 files
+4 −4 classes/cacheonid.html
+21 −21 classes/core.html
+15 −15 classes/cursor.html
+32 −32 classes/instance.html
+4 −4 classes/memorycache.html
+69 −69 classes/model.html
+5 −5 classes/modelcache.html
+5 −5 classes/modelhandlers.html
+11 −11 classes/modelhelpers.html
+3 −3 classes/noopcache.html
+33 −33 classes/omnom.html
+16 −16 globals.html
+3 −3 interfaces/cache.html
+4 −4 interfaces/cachedirector.html
+1 −1 interfaces/callback.html
+13 −13 interfaces/changes.html
+7 −7 interfaces/configuration.html
+7 −7 interfaces/createoptions.html
+1 −1 interfaces/expressmiddleware.html
+4 −4 interfaces/hooks.html
+3 −3 interfaces/index.html
+2 −2 interfaces/instanceconstructor.html
+12 −12 interfaces/instanceimplementation.html
+5 −5 interfaces/instanceinternals.html
+1 −1 interfaces/mapfunction.html
+2 −2 interfaces/mapreduceddocument.html
+2 −2 interfaces/mapreducefunctions.html
+1 −1 interfaces/mapreduceoptions.html
+1 −1 interfaces/middlewarefactory.html
+1 −1 interfaces/modelspecificinstanceconstructor.html
+3 −3 interfaces/plugin.html
+1 −1 interfaces/predicate.html
+4 −4 interfaces/property.html
+1 −1 interfaces/propertygetter.html
+1 −1 interfaces/propertysetter.html
+2 −2 interfaces/propertytransform.html
+5 −5 interfaces/queryoptions.html
+8 −3 interfaces/reducefunction.html
+4 −4 interfaces/removeoptions.html
+1 −1 interfaces/schema.html
+2 −2 interfaces/transformoptions.html
+2 −2 interfaces/transforms.html
+5 −5 interfaces/updateoptions.html
+1 −1 modules/__global.html
2 changes: 1 addition & 1 deletion lib/MapReduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface MapFunction<TDocument> {
* @param {Value[]} values The values to reduce
*/
export interface ReduceFunction<Key, Value> {
(key: Key, values: Value[]): Value
(key: Key, values: Value[]): Value | Value[] | any
}

/**
Expand Down
25 changes: 14 additions & 11 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
conditions = this._helpers.convertToDB(conditions);

let cursor = this.collection.find(conditions);

if(fields)
cursor = cursor.project(fields);

Expand Down Expand Up @@ -383,18 +383,18 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
if (cachedDocument) return cachedDocument;
return new Bluebird<TDocument|null>((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);
Expand Down Expand Up @@ -573,7 +573,7 @@ export class Model<TDocument extends { _id?: any }, TInstance> {

if (opts.multi)
return this.collection.updateMany(conditions, changes, opts, callback);

return this.collection.updateOne(conditions, changes, opts, callback)
})
}).nodeify(callback);
Expand Down Expand Up @@ -671,7 +671,7 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
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);
Expand Down Expand Up @@ -706,11 +706,12 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
* @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<Key, Value>(instanceType: InstanceImplementation<MapReducedDocument<Key, Value>, any> & { mapReduceOptions: MapReduceFunctions<TDocument, Key, Value> },
mapReduce<Key, Value>(instanceType: InstanceImplementation<MapReducedDocument<Key, Value>, any>,
options: MapReduceOptions): Bluebird<void>;
mapReduce<Key, Value>(functions: (InstanceImplementation<MapReducedDocument<Key, Value>, any> & { mapReduceOptions: MapReduceFunctions<TDocument, Key, Value> }) |
mapReduce<Key, Value>(functions: InstanceImplementation<MapReducedDocument<Key, Value>, any> |
MapReduceFunctions<TDocument, Key, Value>, options: MapReduceOptions) {
type fn = MapReduceFunctions<TDocument, Key, Value>;
type instance = InstanceImplementation<MapReducedDocument<Key, Value>, any>

if ((<fn>functions).map) {
return new Bluebird<MapReducedDocument<Key, Value>[]>((resolve, reject) => {
Expand All @@ -725,10 +726,12 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
})
}
else {
let instanceType = <InstanceImplementation<MapReducedDocument<Key, Value>, any> & { mapReduceOptions: MapReduceFunctions<TDocument, Key, Value> }>functions;
let instanceType = <instance>functions;
return new Bluebird<void>((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 = <MongoDB.MapReduceOptions>options;
let out : {[op: string]: string} = {};
out[(<string>options.out)] = instanceType.collection;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iridium",
"version": "7.2.0",
"version": "7.2.1",
"author": "Benjamin Pannell <admin@sierrasoftworks.com>",
"description": "A custom lightweight ORM for MongoDB designed for power-users",
"license": "MIT",
Expand Down
32 changes: 32 additions & 0 deletions test/MapReduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ class MapReducedInstance extends Iridium.Instance<Iridium.MapReducedDocument<str
value: number;
}

@Iridium.MapReduce(function (this: TestDocument) {
emit(this.cust_id, this.amount);
}, function (key: string, values: number[]) {
return values.reduce((sum, val) => sum + val, 0);
})
class DecoratedMapReducedInstance extends Iridium.Instance<Iridium.MapReducedDocument<string, number>, MapReducedInstance>{
static collection = "decoratedMapReduced";
_id: string;
value: number;
}

class NotMapReducedInstance extends Iridium.Instance<Iridium.MapReducedDocument<string, number>, MapReducedInstance>{
static collection = "notMapReduced";
_id: string;
value: number;
}

describe("Model", () => {
let core = new Iridium.Core({ database: "test" });

Expand All @@ -61,6 +78,14 @@ describe("Model", () => {
]));
});

it("should correctly map and reduce with model and decorator", () => {
let reducedModel = new Iridium.Model<Iridium.MapReducedDocument<string, number>, 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<Iridium.MapReducedDocument<string, number>, MapReducedInstance>(core, MapReducedInstance);
let t = reducedModel.remove().then(() => model.mapReduce(MapReducedInstance, {
Expand Down Expand Up @@ -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;
});
});
});

0 comments on commit bad4b0d

Please sign in to comment.