Merge pull request #13055 from simonxca/patch-mongoose-types-2.0

mongoose updates types-2.0
This commit is contained in:
Horiuchi_H
2016-12-02 15:16:46 +09:00
committed by GitHub
2 changed files with 45 additions and 6 deletions

26
mongoose/index.d.ts vendored
View File

@@ -1,4 +1,4 @@
// Type definitions for Mongoose 4.6.8
// Type definitions for Mongoose 4.7.0
// Project: http://mongoosejs.com/
// Definitions by: simonxca <https://github.com/simonxca/>, horiuchi <https://github.com/horiuchi/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -496,11 +496,17 @@ declare module "mongoose" {
*/
eachAsync(fn: (doc: T) => any, callback?: (err: any) => void): Promise<T>;
/**
* Registers a transform function which subsequently maps documents retrieved
* via the streams interface or .next()
*/
map(fn: (doc: T) => T): this;
/**
* Get the next document from this cursor. Will return null when there are
* no documents left.
*/
next(callback?: (err: any) => void): Promise<any>;
next(callback?: (err: any, doc?: T) => void): Promise<any>;
}
/*
@@ -563,6 +569,12 @@ declare module "mongoose" {
/** Compiles indexes from fields and schema-level indexes */
indexes(): any[];
/**
* Loads an ES6 class into a schema. Maps setters + getters, static methods, and
* instance methods to schema virtuals, statics, and methods.
*/
loadClass(model: Function): this;
/**
* Adds an instance method to documents constructed from Models compiled from this schema.
* If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as methods.
@@ -597,9 +609,13 @@ declare module "mongoose" {
* @param method name of the method to hook
* @param fn callback
*/
post<T extends Document>(method: string, fn: (doc: T, next: (err?: NativeError) => void,
...otherArgs: any[]) => void): this;
post<T extends Document>(method: string, fn: (doc: T) => void, ...args: any[]): this;
post<T extends Document>(method: string, fn: (
error: mongodb.MongoError, doc: T, next: (err?: NativeError) => void
) => void): this;
post<T extends Document>(method: string, fn: (
doc: T, next: (err?: NativeError) => void
) => void): this;
/**
* Defines a pre hook for the document.

View File

@@ -193,6 +193,18 @@ QCModel.find({}).cursor({}).on('data', function (doc: any) {
}).on('error', function (error: any) {
throw error;
}).close().then(cb).catch(cb);
querycursor.map(function (doc) {
doc.foo = "bar";
return doc;
}).on('data', function (doc: any) {
console.log(doc.foo);
});
querycursor.map(function (doc) {
doc.foo = "bar";
return doc;
}).next(function (error, doc) {
console.log(doc.foo);
});
/*
* section virtualtype.js
@@ -254,8 +266,19 @@ schema.plugin(function (schema, opts) {
schema.get('path');
opts.hasOwnProperty('');
}).plugin(cb, {opts: true});
schema.post('post', function (doc) {}).post('post', function (doc, next) {
import { Schema, Model, Document, NativeError } from 'mongoose';
schema
.post('save', function (error, doc, next) {
error.stack;
doc.model;
next.apply;
})
.post('save', function (doc: mongoose.Document, next: Function) {
doc.model;
next(new Error());
})
.post('save', function (doc: mongoose.Document) {
doc.model;
});
schema.queue('m1', [1, 2, 3]).queue('m2', [[]]);
schema.remove('path');