Decoupling the Team Model

We have created a models folder in the root project directory. In this folder, we will create all the models for our application. Start creating the team.js file in the src/models folders:

$ touch src/models/team.js

Remember that we use the touch command to create a new file. Then, open this file, and from the src/config/mongoose-connection.js file, cut the following lines and copy them into the src/models/team.js file, as follows:

const mongoose = require('mongoose')

const TeamSchema = new mongoose.Schema({
name: {
type: String,
min: 3,
max: 100,
required: true,
unique: true
},
ranking: {
type: Number,
min: 1
},
captain: {
type: String,
required: true
},
Trainer: {
type: String,
required: true
},
confederation: {
type: String,
required: true,
uppercase: true
}
})

module.exports = mongoose.model('team', TeamSchema)

We just need to isolate the TeamSchema definition, and we are exporting the model created by mongoose to be accessed later by the Rest Controller. Be sure that your src/config/mongoose-connection file looks like the following:

const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:32768/wcDb', { useMongoClient: true })
mongoose.Promise = global.Promise

mongoose.connection.on('connected',() => {
console.log('connection is ready')
})

mongoose.connection.on('error', err => {
console.log(err)
})

Cool! So far, so good. Now it is time to implement our Rest Controller.

..................Content has been hidden....................

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