Models

The models used in SaneJS are standard Mongoose models with the mongoose-paginate-v2 plugin added for pagination. A typical model will look like this:


const mongoose = require('mongoose')
const mongoosePaginate = require('mongoose-paginate-v2')
const validator = require('validator') // https://www.npmjs.com/package/validator
const ObjectId = mongoose.Schema.Types.ObjectId
/* ------------------------------------------------------- */

const modelName = 'Book'

const schema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true
    },
    author: {
      type: String,
      required: true
    }
  },
  { timestamps: true }
)

/* ------------------------------------------------------- */
// Uncomment below if using virtuals or pagination.
// schema.set('toObject', { virtuals: true }) 
// schema.plugin(mongoosePaginate)


module.exports = mongoose.model(modelName, schema)

Learn more about Mongoose models from the official docs:
https://mongoosejs.com/docs/models.html

Learn more about the pagination plugin here:
https://github.com/aravindnc/mongoose-paginate-v2