diff --git a/lib/Cursor.js b/lib/Cursor.js index 5a2e057..af2c854 100644 --- a/lib/Cursor.js +++ b/lib/Cursor.js @@ -8,6 +8,7 @@ var Cursor = (function () { /** * Counts the number of documents which are matched by this cursor * @param {function(Error, Number)} callback A callback which is triggered when the result is available + * @return {Promise} A promise which will resolve with the number of documents matched by this cursor */ Cursor.prototype.count = function (callback) { var _this = this; @@ -23,6 +24,7 @@ var Cursor = (function () { * Runs the specified handler over each instance in the query results * @param {function(Instance)} handler The handler which is triggered for each element in the query * @param {function(Error)} callback A callback which is triggered when all operations have been dispatched + * @return {Promise} A promise which is resolved when all operations have been dispatched */ Cursor.prototype.each = function (handler, callback) { var _this = this; @@ -36,7 +38,13 @@ var Cursor = (function () { }); }).nodeify(callback); }; - Cursor.prototype.map = function (handler, callback) { + /** + * Runs the specified transform over each instance in the query results and returns the resulting transformed objects + * @param {function(Instance): TResult} transform A handler which is used to transform the result objects + * @param {function(Error, TResult[])} callback A callback which is triggered when the transformations are completed + * @return {Promise} A promise which is fulfilled with the results of the transformations + */ + Cursor.prototype.map = function (transform, callback) { var _this = this; return new Promise(function (resolve, reject) { var promises = []; @@ -45,10 +53,15 @@ var Cursor = (function () { return reject(err); if (!item) return resolve(Promise.all(promises)); - promises.push(_this.model.handlers.documentReceived(_this.conditions, item, function (document, isNew, isPartial) { return _this.model.helpers.wrapDocument(document, isNew, isPartial); }).then(handler)); + promises.push(_this.model.handlers.documentReceived(_this.conditions, item, function (document, isNew, isPartial) { return _this.model.helpers.wrapDocument(document, isNew, isPartial); }).then(transform)); }); }).nodeify(callback); }; + /** + * Retrieves all matching instances and returns them in an array + * @param {function(Error, TInstance[])} callback A callback which is triggered with the resulting instances + * @return {Promise} A promise which resolves with the instances returned by the query + */ Cursor.prototype.toArray = function (callback) { var _this = this; return new Promise(function (resolve, reject) { @@ -61,6 +74,11 @@ var Cursor = (function () { return _this.model.handlers.documentReceived(_this.conditions, document, function (document, isNew, isPartial) { return _this.model.helpers.wrapDocument(document, isNew, isPartial); }); }).nodeify(callback); }; + /** + * Retrieves the next item in the results list + * @param {function(Error, TInstance)} callback A callback which is triggered when the next item becomes available + * @return {Promise} A promise which is resolved with the next item + */ Cursor.prototype.next = function (callback) { var _this = this; return new Promise(function (resolve, reject) { @@ -73,17 +91,37 @@ var Cursor = (function () { return _this.model.handlers.documentReceived(_this.conditions, document, function (document, isNew, isPartial) { return _this.model.helpers.wrapDocument(document, isNew, isPartial); }); }).nodeify(callback); }; + /** + * Returns a new cursor which behaves the same as this one did before any results were retrieved + * @return {Cursor} The new cursor which starts at the beginning of the results + */ Cursor.prototype.rewind = function () { return new Cursor(this.model, this.conditions, this.cursor.rewind()); }; + /** + * Returns a new cursor which sorts its results by the given index expression + * @param {model.IndexSpecification} sortExpression The index expression dictating the sort order and direction to use + * @return {Cursor} The new cursor which sorts its results by the sortExpression + */ Cursor.prototype.sort = function (sortExpression) { return new Cursor(this.model, this.conditions, this.cursor.sort(sortExpression)); }; - Cursor.prototype.limit = function (number) { - return new Cursor(this.model, this.conditions, this.cursor.limit(number)); + /** + * Returns a new cursor which limits the number of returned results + * @param {Number} limit The maximum number of results to return + * @return {Cursor} The new cursor which will return a maximum number of results + */ + Cursor.prototype.limit = function (limit) { + return new Cursor(this.model, this.conditions, this.cursor.limit(limit)); }; - Cursor.prototype.skip = function (number) { - return new Cursor(this.model, this.conditions, this.cursor.skip(number)); + /** + * Returns a new cursor which skips a number of results before it begins + * returning any. + * @param {Number} skip The number of results to skip before the cursor beings returning + * @return {Cursor} The new cursor which skips a number of results + */ + Cursor.prototype.skip = function (skip) { + return new Cursor(this.model, this.conditions, this.cursor.skip(skip)); }; return Cursor; })(); diff --git a/lib/Cursor.js.map b/lib/Cursor.js.map index 8960503..8500451 100644 --- a/lib/Cursor.js.map +++ b/lib/Cursor.js.map @@ -1 +1 @@ -{"version":3,"file":"Cursor.js","sourceRoot":"","sources":["Cursor.ts"],"names":["Cursor","Cursor.constructor","Cursor.count","Cursor.each","Cursor.map","Cursor.toArray","Cursor.next","Cursor.rewind","Cursor.sort","Cursor.limit","Cursor.skip"],"mappings":"AAIA,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AAIrC,IAAM,MAAM;IACRA,SADEA,MAAMA,CACYA,KAAwCA,EAAUA,UAAeA,EAAUA,MAAsBA;QAAjGC,UAAKA,GAALA,KAAKA,CAAmCA;QAAUA,eAAUA,GAAVA,UAAUA,CAAKA;QAAUA,WAAMA,GAANA,MAAMA,CAAgBA;IAErHA,CAACA;IAEDD;;;OAGGA;IACHA,sBAAKA,GAALA,UAAMA,QAAmCA;QAAzCE,iBAOCA;QANGA,MAAMA,CAACA,IAAIA,OAAOA,CAASA,UAACA,OAAOA,EAAEA,MAAMA;YACvCA,KAAIA,CAACA,MAAMA,CAACA,KAAKA,CAACA,IAAIA,EAACA,UAACA,GAAGA,EAAEA,KAAKA;gBAC9BA,EAAEA,CAACA,CAACA,GAAGA,CAACA;oBAACA,MAAMA,CAACA,MAAMA,CAACA,GAAGA,CAACA,CAACA;gBAC5BA,MAAMA,CAACA,OAAOA,CAAMA,KAAKA,CAACA,CAACA;YAC/BA,CAACA,CAACA,CAACA;QACPA,CAACA,CAACA,CAACA,OAAOA,CAACA,QAAQA,CAACA,CAACA;IACzBA,CAACA;IAEDF;;;;OAIGA;IACHA,qBAAIA,GAAJA,UAAKA,OAAsCA,EAAEA,QAAiCA;QAA9EG,iBAQCA;QAPGA,MAAMA,CAACA,IAAIA,OAAOA,CAAOA,UAACA,OAAOA,EAAEA,MAAMA;YACrCA,KAAIA,CAACA,MAAMA,CAACA,IAAIA,CAACA,UAACA,GAAGA,EAAEA,IAAeA;gBAClCA,EAAEA,CAACA,CAACA,GAAGA,CAACA;oBAACA,MAAMA,CAACA,MAAMA,CAACA,GAAGA,CAACA,CAACA;gBAC5BA,EAAEA,CAACA,CAACA,CAACA,IAAIA,CAACA;oBAACA,MAAMA,CAACA,OAAOA,CAACA,IAAIA,CAACA,CAACA;gBAChCA,KAAIA,CA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MAAMA,CAACA,CAACA,CAACA;IAC7EA,CAACA;IACLV,aAACA;AAADA,CAACA,AAlFD,IAkFC;AApFD,iBAAS,MAAM,CAAC"} \ No newline at end of file +{"version":3,"file":"Cursor.js","sourceRoot":"","sources":["Cursor.ts"],"names":["Cursor","Cursor.constructor","Cursor.count","Cursor.each","Cursor.map","Cursor.toArray","Cursor.next","Cursor.rewind","Cursor.sort","Cursor.limit","Cursor.skip"],"mappings":"AAIA,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AAIrC,IAAM,MAAM;IACRA,SADEA,MAAMA,CACYA,KAAwCA,EAAUA,UAAeA,EAAUA,MAAsBA;QAAjGC,UAAKA,GAALA,KAAKA,CAAmCA;QAAUA,eAAUA,GAAVA,UAAUA,CAAKA;QAAUA,WAAMA,GAANA,MAAMA,CAAgBA;IAErHA,CAACA;IAEDD;;;;OAIGA;IACHA,sBAAKA,GAALA,UAAMA,QAAmCA;QAAzCE,iBAOCA;QANGA,MAAMA,CAACA,IAAIA,OAAOA,CAASA,UAACA,OAAOA,EAAEA,MAAMA;YACvCA,KAAIA,CAACA,MAAMA,CAACA,KAAKA,CAACA,IAAIA,EAACA,UAACA,GAAGA,EAAEA,KAAKA;gBAC9BA,EAAEA,CAACA,CAACA,GAAGA,CAACA;oBAACA,MAAMA,CAACA,MAAMA,CAACA,GAAGA,CAACA,CAACA;gBAC5BA,MAAMA,CAACA,OAAOA,CAAMA,KAAKA,CAACA,CAACA;YAC/BA,CAACA,CAACA,CAACA;QACPA,CAACA,CAACA,CAACA,OAAOA,CAACA,QAAQA,CAACA,CAACA;IACzBA,CAACA;IAEDF;;;;;OAKGA;IACHA,qBAAIA,GAAJA,UAAKA,OAAsCA,EAAEA,QAAiCA;QAA9EG,iBAQCA;QAPGA,MAAMA,CAACA,IAAIA,OAAOA,CAAOA,UAACA,OAAOA,EAAEA,MAAMA;YACrCA,KAAIA,CAACA,MAAMA,CAACA,IAAIA,CAACA,UAACA,GAAGA,EAAEA,IAAeA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AACA,IAAIA,CAACA,KAAKA,EAAEA,IAAIA,CAACA,UAAUA,EAAEA,IAAIA,CAACA,MAAMA,CAACA,KAAKA,CAACA,KAAKA,CAACA,CAACA,CAACA;IAC7EA,CAACA;IAEDT;;;;;OAKGA;IACHA,qBAAIA,GAAJA,UAAKA,IAAYA;QACbU,MAAMA,CAACA,IAAIA,MAAMA,CAACA,IAAIA,CAACA,KAAKA,EAAEA,IAAIA,CAACA,UAAUA,EAAEA,IAAIA,CAACA,MAAMA,CAACA,IAAIA,CAACA,IAAIA,CAACA,CAACA,CAACA;IAC3EA,CAACA;IACLV,aAACA;AAADA,CAACA,AAxHD,IAwHC;AA1HD,iBAAS,MAAM,CAAC"} \ No newline at end of file diff --git a/lib/Cursor.ts b/lib/Cursor.ts index ee21a71..e25ce91 100644 --- a/lib/Cursor.ts +++ b/lib/Cursor.ts @@ -14,6 +14,7 @@ class Cursor { /** * Counts the number of documents which are matched by this cursor * @param {function(Error, Number)} callback A callback which is triggered when the result is available + * @return {Promise} A promise which will resolve with the number of documents matched by this cursor */ count(callback?: general.Callback): Promise { return new Promise((resolve, reject) => { @@ -28,6 +29,7 @@ class Cursor { * Runs the specified handler over each instance in the query results * @param {function(Instance)} handler The handler which is triggered for each element in the query * @param {function(Error)} callback A callback which is triggered when all operations have been dispatched + * @return {Promise} A promise which is resolved when all operations have been dispatched */ each(handler: (instance: TInstance) => void, callback?: general.Callback): Promise { return new Promise((resolve, reject) => { @@ -39,18 +41,29 @@ class Cursor { }).nodeify(callback); } - map(handler: (instance: TInstance) => TResult | Promise, callback?: general.Callback): Promise { + /** + * Runs the specified transform over each instance in the query results and returns the resulting transformed objects + * @param {function(Instance): TResult} transform A handler which is used to transform the result objects + * @param {function(Error, TResult[])} callback A callback which is triggered when the transformations are completed + * @return {Promise} A promise which is fulfilled with the results of the transformations + */ + map(transform: (instance: TInstance) => TResult | Promise, callback?: general.Callback): Promise { return new Promise((resolve, reject) => { var promises: Promise[] = []; this.cursor.each((err, item: TDocument) => { if (err) return reject(err); if (!item) return resolve(Promise.all(promises)); promises.push(this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)) - .then(<(instance) => TResult>handler)); + .then(<(instance) => TResult>transform)); }); }).nodeify(callback); } + /** + * Retrieves all matching instances and returns them in an array + * @param {function(Error, TInstance[])} callback A callback which is triggered with the resulting instances + * @return {Promise} A promise which resolves with the instances returned by the query + */ toArray(callback?: general.Callback): Promise { return new Promise((resolve, reject) => { this.cursor.toArray((err, results: any[]) => { @@ -62,6 +75,11 @@ class Cursor { }).nodeify(callback); } + /** + * Retrieves the next item in the results list + * @param {function(Error, TInstance)} callback A callback which is triggered when the next item becomes available + * @return {Promise} A promise which is resolved with the next item + */ next(callback?: general.Callback): Promise { return new Promise((resolve, reject) => { this.cursor.nextObject((err, result: any) => { @@ -73,19 +91,39 @@ class Cursor { }).nodeify(callback); } + /** + * Returns a new cursor which behaves the same as this one did before any results were retrieved + * @return {Cursor} The new cursor which starts at the beginning of the results + */ rewind(): Cursor { return new Cursor(this.model, this.conditions, this.cursor.rewind()); } + /** + * Returns a new cursor which sorts its results by the given index expression + * @param {model.IndexSpecification} sortExpression The index expression dictating the sort order and direction to use + * @return {Cursor} The new cursor which sorts its results by the sortExpression + */ sort(sortExpression: model.IndexSpecification): Cursor { return new Cursor(this.model, this.conditions, this.cursor.sort(sortExpression)); } - limit(number: number): Cursor { - return new Cursor(this.model, this.conditions, this.cursor.limit(number)); + /** + * Returns a new cursor which limits the number of returned results + * @param {Number} limit The maximum number of results to return + * @return {Cursor} The new cursor which will return a maximum number of results + */ + limit(limit: number): Cursor { + return new Cursor(this.model, this.conditions, this.cursor.limit(limit)); } - skip(number: number): Cursor { - return new Cursor(this.model, this.conditions, this.cursor.skip(number)); + /** + * Returns a new cursor which skips a number of results before it begins + * returning any. + * @param {Number} skip The number of results to skip before the cursor beings returning + * @return {Cursor} The new cursor which skips a number of results + */ + skip(skip: number): Cursor { + return new Cursor(this.model, this.conditions, this.cursor.skip(skip)); } } \ No newline at end of file