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

Is it possible to inherit virtuals as well? #1

Open
miliu99 opened this issue Sep 3, 2017 · 3 comments
Open

Is it possible to inherit virtuals as well? #1

miliu99 opened this issue Sep 3, 2017 · 3 comments

Comments

@miliu99
Copy link

miliu99 commented Sep 3, 2017

I have some virtuals for populating data. It would be nice if those virtuals would be inherited too.

@doasync
Copy link
Owner

doasync commented Dec 12, 2017

You can use the third argument. Here is the signature of the function: extendSchema(Schema, definition, options). But I can't check now if it possible to get instance options (virtuals) to inherit them.

@thxmike
Copy link

thxmike commented Apr 21, 2018

I have a sample that may help. It leverages the transpiled version of extends from ES6

const mongoose = require("mongoose");

function extendSchema(parent, child, options) {
  let updated_schema = Object.assign({}, parent.obj, child);
  let updated_child_object = new mongoose.Schema(updated_schema, options);
  __extends(updated_child_object, parent);
  return updated_child_object;
}

var __extends =
  (this && this.__extends) ||
  function(d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() {
      this.constructor = d;
    }
    d.prototype =
      b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
  };

module.exports = extendSchema;

@thxmike
Copy link

thxmike commented Apr 21, 2018

Let me know if you want me to put a pull request in for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants