November 2018
Beginner
132 pages
2h 57m
English
To communicate with our database, we need to define a data model for the contacts. Mongoose models serve as wrappers around schema definitions. A Mongoose schema defines the object structure, constraints, default values, and so on. Models are responsible for CRUD operations for an object with the underlying database.
To create a simple data model, let's create a Contact.js file in the models folder, and insert the following code into it:
// ./models/Contact.jsconst Koa = require('koa');const app = new Koa();const mongoose = require('mongoose');mongoose.connect( 'mongodb://localhost:27017/koa-contact', { useNewUrlParser: true });const db = mongoose.connection;db.on('error', error => { throw new Error(`error connecting ...Read now
Unlock full access