to make a new model we need to create a new folder under the /todo
folder called models
, where all of our schemas will live and under models
create a file called task.server.model.js
modules└── todo├── bootstraps│ └── main.server.bootstrap.js├── iam│ └── main.server.iam.js├── controllers│ └── main.server.controller.js└── models└── task.server.model.js
To generate a new model there's a snippet for that, in modules/todo/models/task.server.model.js
type in module:model
and then press TAB
modules/models/task.server.model.js
1/**2 * Module dependencies.3 */4const { Schema, model } = require("mongoose");5const { timestamps } = require("@config/index").lib.mongoose;67const TodoSchema = new Schema(8 {9 name: String,10 done: {11 type: Boolean,12 default: false,13 },14 },15 {16 timestamps,17 }18);1920module.exports = model("Todo", TodoSchema);
Now let's use our newly created model in the controller, on the top of the file require in the model from mongoose and do the logic for listing our todo and creating a task
Edit this page on GitHubmodules/todo/controllers/main.server.controller.js
2const { model, Types } = require("mongoose");3const Todo = model("Todo");57/**8 * List todos9 * @controller List10 * @param {Express.Request} req The request11 * @param {OutcommingMessage} res The response12 * @param {Function} next Go to the next middleware13 */1415exports.list = async function list(req, res, next) {16 try {17 const result = await Todo.find();18 return res.json(result);19 } catch (e) {20 return next(e);21 }22};2426/**27 * Create todo28 * @controller Create29 * @param {Express.Request} req The request30 * @param {OutcommingMessage} res The response31 * @param {Function} next Go to the next middleware32 */3334exports.create = async function create(req, res, next) {35 const { body } = req;36 let task = new Todo(body);3738 try {39 task = await task.save({ new: true });40 } catch (e) {41 return next(e);42 }4344 return res.status(201).json(task);45};4749/**50 * List one Todo51 * @controller One52 * @param {Express.Request} req The request53 * @param {OutcommingMessage} res The response54 * @param {Function} next Go to the next middleware55 */5657exports.one = async function one(req, res, next) {58 const { taskId } = req.params;5960 if (!Types.ObjectId.isValid(taskId)) {61 return res.status(400).send({62 message: req.t("INVALID_TASK_ID", {63 taskId,64 }),65 });66 }67 let task;68 try {69 task = await Todo.findById(taskId);70 } catch (e) {71 return next(e);72 }73 if (!task) {74 return res.status(404).send({75 message: req.t("TASK_NOT_FOUND", {76 taskId,77 }),78 });79 }80 return res.json(task);81};8385/**86 * Update todo87 * @controller Update88 * @param {Express.Request} req The request89 * @param {OutcommingMessage} res The response90 * @param {Function} next Go to the next middleware91 */9293exports.update = async function update(req, res, next) {94 const { taskId } = req.params;95 if (!Types.ObjectId.isValid(taskId)) {96 return res.status(400).send({97 message: req.t("INVALID_TASK_ID", {98 taskId,99 }),100 });101 }102103 let updatedTodo;104 try {105 updatedTodo = await Todo.findOneAndUpdate(106 { _id: taskId },107 {108 name: req.body.name,109 done: req.body.done,110 },111 { new: true }112 );113 } catch (e) {114 next(e);115 }116117 return res.send(updatedTodo);118};120122/**123 * Remove todo124 * @controller Remove125 * @param {Express.Request} req The request126 * @param {OutcommingMessage} res The response127 * @param {Function} next Go to the next middleware128 */129130exports.remove = async function remove(req, res, next) {131 const { taskId } = req.params;132 if (!Types.ObjectId.isValid(taskId)) {133 return res.status(400).send({134 message: req.t("INVALID_TASK_ID", {135 taskId,136 }),137 });138 }139 let deletedTask;140141 try {142 deletedTask = await Todo.deleteOne({ _id: taskId });143 } catch (e) {144 next(e);145 }146147 return res.send(deletedTask);148};