diff --git a/mongoose/mongoose-tests.ts b/mongoose/mongoose-tests.ts index ddec97e9d1..d846023143 100644 --- a/mongoose/mongoose-tests.ts +++ b/mongoose/mongoose-tests.ts @@ -27,7 +27,7 @@ mongoose.connect(connectUri, { autoIndex: true }, mongos: true -}).then(cb).fulfill(); +}).then(cb); mongoose.connect(connectUri, function (error) { error.stack; }); @@ -45,7 +45,7 @@ mongoose.createConnection('localhost', 'database', 3000, { autoIndex: false } }).open(''); -mongoose.disconnect(cb).then(cb).fulfill; +mongoose.disconnect(cb).then(cb); mongoose.get('test'); mongoose.model('Actor', new mongoose.Schema({ name: String @@ -540,7 +540,7 @@ query.where('loc').within().box(lowerLeft, upperRight) query.box({ ll : lowerLeft, ur : upperRight }).box({}); var queryModel = mongoose.model('QModel') query.cast(new queryModel(), {}).hasOwnProperty(''); -query.catch(function (err) {}).catch(); +query.catch(cb).catch(cb); query.center({}).center({}); query.centerSphere({ center: [50, 50], radius: 10 }).centerSphere('path', {}); query.circle({ center: [50, 50], radius: 10 }).circle('path'); @@ -695,7 +695,7 @@ query.stream().on('data', function (doc: any) { }); query.tailable().tailable(false); query.then(cb).catch(cb); -(new (query.toConstructor())()).toConstructor(); +(new (query.toConstructor())(1, 2, 3)).toConstructor(); query.update({}, doc, { }, cb); @@ -1060,12 +1060,13 @@ mongoose.model('').aggregate() }); /* pluggable promise */ -mongoose.Promise = Promise; +(mongoose).Promise = Promise; +require('mongoose').Promise = Promise; mongoose.Promise.race; mongoose.Promise.all; mongoose.model('').findOne() - .exec().addErrback(cb); + .exec().then(cb); /* * section model.js @@ -1377,5 +1378,5 @@ export var Final: MyModel = mongoose.connection.model('Fina Final.findOne(function (err: any, doc: MyDocument) { doc.save(); doc.remove(); - doc.model(null, null); + doc.model(''); }); \ No newline at end of file diff --git a/mongoose/mongoose.d.ts b/mongoose/mongoose.d.ts index a94644393e..b28a222778 100644 --- a/mongoose/mongoose.d.ts +++ b/mongoose/mongoose.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Mongoose 4.5.4 +// Type definitions for Mongoose 4.5.9 // Project: http://mongoosejs.com/ // Definitions by: simonxca , horiuchi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,7 +6,6 @@ /// /// /// -/// /* * Guidelines for maintaining these definitions: @@ -93,7 +92,6 @@ declare module "mongoose" { * http://mongoosejs.com/docs/api.html#index-js */ export var DocumentProvider: any; - export var Model: Model; // recursive constructor export var Mongoose: new(...args: any[]) => typeof mongoose; export var SchemaTypes: typeof Schema.Types; @@ -107,7 +105,6 @@ declare module "mongoose" { /** The Mongoose version */ export var version: string; - /* Methods */ /** * Opens the default mongoose connection. * Options passed take precedence over options included in connection strings. @@ -173,20 +170,19 @@ declare module "mongoose" { /** Sets mongoose options */ export function set(key: string, value: any): void; - type MongooseThenable = typeof mongoose & _MongooseThenable; - interface _MongooseThenable { + type MongooseThenable = typeof mongoose & { /** * Ability to use mongoose object as a pseudo-promise so .connect().then() * and .disconnect().then() are viable. */ then(onFulfill?: () => void | TRes | PromiseLike, - onRejected?: (err: mongodb.MongoError) => void | TRes | PromiseLike): _MongoosePromise; + onRejected?: (err: mongodb.MongoError) => void | TRes | PromiseLike): Promise; /** * Ability to use mongoose object as a pseudo-promise so .connect().then() * and .disconnect().then() are viable. */ - catch(onRejected?: (err: mongodb.MongoError) => void | TRes | PromiseLike): _MongoosePromise; + catch(onRejected?: (err: mongodb.MongoError) => void | TRes | PromiseLike): Promise; } class CastError extends Error { @@ -205,7 +201,7 @@ declare module "mongoose" { * http://mongoosejs.com/docs/api.html#querystream-js * * QueryStream can only be accessed using query#stream(), we only - * expose its interface here to enable type-checking. + * expose its interface here. */ interface QueryStream extends stream.Stream { /** @@ -293,7 +289,7 @@ declare module "mongoose" { callback?: (err: any) => void): any; /** Closes the connection */ - close(callback?: (err: any) => void): _MongoosePromise; + close(callback?: (err: any) => void): Promise; /** * Retrieves a collection, creating it if not cached. @@ -375,6 +371,10 @@ declare module "mongoose" { mongos?: boolean; } + interface ConnectOptions extends + ConnectionOpenOptions, + ConnectionOpenSetOptions {} + /* * section drivers/node-mongodb-native/collection.js * http://mongoosejs.com/docs/api.html#drivers-node-mongodb-native-collection-js @@ -412,8 +412,6 @@ declare module "mongoose" { static STATES: Object; } - interface ConnectOptions extends ConnectionOpenOptions, ConnectionOpenSetOptions {} - /* * section error/validation.js * http://mongoosejs.com/docs/api.html#error-validation-js @@ -474,7 +472,7 @@ declare module "mongoose" { constructor(query: Query, options: Object): QueryCursor; /** Marks this cursor as closed. Will stop streaming and subsequent calls to next() will error. */ - close(callback?: (error: any, result: any) => void): _MongoosePromise; + close(callback?: (error: any, result: any) => void): Promise; /** * Execute fn for every document in the cursor. If fn returns a promise, @@ -482,13 +480,13 @@ declare module "mongoose" { * Returns a promise that resolves when done. * @param callback executed when all docs have been processed */ - eachAsync(fn: (doc: T) => any, callback?: (err: any) => void): _MongoosePromise; + eachAsync(fn: (doc: T) => any, callback?: (err: any) => void): Promise; /** * Get the next document from this cursor. Will return null when there are * no documents left. */ - next(callback?: (err: any) => void): _MongoosePromise; + next(callback?: (err: any) => void): Promise; } /* @@ -729,7 +727,7 @@ declare module "mongoose" { * Useful for ES2015 integration. * @returns promise that resolves to the document when population is done */ - execPopulate(): _MongoosePromise; + execPopulate(): Promise; /** * Returns the value of a path. @@ -848,8 +846,8 @@ declare module "mongoose" { * @param optional options internal options * @param callback callback called after validation completes, passing an error if one occurred */ - validate(callback?: (err: any) => void): _MongoosePromise; - validate(optional: Object, callback?: (err: any) => void): _MongoosePromise; + validate(callback?: (err: any) => void): Promise; + validate(optional: Object, callback?: (err: any) => void): Promise; /** * Executes registered validation rules (skipping asynchronous validators) for this document. @@ -915,9 +913,9 @@ declare module "mongoose" { } /* - * section types/array.js - * http://mongoosejs.com/docs/api.html#types-array-js - */ + * section types/array.js + * http://mongoosejs.com/docs/api.html#types-array-js + */ class Array extends global.Array { /** * Atomically shifts the array at most one time per document save(). @@ -1053,9 +1051,9 @@ declare module "mongoose" { } /* - * section types/buffer.js - * http://mongoosejs.com/docs/api.html#types-buffer-js - */ + * section types/buffer.js + * http://mongoosejs.com/docs/api.html#types-buffer-js + */ class Buffer extends global.Buffer { /** * Copies the buffer. @@ -1082,8 +1080,7 @@ declare module "mongoose" { * section types/objectid.js * http://mongoosejs.com/docs/api.html#types-objectid-js */ - var ObjectId: typeof mongodb.ObjectID; - interface ObjectId extends mongodb.ObjectID {} + class ObjectId extends mongodb.ObjectID {} /* * section types/embedded.js @@ -1123,13 +1120,13 @@ declare module "mongoose" { /* * section query.js * http://mongoosejs.com/docs/api.html#query-js + * + * Query is for backwards compatibility. Example: Query.find() returns Query. + * If later in the query chain a method returns Query, we will need to know type T. + * So we save this type as the second type parameter in DocumentQuery. Since people have + * been using Query, we set it as an alias of DocumentQuery. */ class Query extends DocumentQuery {} - - /* - * Query.find() will return Query[]> however we need the - * type T to create this so we save T in another parameter. - */ class DocumentQuery extends mquery { /** * Specifies a javascript function or expression to pass to MongoDBs query system. @@ -1170,7 +1167,7 @@ declare module "mongoose" { * resolved with either the doc(s) or rejected with the error. * Like .then(), but only takes a rejection handler. */ - catch(reject?: (err: any) => void | TRes | PromiseLike): _MongoosePromise; + catch(reject?: (err: any) => void | TRes | PromiseLike): Promise; /** * DEPRECATED Alias for circle @@ -1223,8 +1220,8 @@ declare module "mongoose" { equals(val: Object): this; /** Executes the query */ - exec(callback?: (err: any, res: T) => void): _MongoosePromise; - exec(operation: string | Function, callback?: (err: any, res: T) => void): _MongoosePromise; + exec(callback?: (err: any, res: T) => void): Promise; + exec(operation: string | Function, callback?: (err: any, res: T) => void): Promise; /** Specifies an $exists condition */ exists(val?: boolean): this; @@ -1515,13 +1512,14 @@ declare module "mongoose" { /** Executes this query and returns a promise */ then(resolve?: (res: T) => void | TRes | PromiseLike, - reject?: (err: any) => void | TRes | PromiseLike): _MongoosePromise; + reject?: (err: any) => void | TRes | PromiseLike): Promise; /** * Converts this query to a customized, reusable query * constructor with all arguments and options retained. */ - toConstructor(): typeof DocumentQuery; + toConstructor(): new(...args: any[]) => Query; + toConstructor(): new(...args: any[]) => DocumentQuery; /** * Declare and/or execute this query as an update() operation. @@ -1858,10 +1856,10 @@ declare module "mongoose" { // If cursor option is on, could return an object /** Executes the aggregate pipeline on the currently bound Model. */ - exec(callback?: (err: any, result: T) => void): _MongoosePromise | any; + exec(callback?: (err: any, result: T) => void): Promise | any; /** Execute the aggregation with explain */ - explain(callback?: (err: any, result: T) => void): _MongoosePromise; + explain(callback?: (err: any, result: T) => void): Promise; /** * Appends a new custom $group operator to this aggregate pipeline. @@ -1936,7 +1934,7 @@ declare module "mongoose" { /** Provides promise for aggregate. */ then(resolve?: (val: T) => void | TRes | PromiseLike, - reject?: (err: any) => void | TRes | PromiseLike): _MongoosePromise + reject?: (err: any) => void | TRes | PromiseLike): Promise /** * Appends new custom $unwind operator(s) to this aggregate pipeline. @@ -2004,34 +2002,37 @@ declare module "mongoose" { type?: string): this; } - /** + /* * section promise.js * http://mongoosejs.com/docs/api.html#promise-js - * - * You must assign a promise library: - * - * 1. To use mongoose's default promise library: - * Install mongoose-promise.d.ts - * - * 2. To use native ES6 promises, add this line to your main .d.ts file: - * type MongoosePromise = Promise; - * - * 3. To use another promise library (for example q): - * Install q.d.ts - * Then add this line to your main .d.ts file: - * type MongoosePromise = Q.Promise; */ - interface _MongoosePromise extends MongoosePromise {} - interface Promise extends MongoosePromise {} - /** * To assign your own promise library: * - * 1. Include this somewhere in your code: - * mongoose.Promise = YOUR_PROMISE; + * 1. Typescript does not allow assigning properties of imported modules. + * To avoid compile errors use one of the options below in your code: * - * 2. Include this somewhere in your main .d.ts file: - * type MongoosePromise = YOUR_PROMISE; + * - (mongoose).Promise = YOUR_PROMISE; + * - require('mongoose').Promise = YOUR_PROMISE; + * - import mongoose = require('mongoose'); + * mongoose.Promise = YOUR_PROMISE; + * + * 2. To assign type definitions for your promise library, you will need + * to have a .d.ts file with the following code when you compile: + * + * - import * as Q from 'q'; + * declare module 'mongoose' { + * type Promise = Q.promise; + * } + * + * - import * as Bluebird from 'bluebird'; + * declare module 'mongoose' { + * type Promise = Bluebird; + * } + * + * Uses global.Promise by default. If you would like to use mongoose default + * mpromise implementation (which is deprecated), you can omit step 1 and + * run npm install @types/mongoose-promise */ export var Promise: any; export var PromiseProvider: any; @@ -2040,6 +2041,7 @@ declare module "mongoose" { * section model.js * http://mongoosejs.com/docs/api.html#model-js */ + export var Model: Model; interface Model extends NodeJS.EventEmitter, ModelProperties { /** * Model constructor @@ -2087,7 +2089,7 @@ declare module "mongoose" { * @param ... aggregation pipeline operator(s) or operator array */ aggregate(...aggregations: Object[]): Aggregate; - aggregate(...aggregationsWithCallback: Object[]): _MongoosePromise; + aggregate(...aggregationsWithCallback: Object[]): Promise; /** Counts number of matching documents in a database collection. */ count(conditions: Object, callback?: (err: any, count: number) => void): Query; @@ -2097,9 +2099,9 @@ declare module "mongoose" { * does new MyModel(doc).save() for every doc in docs. * Triggers the save() hook. */ - create(docs: any[], callback?: (err: any, res: T[]) => void): _MongoosePromise; - create(...docs: Object[]): _MongoosePromise; - create(...docsWithCallback: Object[]): _MongoosePromise; + create(docs: any[], callback?: (err: any, res: T[]) => void): Promise; + create(...docs: Object[]): Promise; + create(...docsWithCallback: Object[]): Promise; /** * Adds a discriminator type. @@ -2118,8 +2120,8 @@ declare module "mongoose" { * @param options internal options * @param cb optional callback */ - ensureIndexes(callback?: (err: any) => void): _MongoosePromise; - ensureIndexes(options: Object, callback?: (err: any) => void): _MongoosePromise; + ensureIndexes(callback?: (err: any) => void): Promise; + ensureIndexes(options: Object, callback?: (err: any) => void): Promise; /** * Finds documents. @@ -2254,9 +2256,9 @@ declare module "mongoose" { * document. * This function does not trigger save middleware. */ - insertMany(docs: any[], callback?: (error: any, docs: T[]) => void): _MongoosePromise; - insertMany(doc: any, callback?: (error: any, doc: T) => void): _MongoosePromise; - insertMany(...docsWithCallback: Object[]): _MongoosePromise; + insertMany(docs: any[], callback?: (error: any, docs: T[]) => void): Promise; + insertMany(doc: any, callback?: (error: any, doc: T) => void): Promise; + insertMany(...docsWithCallback: Object[]): Promise; /** * Executes a mapReduce command. @@ -2266,7 +2268,7 @@ declare module "mongoose" { mapReduce( o: ModelMapReduceOption, callback?: (err: any, res: any) => void - ): _MongoosePromise; + ): Promise; /** * Populates document references. @@ -2275,9 +2277,9 @@ declare module "mongoose" { * @param callback Optional callback, executed upon completion. Receives err and the doc(s). */ populate(docs: Object[], options: ModelPopulateOptions | ModelPopulateOptions[], - callback?: (err: any, res: T[]) => void): _MongoosePromise; + callback?: (err: any, res: T[]) => void): Promise; populate(docs: Object, options: ModelPopulateOptions | ModelPopulateOptions[], - callback?: (err: any, res: T) => void): _MongoosePromise; + callback?: (err: any, res: T) => void): Promise; /** Removes documents from the collection. */ remove(conditions: Object, callback?: (err: any) => void): Query; @@ -2309,7 +2311,7 @@ declare module "mongoose" { * Removes this document from the db. * @param fn optional callback */ - remove(fn?: (err: any, product: this) => void): _MongoosePromise; + remove(fn?: (err: any, product: this) => void): Promise; /** * Saves this document. @@ -2318,7 +2320,7 @@ declare module "mongoose" { * @param options.validateBeforeSave set to false to save without validating. * @param fn optional callback */ - save(fn?: (err: any, product: this, numAffected: number) => void): _MongoosePromise; + save(fn?: (err: any, product: this, numAffected: number) => void): Promise; } interface ModelProperties {