Skip to content

Commit

Permalink
style: Tweak things to look like the rest of the Iridium codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed May 18, 2017
1 parent a02dc9a commit 8c01e17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
5 changes: 2 additions & 3 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Index, IndexSpecification} from "./Index";
import {Schema} from "./Schema";
import {InstanceImplementation} from "./InstanceInterface";
import {Transforms, DefaultTransforms} from "./Transforms";
import * as MapReduceDef from './MapReduce';
import * as MapReduceDef from "./MapReduce";

/**
* Specifies the name of the collection to which this instance's documents should be sent.
Expand Down Expand Up @@ -179,8 +179,7 @@ export function Binary(target: Instance<any, any>, name: string) {
* classes. If your transpiler does not support decorators then you are free to make use of the
* property instead.
*/
export function MapReduce<TDocument, Key, Value>(map: MapReduceDef.MapFunction<TDocument>,
reduce: MapReduceDef.ReduceFunction<Key, Value>) {
export function MapReduce<TDocument, Key, Value>(map: MapReduceDef.MapFunction<TDocument>, reduce: MapReduceDef.ReduceFunction<Key, Value>) {
return function (target: InstanceImplementation<any, any>) {
target.mapReduceOptions = { map: map, reduce: reduce };
};
Expand Down
22 changes: 11 additions & 11 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,27 +716,27 @@ export class Model<TDocument extends { _id?: any }, TInstance> {
return new Bluebird<MapReducedDocument<Key, Value>[]>((resolve, reject) => {
if (options.out && options.out != "inline")
return reject(new Error("Expected inline mapReduce output mode for this method signature"));
let opts = <MongoDB.MapReduceOptions>options
opts.out = { inline: 1 }
let opts = <MongoDB.MapReduceOptions>options;
opts.out = { inline: 1 };
this.collection.mapReduce((<fn>functions).map, (<fn>functions).reduce, opts, function (err, data) {
if (err) return reject(err);
return resolve(data);
})
});
})
}
else {
let instanceType = <InstanceImplementation<MapReducedDocument<Key, Value>, any> & { mapReduceOptions: MapReduceFunctions<TDocument, Key, Value> }>functions
let instanceType = <InstanceImplementation<MapReducedDocument<Key, Value>, any> & { mapReduceOptions: MapReduceFunctions<TDocument, Key, Value> }>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"));
let opts = <MongoDB.MapReduceOptions>options
let out : {[op: string]: string} = {}
out[(<string>options.out)] = instanceType.collection
opts.out = out
let opts = <MongoDB.MapReduceOptions>options;
let out : {[op: string]: string} = {};
out[(<string>options.out)] = instanceType.collection;
opts.out = out;
this.collection.mapReduce(instanceType.mapReduceOptions.map, instanceType.mapReduceOptions.reduce, opts, (err, data) => {
if (err) return reject(err)
return resolve()
})
if (err) return reject(err);
return resolve();
});
})
}
}
Expand Down
23 changes: 12 additions & 11 deletions test/MapReduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ class MapReducedInstance extends Iridium.Instance<Iridium.MapReducedDocument<str
emit(this.cust_id, this.amount);
},
reduce: function (key: string, values: number[]) {
return values.reduce((sum, val) => sum + val, 0)
return values.reduce((sum, val) => sum + val, 0);
}
}
_id: string
value: number

_id: string;
value: number;
}

describe("Model", () => {
Expand All @@ -64,7 +65,7 @@ describe("Model", () => {
let reducedModel = new Iridium.Model<Iridium.MapReducedDocument<string, number>, MapReducedInstance>(core, MapReducedInstance);
let t = reducedModel.remove().then(() => model.mapReduce(MapReducedInstance, {
out: "replace", query: { status: "A" }
}).then(() => reducedModel.find().toArray()))
}).then(() => reducedModel.find().toArray()));
return chai.expect(t).to.eventually.exist.and.have.length(2);
});

Expand All @@ -73,9 +74,9 @@ describe("Model", () => {
map: function (this: TestDocument) {
emit(this.cust_id, this.amount);
}, reduce: function (k: string, v: number[]) {
return v.reduce((sum, val) => sum + val, 0)
return v.reduce((sum, val) => sum + val, 0);
}
}, { query: { status: "A" } })
}, { query: { status: "A" } });
return chai.expect(t).to.eventually.exist.and.have.length(2);
});

Expand All @@ -84,17 +85,17 @@ describe("Model", () => {
map: function (this: TestDocument) {
emit(this.cust_id, this.amount);
}, reduce: function (k: string, v: number[]) {
return v.reduce((sum, val) => sum + val, 0)
return v.reduce((sum, val) => sum + val, 0);
}
}, { out: "replace", query: { status: "A" } })
return chai.expect(t).to.eventually.be.rejected
}, { out: "replace", query: { status: "A" } });
return chai.expect(t).to.eventually.be.rejected;
});

it("should reject with wrong out option for model", () => {
let t = model.mapReduce(MapReducedInstance, {
out: "inline", query: { status: "A" }
})
return chai.expect(t).to.eventually.be.rejected
});
return chai.expect(t).to.eventually.be.rejected;
});
});
});

0 comments on commit 8c01e17

Please sign in to comment.