question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating schemas from ES6 classes

See original GitHub issue

Hi,

My versions: node: v10.5.0, mongoose: v5.2.7, mongodb: v4.0.0

I want to ask about creating schemas from ES6 classes. Here http://mongoosejs.com/docs/advanced_schemas.html#creating-from-es6-classes-using-loadclass it says: “Mongoose allows creating schemas from ES6 classes.” which is not true since one still has to define schema fields separately like:

const schema = new Schema({ firstName: String, lastName: String });

define the class:

class PersonClass { ... }

and then:

schema.loadClass(PersonClass);
var Person = db.model('Person', schema);

I mean what is the point doing all this since I still have to define all schema fields separately?

Thank you

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

5reactions
samcoenencommented, Aug 13, 2018

Hi Yannis!

Both statements are correct. You still need a schema as you’ve demonstrated, e.g.

const schema = new Schema({ firstName: String, lastName: String });

Once you have that, you can use an ES6 class to define and add custom methods on your models. It’s written in the next sentence after the one you’ve quoted:

Mongoose allows creating schemas from ES6 classes. The loadClass() function lets you pull in methods, statics, and virtuals from an ES6 class.

The functionality is limited to only defining these methods, statics and virtuals. (if I’m mistaken, someone can correct me).

A full example:

Using pure schema notation

const schema = new Schema({ firstName: String, lastName: String });

schema.methods.setName = function setName(firstName, lastName) => {
  this.firstName = firstName;
  this.lastName = lastName;
}

schema.statics.findByFirstName = function findByFirstName(firstName, cb) => {
  return this.where('firstName', new RegExp(firstName, 'i')).exec(cb);
}

Using a mixed notation

const schema = new Schema({ firstName: String, lastName: String });

class User {
  set name(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  static findByFirstName(firstName, cb) {
    return this.where('firstName', new RegExp(firstName, 'i').exec(cb);
  }
}

schema.loadClass(User);

As you can see, both examples define the initial schema. Afterwards the additional logic can be defined using both ways. Using the ES6 class notation makes you code easier to read and maintain, especially if you’re adding a lot of methods, statics and virtuals. It can greatly reduce the amount of code you need to write, though this really depends on your own project.

ES6 classes do not provide an alternative to writing your schema. ES6 classes provide an alternative to writing additional methods for your Model.

Hopefully this has answered your question.

1reaction
vkarpov15commented, Aug 19, 2018

@YannisMarios you should be able to implement schema inheritance using something like this:

function ChildSchema(obj) {
  return baseSchema.clone().add(obj);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Advanced Schemas v4.13.20
Mongoose allows creating schemas from ES6 classes. The loadClass() function lets you pull in methods, statics, and virtuals from an ES6 class. A...
Read more >
How to use ES6 classes to create Mongoose schemas
Mongoose allows us to create a schema from ES6 classes. The loadClass() method provided by Mongoose Schemas takes in a class object as...
Read more >
Mongoose Schemas With ES6 Classes - GeeksforGeeks
Mongoose is a MongoDB object modeling and handling for a node.js environment. To load Mongoose schema from an ES6 Class, we can use...
Read more >
How to write a Mongoose model in ES6 / ES2015
I'm not sure why you're attempting to use ES6 classes in this case. mongoose.Schema is a constructor to create new schemas. When you...
Read more >
Create a Data Schema - Go Full-Stack With Node.js, Express ...
Create a Thing Schema ... Let's look at how to create a thing schema in the next video! ... title: { type: String,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found