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

Integration for ES6 classes #4668

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,23 @@ Mongoose.prototype.disconnect.$hasSideEffects = true;
* var collectionName = 'actor'
* var M = mongoose.model('Actor', schema, collectionName)
*
* @param {String} name model name
* @param {String|Function} name model name or class extending Model
* @param {Schema} [schema]
* @param {String} [collection] name (optional, induced from model name)
* @param {String} [collection] name (optional, inferred from model name)
* @param {Boolean} [skipInit] whether to skip initialization (defaults to false)
* @api public
*/

Mongoose.prototype.model = function(name, schema, collection, skipInit) {
var model;
if (typeof name === 'function') {
model = name;
name = model.name;
if (!(model.prototype instanceof Model)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong.. I don't think this would work in the case when using a Base class on an individual class.

export class Foo extends MyBase {...}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, instanceof looks through the whole prototype chain. So as long as MyBase extends Model, it's fine. I'm actually currently using this use of extends for some of the models in my app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for clarifying that. I didn't know that about instanceof; obviously :)

throw new mongoose.Error('The provided class ' + name + ' must extend Model');
}
}

if (typeof schema === 'string') {
collection = schema;
schema = false;
Expand Down Expand Up @@ -354,7 +363,6 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
this._applyPlugins(schema);
}

var model;
var sub;

// connection.model() may be passing a different schema for
Expand Down Expand Up @@ -393,7 +401,7 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
}

var connection = options.connection || this.connection;
model = this.Model.compile(name, schema, collection, connection, this);
model = this.Model.compile(model || name, schema, collection, connection, this);
Copy link
Contributor

@mleanos mleanos Nov 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change needed? name is being set above when model is set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where the problem you see is, can you elaborate? model is unset if the name passed in is a string, which means that Model.compile receives a string name rather than an ES6 class, and everything proceeds normally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I just saw the diff and thought it wasn't necessary based on my misunderstanding of what the compile method was doing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first looked at this, I thought this bit was still inside the if (typeof name === 'function').. scope.


if (!skipInit) {
model.init();
Expand Down
27 changes: 18 additions & 9 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3190,7 +3190,7 @@ Model._getSchema = function _getSchema(path) {
/*!
* Compiler utility.
*
* @param {String} name model name
* @param {String|Function} name model name or class extending Model
* @param {Schema} schema
* @param {String} collectionName
* @param {Connection} connection
Expand All @@ -3207,19 +3207,28 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
schema.add(o);
}

// generate new class
function model(doc, fields, skipId) {
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
var model;
if (typeof name === 'function' && name.prototype instanceof Model) {
model = name;
name = model.name;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the '.name' defined?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because type of name=== 'function' and model = name model is a function. This function.prototype.name is what it's calling. So it would be "myFunction" in the below function function my function() {}

With a class this is the name of the class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we want our model to have a name different than the class name, i.e if our model is name is 'my.function'?

We have to override it? i.e

class myFunction {
     static get name(){
          return 'my.function';
     }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is a read-only property, so you can't change it
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

I suggest you look at the link that vkarpov15 commented to use .LoadClass into the schema you want

schema.loadClass(model, true);
} else {
// generate new class
model = function model(doc, fields, skipId) {
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
};
}

model.hooks = schema.s.hooks.clone();
model.base = base;
model.modelName = name;
model.__proto__ = Model;
model.prototype.__proto__ = Model.prototype;
if (!(model.prototype instanceof Model)) {
model.__proto__ = Model;
model.prototype.__proto__ = Model.prototype;
}
model.model = Model.prototype.model;
model.db = model.prototype.db = connection;
model.discriminators = model.prototype.discriminators = undefined;
Expand Down
24 changes: 14 additions & 10 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1509,28 +1509,32 @@ Schema.prototype.remove = function(path) {
*
* @param {Function} model
*/
Schema.prototype.loadClass = function(model) {
Schema.prototype.loadClass = function(model, virtualsOnly) {
if (model === Object.prototype || model === Function.prototype) {
return this;
}

// Add static methods
Object.getOwnPropertyNames(model).forEach(function(name) {
if (name.match(/^(length|name|prototype)$/)) {
return;
}
var method = Object.getOwnPropertyDescriptor(model, name);
if (typeof method.value === 'function') this.static(name, method.value);
}, this);
if (!virtualsOnly) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm what's the point of this virtualsOnly option? Looks like it's always true anyway...

Copy link
Contributor Author

@kherock kherock Nov 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it's probably unnecessary. Its only use is mainly internal by Model.compile. That's also why I didn't add JSDoc for the parameter.

It seems like Schemas need the virtuals defined on them directly for them to function properly, e.g. in toObject and toJSON. It just felt wasteful copying all of the statics and instance methods from the class onto the schema when applyMethods(model, schema) and applyStatics(model, schema) are being called immediately afterward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, loadClass is intended to be public, used generally when someone might want to use a class to define virtuals, etc. on a subschema, like

const subSchema = new Schema({ ... });
subSchema.loadClass(class { ... });

const schema = new Schema({ field1: subSchema });
class MyModel extends Model { ... }

In this case virtualsOnly is undefined.

Object.getOwnPropertyNames(model).forEach(function(name) {
if (name.match(/^(length|name|prototype)$/)) {
return;
}
var method = Object.getOwnPropertyDescriptor(model, name);
if (typeof method.value === 'function') this.static(name, method.value);
}, this);
}

// Add methods and virtuals
Object.getOwnPropertyNames(model.prototype).forEach(function(name) {
if (name.match(/^(constructor)$/)) {
return;
}
var method = Object.getOwnPropertyDescriptor(model.prototype, name);
if (typeof method.value === 'function') {
this.method(name, method.value);
if (!virtualsOnly) {
if (typeof method.value === 'function') {
this.method(name, method.value);
}
}
if (typeof method.get === 'function') {
this.virtual(name).get(method.get);
Expand Down