-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated find() to use an Iridium cursor object
- Loading branch information
1 parent
6adc765
commit 6ddcbbd
Showing
12 changed files
with
300 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/// <reference path="../_references.d.ts" /> | ||
import model = require('./Model'); | ||
import general = require('./General'); | ||
import MongoDB = require('mongodb'); | ||
import Promise = require('bluebird'); | ||
|
||
export = Cursor; | ||
|
||
class Cursor<TDocument, TInstance> { | ||
constructor(private model: model.Model<TDocument, TInstance>, private conditions: any, private cursor: MongoDB.Cursor) { | ||
|
||
} | ||
|
||
count(callback?: general.Callback<number>): Promise<number> { | ||
return new Promise<number>((resolve, reject) => { | ||
this.cursor.count(true,(err, count) => { | ||
if (err) return reject(err); | ||
return resolve(<any>count); | ||
}); | ||
}).nodeify(callback); | ||
} | ||
|
||
each(handler: (instance: TInstance) => void, callback?: general.Callback<void>): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
var promises = []; | ||
this.cursor.each((err, item: TDocument) => { | ||
if (err) return reject(err); | ||
if (!item) return resolve(Promise.all(promises).then(() => null)); | ||
promises.push(this.model.handlers.documentReceived(this.conditions, item,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)).then(handler)); | ||
}); | ||
}).nodeify(callback); | ||
} | ||
|
||
map<TResult>(handler: (instance: TInstance) => TResult | Promise<TResult>, callback?: general.Callback<TResult[]>): Promise<TResult[]> { | ||
return new Promise<TResult[]>((resolve, reject) => { | ||
var promises: Promise<TResult>[] = []; | ||
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)); | ||
}); | ||
}).nodeify(callback); | ||
} | ||
|
||
toArray(callback?: general.Callback<TInstance[]>): Promise<TInstance[]> { | ||
return new Promise<TDocument[]>((resolve, reject) => { | ||
this.cursor.toArray((err, results: any[]) => { | ||
if (err) return reject(err); | ||
return resolve(<any>results); | ||
}); | ||
}).map<TDocument, TInstance>((document) => { | ||
return this.model.handlers.documentReceived(this.conditions, document,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)); | ||
}).nodeify(callback); | ||
} | ||
|
||
next(callback?: general.Callback<TInstance>): Promise<TInstance> { | ||
return new Promise<TDocument>((resolve, reject) => { | ||
this.cursor.nextObject((err, result: any) => { | ||
if (err) return reject(err); | ||
return resolve(<any>result); | ||
}); | ||
}).then((document) => { | ||
return this.model.handlers.documentReceived(this.conditions, document,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)); | ||
}).nodeify(callback); | ||
} | ||
|
||
rewind(): Cursor<TDocument, TInstance> { | ||
return new Cursor(this.model, this.conditions, this.cursor.rewind()); | ||
} | ||
|
||
sort(sortExpression: model.IndexSpecification): Cursor<TDocument, TInstance> { | ||
return new Cursor(this.model, this.conditions, this.cursor.sort(sortExpression)); | ||
} | ||
|
||
limit(number: number): Cursor<TDocument, TInstance> { | ||
return new Cursor(this.model, this.conditions, this.cursor.limit(number)); | ||
} | ||
|
||
skip(number: number): Cursor<TDocument, TInstance> { | ||
return new Cursor(this.model, this.conditions, this.cursor.skip(number)); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.