Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to realize multi level chain call #9591

Closed
LeonYanghaha opened this issue Nov 28, 2020 · 4 comments
Closed

How to realize multi level chain call #9591

LeonYanghaha opened this issue Nov 28, 2020 · 4 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@LeonYanghaha
Copy link

I think mongoose find function is very good, it can Cat.find({name:"leon"}).populate("path").sort({"field": - 1}).skip (0).limit (10).lean(); this chain call is very easy to use, but now I have a problem. I want to encapsulate the find function myself. How can I encapsulate it? I don't know how to encapsulate it. I know return this is a good idea, but what should I return here? I want someone to give me some ideas? Thank you for your help.

This is my code now. What should I do next. How can I write it?

 async find(options: Object, populate?: string) {

    if (!populate) {
      return await this.mappingModel(meta.collections).find(options);
    }

    if (typeof populate === "string" && populate !== "") {
      return await this.mappingModel(meta.collections).find(options).populate(populate);
    }
    throw Error("Multi level chained calls are not supported ");
    // TODO 2020/11/27 15:09    How to realize multi level chain call
  }

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Nov 28, 2020

It's a good idea to encapsulate 3rd-party code with your own code, it helps with testing, and makes things easier if you want to change libraries in the future.

Assuming this.mappingModel(meta.collections) returns a mongoose model, you could do the following.

module.exports = {
  async find({ filter, populate, skip, limit, sort }) {
    return await this.mappingModel(meta.collections)
      .find(filter)
      .populate(populate)
      .skip(skip)
      .limit(limit)
      .sort(sort);
  }
};

Notice how the code doesn't do any checks for whether populate/skip/limit/sort are present or not, because mongoose is smart enough to ignore the .populate if it's passed an empty value, same goes for the rest of the chain.

I am not sure what you mean by "multi level chain call", can you elaborate on that?

@AbdelrahmanHafez AbdelrahmanHafez added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Nov 28, 2020
@LeonYanghaha
Copy link
Author

Your answer is very good, perfectly solved my problem. Thank you.

@AbdelrahmanHafez
Copy link
Collaborator

Happy to help.

@vkarpov15
Copy link
Collaborator

It sounds like you might be looking for custom query methods, that lets you add custom helpers like so:

catSchema.query.byName = function(name) {
  return this.find({name: name}).populate("path").sort({"field": - 1}).skip(0).limit(10).lean();
};

You can then use byName() with chaining, like Cat.find().byName('leon').select('name path')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants