Step 10

Let's refactor our application to persist data. As the first step, we will update our model for "note."

Open Note.js file inside server/model folder. It currently has the following content:

const { v4: uuidv4 } = require("uuid");

class Note {
  constructor(title, text) {
    this._id = uuidv4();
    this.title = title;
    this.text = text;
  }
}

module.exports = Note;

Update the source code:

const mongoose = require("mongoose");

const NoteSchema = new mongoose.Schema({
  title: { type: String, required: true },
  text: { type: String, required: true },
});

const Note = mongoose.model("Note", NoteSchema);

module.exports = Note;

As can be seen, we are making a schema for notes document and linking that schema to a Mongoose Model called Note. Moreover, notice the required: true attribute in the NoteSchema. This attribute will help us with input validation.

Resources

To learn more about other attributes that can be used when defining your schemas, consult Mongoose's Docs on Schema.