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 Aug 4, 2015
2 parents b4b42a2 + 465cc85 commit ba70443
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 26 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## [Working Changes](https://github.com/sierrasoftworks/iridium)
- [431bb0b](https://github.com/sierrasoftworks/iridium/commit/431bb0b) Version 5.9.0
- [5f326fd](https://github.com/sierrasoftworks/iridium/commit/5f326fd) Clean up the references.d.ts file before bumping the version
- [445ebb6](https://github.com/sierrasoftworks/iridium/commit/445ebb6) Added support and tests for the MongoDB aggregate operation
- [f5926ce](https://github.com/sierrasoftworks/iridium/commit/f5926ce) (origin/master, origin/HEAD) Updated CHANGELOG

## [v5.8.0](https://github.com/sierrasoftworks/iridium/tree/v5.8.0)
- [ab6904e](https://github.com/sierrasoftworks/iridium/commit/ab6904e) Version 5.8.0
- [625397d](https://github.com/sierrasoftworks/iridium/commit/625397d) Added support and tests for a toObjectID helper method
- [4ebd33d](https://github.com/sierrasoftworks/iridium/commit/4ebd33d) (origin/master, origin/HEAD) Updated CHANGELOG
- [4ebd33d](https://github.com/sierrasoftworks/iridium/commit/4ebd33d) Updated CHANGELOG

## [v5.7.5](https://github.com/sierrasoftworks/iridium/tree/v5.7.5)
- [b5cb543](https://github.com/sierrasoftworks/iridium/commit/b5cb543) Version 5.7.5
Expand Down
5 changes: 3 additions & 2 deletions build/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ function getPackageJsonVersion() {

gulp.task('version-bump', function () {
var args = minimist(process.argv);

var options = {};
if (semver.valid(args.version)) options.version = args.version;
else options.type = args.version;

return gulp.src(['./package.json'])
.pipe(bump(options).on('error', gutil.log))
.pipe(gulp.dest('./'));
Expand All @@ -45,6 +45,7 @@ gulp.task('version-push-tags', function (cb) {

gulp.task('version', function (callback) {
runSequence(
'postpublish',
'version-bump',
'version-commit',
'version-tag',
Expand Down
3 changes: 3 additions & 0 deletions dist/lib/Aggregate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/lib/Aggregate.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/lib/AggregationPipeline.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/lib/AggregationPipeline.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/lib/Model.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/Model.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/Aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../_references.d.ts" />

export interface Stage {

}
52 changes: 31 additions & 21 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ModelHandlers from './ModelHandlers';
import * as ModelInterfaces from './ModelInterfaces';
import ModelSpecificInstance from './ModelSpecificInstance';
import InstanceImplementation from './InstanceInterface';
import * as AggregationPipeline from './Aggregate';

/**
* An Iridium Model which represents a structured MongoDB collection
Expand All @@ -45,14 +46,14 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
if (typeof instanceType != 'function') throw new Error("You failed to provide a valid instance constructor for this model");
if (typeof instanceType.collection != 'string' || !instanceType.collection) throw new Error("You failed to provide a valid collection name for this model");
if (!_.isPlainObject(instanceType.schema) || instanceType.schema._id === undefined) throw new Error("You failed to provide a valid schema for this model");

this._core = core;

this.loadExternal(instanceType);
this.onNewModel();
this.loadInternal();
}

private loadExternal(instanceType: InstanceImplementation<TDocument, TInstance>) {
this._collection = instanceType.collection;
this._schema = instanceType.schema;
Expand All @@ -61,9 +62,9 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
this._transforms = instanceType.transforms || {};
this._validators = instanceType.validators || [];
this._indexes = instanceType.indexes || [];

if(!this._schema._id) this._schema._id = MongoDB.ObjectID;

if(this._schema._id === MongoDB.ObjectID && !this._transforms['_id'])
this._transforms['_id'] = {
fromDB: value => value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value,
Expand All @@ -75,13 +76,13 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
else
this._Instance = instanceType.bind(undefined, this);
}

private loadInternal() {
this._cache = new ModelCache(this);
this._helpers = new ModelHelpers(this);
this._handlers = new ModelHandlers(this);
}

private onNewModel() {
this._core.plugins.forEach(plugin => plugin.newModel && plugin.newModel(this));
}
Expand All @@ -103,9 +104,9 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
get handlers(): ModelHandlers<TDocument, TInstance> {
return this._handlers;
}

private _hooks: Hooks<TDocument, TInstance> = {};

/**
* Gets the even hooks subscribed on this model for a number of different state changes
* @returns {Hooks}
Expand Down Expand Up @@ -144,7 +145,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
if (!this.core.connection) throw new Error("Iridium Core not connected to a database.");
return this.core.connection.collection(this._collection);
}

/**
* Gets the name of the underlying MongoDB collection from which this model's documents are retrieved
* @public
Expand Down Expand Up @@ -189,25 +190,25 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
get Instance(): ModelInterfaces.ModelSpecificInstanceConstructor<TDocument, TInstance> {
return this._Instance;
}

private _transforms: { [property: string]: { fromDB: (value: any) => any; toDB: (value: any) => any; } };

get transforms() {
return this._transforms;
}

private _validators: Skmatc.Validator[];

get validators() {
return this._validators;
}

private _indexes: (Index.Index | Index.IndexSpecification)[];

get indexes() {
return this._indexes;
}

/**
* Retrieves all documents in the collection and wraps them as instances
* @param {function(Error, TInstance[])} callback An optional callback which will be triggered when results are available
Expand Down Expand Up @@ -497,7 +498,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
callback = <General.Callback<number>>options;
options = {};
}

options = options || {};

if (!_.isPlainObject(conditions)) conditions = {
Expand Down Expand Up @@ -587,12 +588,12 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
remove(conditions: { _id?: any, [key: string]: any }, options: ModelOptions.RemoveOptions, callback?: General.Callback<number>): Bluebird<number>;
remove(conds?: any, options?: ModelOptions.RemoveOptions, callback?: General.Callback<number>): Bluebird<number> {
var conditions: { _id?: any, [key: string]: any } = <{ _id?: any, [key: string]: any }>conds;

if (typeof options === 'function') {
callback = <General.Callback<number>>options;
options = {};
}

if (typeof conds == 'function') {
callback = <General.Callback<number>>conds;
options = {};
Expand All @@ -601,7 +602,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {

conditions = conditions || {};
options = options || {};

_.defaults(options, {
w: 'majority'
});
Expand All @@ -625,6 +626,15 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
}).nodeify(callback);
}

aggregate<T>(pipeline: AggregationPipeline.Stage[]): Bluebird<T[]> {
return new Bluebird<T[]>((resolve, reject) => {
this.collection.aggregate(pipeline, (err, results) => {
if(err) return reject(err);
return resolve(results);
});
});
}

/**
* Ensures that the given index is created for the collection
* @param {Object} specification The index specification object used by MongoDB
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": "5.8.0",
"version": "5.9.0",
"author": "Benjamin Pannell <admin@sierrasoftworks.com>",
"description": "A custom lightweight ORM for MongoDB designed for power-users",
"license": "MIT",
Expand Down
63 changes: 63 additions & 0 deletions test/Aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// <reference path="../_references.d.ts" />
import * as Iridium from '../index';
import MongoDB = require('mongodb');
import Cursor from '../lib/Cursor';
import Promise = require('bluebird');
import _ = require('lodash');

interface TestDocument {
_id?: string;
group: string;
score: number;
}

class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument {
static collection = 'test';
static schema: Iridium.Schema = {
_id: MongoDB.ObjectID,
group: String,
score: Number
};

_id: string;
group: string;
score: number;
}

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

before(() => core.connect());


describe("aggregate()", () => {
let model = new Iridium.Model<TestDocument, Test>(core, Test);

beforeEach(() => {
return core.connect().then(() => model.remove()).then(() => model.insert([
{ group: 'A', score: 10 },
{ group: 'B', score: 11 },
{ group: 'C', score: 12 },
{ group: 'A', score: 13 },
{ group: 'B', score: 14 }
]));
});

it("should correctly pass through the aggregation pipeline", () => {
return chai.expect(model.aggregate([
{ $group: { _id: '$group', score: { $sum: "$score" } } }
])).to.eventually.exist.and.have.length(3);
});

it("should allow you to specify the type of the resulting documents", () => {
return model.aggregate<{ _id: string; score: number; }>([
{ $match: { group: 'A' } },
{ $group: { _id: '$group', score: { $sum: "$score" } } }
]).then(results => {
chai.expect(results).to.exist.and.have.length(1);
chai.expect(results[0]._id).to.eql('A');
chai.expect(results[0].score).to.eql(23);
});
});
});
});

0 comments on commit ba70443

Please sign in to comment.