mongoose-legacy
v1.0.4
Published
Mongoose-Legacy library provides backward compatibility support for the latest version of Mongoose (v7). It allows seamless migration and enables callbacks for deprecated methods. Compatible with Node.js and MongoDB.
Downloads
2,698
Maintainers
Readme
Mongoose-Legacy
The Mongoose-Legacy library was created to address the challenge of migrating a large codebase that heavily relies on callback syntax to the latest version of Mongoose, which uses promises. Rewriting thousands of instances of callback syntax into promises can be a daunting task in a large project, making it impractical to update to the latest version of Mongoose without significant code changes.
To overcome this limitation, Mongoose-Legacy provides an updated instance of Mongoose (v7) that retains compatibility with callbacks and other deprecated methods. This allows existing projects to benefit from the advantages of the latest version of Mongoose, such as improved performance, new features, and bug fixes, without the need for extensive code modifications.
Installation
To use this library, import it instead of the regular Mongoose module wherever you have defined your model. Install it using npm:
npm install mongoose-legacy
By importing the mongoose-legacy
library, you can seamlessly integrate the latest version of Mongoose into your project, leveraging its benefits without breaking existing code that relies on callback syntax.
Usage
In your code, require the library and use it to define your Mongoose models:
var { mongoose, Schema } = require('mongoose-legacy');
var userSchema = new Schema({
// Define your schema fields here
});
var User = mongoose.model('User', userSchema);
// Make this available in our Node applications
module.exports = User;
Examples
Here are some examples demonstrating the usage of the Mongoose-Legacy library:
Example 1: Using Model.findById with Callback and Async/Await
Callback:
User.findById(userId, function (err, user) {
if (err) {
console.error(err);
return;
}
console.log(user);
});
Async/Await:
try {
const user = await User.findById(userId);
console.log(user);
} catch (err) {
console.error(err);
}
Example 2: Query Chaining with Exec and Callbacks, and using Async/Await
Callback:
User.find()
.where('age').gte(18)
.exec(function (err, users) {
if (err) {
console.error(err);
return;
}
console.log(users);
});
Async/Await:
try {
const users = await User.find().where('age').gte(18).exec();
console.log(users);
} catch (err) {
console.error(err);
}
Example 3: Using Update Query (deprecated in v7) with Mongoose-Legacy
User.update(
{ name: 'John' },
{ $set: { age: 30 } },
function (err, result) {
if (err) {
console.error(err);
return;
}
console.log(result);
}
);
The Mongoose-Legacy library adds support for the deprecated update
query by internally using updateOne
. This ensures that you can continue using the familiar update
method in your code without any compatibility issues.
Documentation
For detailed documentation on how to use Mongoose and its features, refer to the official Mongoose documentation.
Compatibility
Mongoose-Legacy is backward compatible up to Mongoose version 4 and is designed to work with the latest Mongoose version (v7). It provides a seamless transition to the new version, allowing you to maintain existing code while benefiting from the latest enhancements and features.
License
This library is released under the ISC License. Please see the LICENSE file for more details.
Author
This library is developed and maintained by Trailingcrypto. Feel free to reach out with any questions or feedback.
Acknowledgements
We would like to thank the Mongoose team for their great work on the original Mongoose library, which serves as the foundation for Mongoose-Legacy. Your contributions and efforts are invaluable to the development community.