Skip to content

Commit

Permalink
Improve method definitions for conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 11, 2015
1 parent 0bc1afc commit 42588d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
25 changes: 13 additions & 12 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.

43 changes: 23 additions & 20 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {Object} conditions The MongoDB query dictating which documents to return
* @returns {Promise<TInstance[]>}
*/
find(conditions: any): Cursor<TDocument, TInstance>;
find(conditions: { _id?: any, [key: string]: any } | any): Cursor<TDocument, TInstance>;
/**
* Returns all documents in the collection which match the conditions
* @param {Object} conditions The MongoDB query dictating which documents to return
* @param {Object} fields The fields to include or exclude from the document
* @returns {Promise<TInstance[]>}
*/
find(conditions: any, fields: { [name: string]: number }): Cursor<TDocument, TInstance>;
find(conditions?: any, fields?: any): Cursor<TDocument, TInstance> {
find(conditions: { _id?: any, [key: string]: any } | any, fields: { [name: string]: number }): Cursor<TDocument, TInstance>;
find(conditions?: { _id?: any, [key: string]: any } | any, fields?: any): Cursor<TDocument, TInstance> {
conditions = conditions || {};
fields = fields || {};

Expand Down Expand Up @@ -258,7 +258,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, TInstance)} callback An optional callback which will be triggered when a result is available
* @returns {Promise<TInstance>}
*/
get(conditions: { [key: string]: any }, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
get(conditions: { _id?: any, [key: string]: any }, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
/**
* Retrieves a single document from the collection with the given ID and wraps it as an instance
* @param {any} id The document's unique _id field value in downstream format
Expand All @@ -274,7 +274,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, TInstance)} callback An optional callback which will be triggered when a result is available
* @returns {Promise<TInstance>}
*/
get(conditions: { [key: string]: any }, options: ModelOptions.QueryOptions, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
get(conditions: { _id?: any, [key: string]: any }, options: ModelOptions.QueryOptions, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
get(...args: any[]): Bluebird<TInstance> {
return this.findOne.apply(this, args);
}
Expand All @@ -298,7 +298,7 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, TInstance)} callback An optional callback which will be triggered when a result is available
* @returns {Promise<TInstance>}
*/
findOne(conditions: { [key: string]: any }, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
findOne(conditions: { _id?: any, [key: string]: any }, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
/**
* Retrieves a single document from the collection with the given ID and wraps it as an instance
* @param {any} id The document's unique _id field value in downstream format
Expand All @@ -314,9 +314,9 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, TInstance)} callback An optional callback which will be triggered when a result is available
* @returns {Promise<TInstance>}
*/
findOne(conditions: { [key: string]: any }, options: ModelOptions.QueryOptions, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
findOne(conditions: { _id?: any, [key: string]: any }, options: ModelOptions.QueryOptions, callback?: General.Callback<TInstance>): Bluebird<TInstance>;
findOne(...args: any[]): Bluebird<TInstance> {
var conditions: { [key: string]: any } = null;
var conditions: { _id?: any, [key: string]: any } = null;
var options: ModelOptions.QueryOptions = null;
var callback: General.Callback<TInstance> = null;

Expand Down Expand Up @@ -482,16 +482,16 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {Object} changes The changes to make to the documents
* @param {function(Error, Number)} callback A callback which is triggered when the operation completes
*/
update(conditions: any, changes: any, callback?: General.Callback<number>): Bluebird<number>;
update(conditions: { _id?: any, [key: string]: any } | any, changes: any, callback?: General.Callback<number>): Bluebird<number>;
/**
* Updates the documents in the backing collection which match the conditions using the given update instructions
* @param {Object} conditions The conditions which determine which documents will be updated
* @param {Object} changes The changes to make to the documents
* @param {UpdateOptions} options The options which dictate how this function behaves
* @param {function(Error, Number)} callback A callback which is triggered when the operation completes
*/
update(conditions: any, changes: any, options: ModelOptions.UpdateOptions, callback?: General.Callback<number>): Bluebird<number>;
update(conditions: any, changes: any, options?: ModelOptions.UpdateOptions, callback?: General.Callback<number>): Bluebird<number> {
update(conditions: { _id?: any, [key: string]: any } | any, changes: any, options: ModelOptions.UpdateOptions, callback?: General.Callback<number>): Bluebird<number>;
update(conditions: { _id?: any, [key: string]: any } | any, changes: any, options?: ModelOptions.UpdateOptions, callback?: General.Callback<number>): Bluebird<number> {
if (typeof options == 'function') {
callback = <General.Callback<number>>options;
options = {};
Expand Down Expand Up @@ -538,10 +538,11 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, Number)} callback A callback which is triggered when the operation completes
* @returns {Promise<number>}
*/
count(conditions: any, callback?: General.Callback<number>): Bluebird<number>;
count(conditions?: any, callback?: General.Callback<number>): Bluebird<number> {
if (typeof conditions == 'function') {
callback = <General.Callback<number>>conditions;
count(conditions: { _id?: any, [key: string]: any } | any, callback?: General.Callback<number>): Bluebird<number>;
count(conds?: any, callback?: General.Callback<number>): Bluebird<number> {
var conditions: { _id?: any, [key: string]: any } = <{ _id?: any, [key: string]: any }>conds;
if (typeof conds == 'function') {
callback = <General.Callback<number>>conds;
conditions = {};
}

Expand Down Expand Up @@ -576,23 +577,25 @@ export default class Model<TDocument extends { _id?: any }, TInstance> implement
* @param {function(Error, Number)} callback A callback which is triggered when the operation completes
* @returns {Promise<number>}
*/
remove(conditions: any, callback?: General.Callback<number>): Bluebird<number>;
remove(conditions: { _id?: any, [key: string]: any } | any, callback?: General.Callback<number>): Bluebird<number>;
/**
* Removes all documents from the collection which match the conditions
* @param {Object} conditions The conditions determining whether an object is removed or not
* @param {Object} options The options controlling the way in which the function behaves
* @param {function(Error, Number)} callback A callback which is triggered when the operation completes
* @returns {Promise<number>}
*/
remove(conditions: any, options: ModelOptions.RemoveOptions, callback?: General.Callback<number>): Bluebird<number>;
remove(conditions?: any, options?: ModelOptions.RemoveOptions, callback?: General.Callback<number>): Bluebird<number> {
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 conditions === 'function') {
callback = <General.Callback<number>>conditions;
if (typeof conds == 'function') {
callback = <General.Callback<number>>conds;
options = {};
conditions = {};
}
Expand Down

0 comments on commit 42588d2

Please sign in to comment.