There's more...

It's worth pausing to reflect on how similar our Mongoose model schema configuration, and our model schema inside a client application like our Angular application, are to one another:

  • Mongoose Post Schema:
  var postSchema = new Schema({
title: String,
content: String,
published: { type: Date, default: Date.now }
});
  • Angular Post Schema:
   export class BlogPost {
constructor(
public title: string = "",
public content: string = "",
public published: Date = new Date(),
) {}
}

You may be wondering if there is some logical way to simplify our application to create our model schema once, and simply load it into both places, considering they are both JSON representations of the same object. If a model was simply a collection of properties and types, this sort of approach would be trivial. However, due to the complexity of converting default values, validation, and virtuals from one format to another, this approach is quite complicated.

While we will not dive into this topic in the scope of this book, if you do find the concept of isomorphic model definitions intriguing and are interested in experimenting with the concept yourself, I recommend checking out the Advanced Schema's section of Mongoose's official API documentation.

It provides guidance on how to convert your Mongoose schemas to ES6 classes that are much more compatible with Angular's build system and could provide an avenue for code reuse across the backend and frontend of your application.

For more information on this, you can visit the following link:
http://mongoosejs.com/docs/advanced_schemas.html
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.138.202