npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

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.