Creating schemas from ES6 classes
See original GitHub issueHi,
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:
- Created 5 years ago
- Reactions:5
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi Yannis!
Both statements are correct. You still need a schema as you’ve demonstrated, e.g.
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:
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
Using a mixed notation
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.
@YannisMarios you should be able to implement schema inheritance using something like this: