diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 25cf1eb49d..ac92a9cd74 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -1476,33 +1476,33 @@ declare module "mongoose" { * @param criteria mongodb selector * @param projection optional fields to return */ - findOne(callback?: (err: any, res: DocType) => void): DocumentQuery; + findOne(callback?: (err: any, res: DocType | null) => void): DocumentQuery; findOne(criteria: Object, - callback?: (err: any, res: DocType) => void): DocumentQuery; + callback?: (err: any, res: DocType | null) => void): DocumentQuery; /** * Issues a mongodb findAndModify remove command. * Finds a matching document, removes it, passing the found document (if any) to the * callback. Executes immediately if callback is passed. */ - findOneAndRemove(callback?: (error: any, doc: DocType, result: any) => void): DocumentQuery; + findOneAndRemove(callback?: (error: any, doc: DocType | null, result: any) => void): DocumentQuery; findOneAndRemove(conditions: Object, - callback?: (error: any, doc: DocType, result: any) => void): DocumentQuery; + callback?: (error: any, doc: DocType | null, result: any) => void): DocumentQuery; findOneAndRemove(conditions: Object, options: QueryFindOneAndRemoveOptions, - callback?: (error: any, doc: DocType, result: any) => void): DocumentQuery; + callback?: (error: any, doc: DocType | null, result: any) => void): DocumentQuery; /** * Issues a mongodb findAndModify update command. * Finds a matching document, updates it according to the update arg, passing any options, and returns * the found document (if any) to the callback. The query executes immediately if callback is passed. */ - findOneAndUpdate(callback?: (err: any, doc: DocType) => void): DocumentQuery; + findOneAndUpdate(callback?: (err: any, doc: DocType | null) => void): DocumentQuery; findOneAndUpdate(update: Object, - callback?: (err: any, doc: DocType) => void): DocumentQuery; + callback?: (err: any, doc: DocType | null) => void): DocumentQuery; findOneAndUpdate(query: Object | Query, update: Object, - callback?: (err: any, doc: DocType) => void): DocumentQuery; + callback?: (err: any, doc: DocType | null) => void): DocumentQuery; findOneAndUpdate(query: Object | Query, update: Object, options: QueryFindOneAndUpdateOptions, - callback?: (err: any, doc: DocType) => void): DocumentQuery; + callback?: (err: any, doc: DocType | null) => void): DocumentQuery; /** * Specifies a $geometry condition. geometry() must come after either intersects() or within(). @@ -2334,11 +2334,11 @@ declare module "mongoose" { * @param projection optional fields to return */ findById(id: Object | string | number, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findById(id: Object | string | number, projection: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findById(id: Object | string | number, projection: Object, options: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; model(name: string): Model; @@ -2395,9 +2395,9 @@ declare module "mongoose" { */ find(callback?: (err: any, res: T[]) => void): DocumentQuery; find(conditions: Object, callback?: (err: any, res: T[]) => void): DocumentQuery; - find(conditions: Object, projection: Object, + find(conditions: Object, projection?: Object | null, callback?: (err: any, res: T[]) => void): DocumentQuery; - find(conditions: Object, projection: Object, options: Object, + find(conditions: Object, projection?: Object | null, options?: Object | null, callback?: (err: any, res: T[]) => void): DocumentQuery; @@ -2409,27 +2409,27 @@ declare module "mongoose" { * Executes immediately if callback is passed, else a Query object is returned. * @param id value of _id to query by */ - findByIdAndRemove(): DocumentQuery; + findByIdAndRemove(): DocumentQuery; findByIdAndRemove(id: Object | number | string, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findByIdAndRemove(id: Object | number | string, options: { /** if multiple docs are found by the conditions, sets the sort order to choose which doc to update */ sort?: Object; /** sets the document fields to return */ select?: Object; - }, callback?: (err: any, res: T) => void): DocumentQuery; + }, callback?: (err: any, res: T | null) => void): DocumentQuery; /** * Issues a mongodb findAndModify update command by a document's _id field. findByIdAndUpdate(id, ...) * is equivalent to findOneAndUpdate({ _id: id }, ...). * @param id value of _id to query by */ - findByIdAndUpdate(): DocumentQuery; + findByIdAndUpdate(): DocumentQuery; findByIdAndUpdate(id: Object | number | string, update: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findByIdAndUpdate(id: Object | number | string, update: Object, options: ModelFindByIdAndUpdateOptions, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; /** * Finds one document. @@ -2437,20 +2437,20 @@ declare module "mongoose" { * @param projection optional fields to return */ findOne(conditions?: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findOne(conditions: Object, projection: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findOne(conditions: Object, projection: Object, options: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; /** * Issue a mongodb findAndModify remove command. * Finds a matching document, removes it, passing the found document (if any) to the callback. * Executes immediately if callback is passed else a Query object is returned. */ - findOneAndRemove(): DocumentQuery; + findOneAndRemove(): DocumentQuery; findOneAndRemove(conditions: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findOneAndRemove(conditions: Object, options: { /** * if multiple docs are found by the conditions, sets the sort order to choose @@ -2461,7 +2461,7 @@ declare module "mongoose" { maxTimeMS?: number; /** sets the document fields to return */ select?: Object; - }, callback?: (err: any, res: T) => void): DocumentQuery; + }, callback?: (err: any, res: T | null) => void): DocumentQuery; /** * Issues a mongodb findAndModify update command. @@ -2469,12 +2469,12 @@ declare module "mongoose" { * and returns the found document (if any) to the callback. The query executes immediately * if callback is passed else a Query object is returned. */ - findOneAndUpdate(): DocumentQuery; + findOneAndUpdate(): DocumentQuery; findOneAndUpdate(conditions: Object, update: Object, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; findOneAndUpdate(conditions: Object, update: Object, options: ModelFindOneAndUpdateOptions, - callback?: (err: any, res: T) => void): DocumentQuery; + callback?: (err: any, res: T | null) => void): DocumentQuery; /** * geoNear support for Mongoose diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index c6de968430..3763d5975e 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -71,7 +71,7 @@ mongoose.version.toLowerCase(); * section querystream.js * http://mongoosejs.com/docs/api.html#querystream-js */ -var querystream: mongoose.QueryStream; +var querystream = {}; querystream.destroy(new Error()); querystream.pause(); querystream.pipe(process.stdout, {end: true}).end(); @@ -81,7 +81,7 @@ querystream.readable; /* inherited properties */ querystream.getMaxListeners(); /* practical examples */ -var QSModel: typeof mongoose.Model; +var QSModel = {}; var QSStream: mongoose.QueryStream = QSModel.find().stream(); QSStream.on('data', function (doc: any) { doc.save(); @@ -97,7 +97,7 @@ QSModel.where('created').gte(20000).stream().pipe(process.stdout); * section drivers/node-mongodb-native/collection.js * http://mongoosejs.com/docs/api.html#drivers-node-mongodb-native-collection-js */ -var coll1: mongoose.Collection; +var coll1 = {}; coll1.$format(999).toLowerCase(); coll1.$print('name', 'i', [1, 2, 3]); coll1.getIndexes(); @@ -109,7 +109,7 @@ coll1.ensureIndex(); coll1.find({}); coll1.insert({}, {}); -var coll2 = new mongoose.Collection('', new mongoose.Connection(null)); +var coll2 = new mongoose.Collection('', new mongoose.Connection(mongoose)); coll2.$format(999).toLowerCase(); /* inherited properties */ coll2.initializeOrderedBulkOp; @@ -155,7 +155,7 @@ conn1.addListener('close', cb); * section error/validation.js * http://mongoosejs.com/docs/api.html#error-validation-js */ -var validationError: mongoose.ValidationError; +var validationError = {}; validationError.toString().toLowerCase(); /* inherited properties */ validationError.stack; @@ -178,7 +178,7 @@ mongoose.Error.Messages.hasOwnProperty(''); * section querycursor.js * http://mongoosejs.com/docs/api.html#querycursor-js */ -var querycursor: mongoose.QueryCursor; +var querycursor = > {}; querycursor.close(function (error, result) { result.execPopulate(); }).catch(cb); @@ -267,8 +267,10 @@ schema.method('name', cb).method({ schema.path('a', mongoose.Schema.Types.Buffer).path('a'); schema.pathType('m1').toLowerCase(); schema.plugin(function (schema, opts) { -schema.get('path'); - opts.hasOwnProperty(''); + schema.get('path'); + if (opts) { + opts.hasOwnProperty(''); + } }).plugin(cb, {opts: true}); schema @@ -426,7 +428,7 @@ new mongoose.Schema({ * section document.js * http://mongoosejs.com/docs/api.html#document-js */ -var doc: mongoose.MongooseDocument; +var doc = {}; doc.$isDefault('path').valueOf(); doc.depopulate('path'); doc.equals(doc).valueOf(); @@ -483,7 +485,9 @@ var MyModel = mongoose.model('test', new mongoose.Schema({ doc = new MyModel(); doc.$isDefault('name'); MyModel.findOne().populate('author').exec(function (err, doc) { - doc.depopulate('author'); + if (doc) { + doc.depopulate('author'); + } }); doc.populate('path'); doc.populate({path: 'hello'}); @@ -540,7 +544,7 @@ interface MySubEntity extends mongoose.Types.Subdocument { interface MyEntity extends mongoose.Document { sub: mongoose.Types.Array } -var myEntity: MyEntity; +var myEntity = {}; var subDocArray = _.filter(myEntity.sub, function (sd) { sd.property1; sd.property2.toLowerCase(); @@ -570,7 +574,7 @@ interface MySubEntity1 extends mongoose.Types.Subdocument { interface MyEntity1 extends mongoose.Document { sub: mongoose.Types.DocumentArray } -var newEnt: MyEntity1; +var newEnt = {}; var newSub: MySubEntity1 = newEnt.sub.create({ property1: "example", property2: "example" }); /* @@ -645,7 +649,7 @@ embeddedDocument.execPopulate(); * section query.js * http://mongoosejs.com/docs/api.html#query-js */ -var query: mongoose.Query; +var query = > {}; query.$where('').$where(cb); query.all(99).all('path', 99); query.and([{ color: 'green' }, { status: 'ok' }]).and([]); @@ -877,7 +881,7 @@ schemaArray.sparse(true); * section schema/string.js * http://mongoosejs.com/docs/api.html#schema-string-js */ -var MongoDocument: mongoose.Document; +var MongoDocument = {}; var schemastring: mongoose.Schema.Types.String = new mongoose.Schema.Types.String('hello'); schemastring.checkRequired(234, MongoDocument).valueOf(); schemastring.enum(['hi', 'a', 'b']).enum('hi').enum({}); @@ -1161,7 +1165,9 @@ mongoose.model('') .findOne({}) .exec() .then(function (arg) { - arg.save; + if (arg) { + arg.save; + } return 1; }).then(function (num) { num.toFixed; @@ -1172,7 +1178,9 @@ mongoose.model('') str.toLowerCase return (mongoose.model('')).findOne({}).exec(); }).then(function (arg) { - arg.save; + if (arg) { + arg.save; + } return 1; }).catch(function (err) { return 1; @@ -1188,7 +1196,9 @@ mongoose.model('') mongoose.model('').findOne({}) .then(function (arg) { - arg.save; + if (arg) { + arg.save; + } return 2; }).then(function (num) { num.toFixed; @@ -1237,6 +1247,9 @@ MongoModel.find({}).$where('indexOf("val") !== -1').exec(function (err, docs) { }); MongoModel.findById(999, function (err, doc) { var handleSave = function(err: Error, product: mongoose.Document, numAffected: number) {}; + if (!doc) { + return; + } doc.increment(); doc.save(handleSave).then(cb).catch(cb); doc.save({ validateBeforeSave: false }, handleSave).then(cb).catch(cb); @@ -1256,7 +1269,9 @@ var mongoModel = new MongoModel(); mongoModel.remove(function (err, product) { if (err) throw(err); MongoModel.findById(product._id, function (err, product) { - product.remove(); + if (product) { + product.remove(); + } }); }); mongoModel.save().then(function (product) { @@ -1304,11 +1319,15 @@ MongoModel.find({ name: /john/i }, null, { skip: 10 }).exec(function (err, docs) MongoModel.findById(999, function (err, adventure) {}); MongoModel.findById(999).exec(cb); MongoModel.findById(999, 'name length', function (err, adventure) { - adventure.save(); + if (adventure) { + adventure.save(); + } }); MongoModel.findById(999, 'name length').exec(cb); MongoModel.findById(999, '-length').exec(function (err, adventure) { - adventure.addListener('click', cb); + if (adventure) { + adventure.addListener('click', cb); + } }); MongoModel.findById(999, 'name', { lean: true }, function (err, doc) {}); MongoModel.findById(999, 'name').lean().exec(function (err, doc) {}); @@ -1372,6 +1391,9 @@ MongoModel.mapReduce({ console.log(docs); }).then(null, cb); MongoModel.findById(999, function (err, user) { + if (!user) { + return; + } var opts = [ { path: 'company', match: { x: 1 }, select: 'name' } , { path: 'notes', options: { limit: 10 }, model: 'override' } @@ -1425,7 +1447,9 @@ mongoModel.addListener('event', cb); MongoModel.findOne({ title: /timex/i }) .populate('_creator', 'name') .exec(function (err, story) { - story.execPopulate(); + if (story) { + story.execPopulate(); + } }); MongoModel.find({ id: 999 @@ -1460,6 +1484,9 @@ var LocModel = mongoose.model("Location", locationSchema); LocModel.findById(999) .select("-reviews -rating") .exec(function (err, location) { + if (!location) { + return; + } location.name = 'blah'; location.address = 'blah'; location.reviews.forEach(review => {}); @@ -1496,18 +1523,35 @@ LocModel.distinct('') .then(cb).catch(cb); LocModel.findByIdAndRemove() .exec(function (err, doc) { + if (!doc) { + return; + } doc.addListener; doc.openingTimes; }); LocModel.findByIdAndUpdate() .select({}) .exec(function (err, location) { - location.reviews; + if (location) { + location.reviews; + } }); -LocModel.findOne({}, function (err, doc) { doc.openingTimes; }); +LocModel.findOne({}, function (err, doc) { + if (doc) { + doc.openingTimes; + } +}); LocModel.findOneAndRemove() - .exec(function (err, location) { location.name; }); -LocModel.findOneAndUpdate().exec().then(function (arg) { arg.openingTimes; }); + .exec(function (err, location) { + if (location) { + location.name; + } + }); +LocModel.findOneAndUpdate().exec().then(function (arg) { + if (arg) { + arg.openingTimes; + } +}); LocModel.geoSearch({}, { near: [1, 2], maxDistance: 22 @@ -1528,7 +1572,7 @@ interface ModelStruct { model: MyModel; method1: (callback: (model: MyModel, doc: MyDocument) => void) => MyModel; } -var modelStruct1: ModelStruct; +var modelStruct1 = {}; var myModel1: MyModel; var myDocument1: MyDocument; modelStruct1.method1(function (myModel1, myDocument1) { @@ -1555,8 +1599,8 @@ final2.method;interface ibase extends mongoose.Document { interface extended extends ibase { email: string; } -const base: mongoose.Model = mongoose.model('testfour', null) -const extended: mongoose.Model = base.discriminator('extendedS', null); +const base: mongoose.Model = mongoose.model('testfour') +const extended: mongoose.Model = base.discriminator('extendedS', schema); const x = new extended({ username: 'hi', // required in baseSchema email: 'beddiw', // required in extededSchema diff --git a/types/mongoose/tsconfig.json b/types/mongoose/tsconfig.json index 0c7d49295c..de340232f7 100644 --- a/types/mongoose/tsconfig.json +++ b/types/mongoose/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": false, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../"