izmjs

Models

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;
6
7const TodoSchema = new Schema(
8 {
9 name: String,
10 done: {
11 type: Boolean,
12 default: false,
13 },
14 },
15 {
16 timestamps,
17 }
18);
19
20module.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

modules/todo/controllers/main.server.controller.js
2const { model, Types } = require("mongoose");
3const Todo = model("Todo");
5
7/**
8 * List todos
9 * @controller List
10 * @param {Express.Request} req The request
11 * @param {OutcommingMessage} res The response
12 * @param {Function} next Go to the next middleware
13 */
14
15exports.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};
24
26/**
27 * Create todo
28 * @controller Create
29 * @param {Express.Request} req The request
30 * @param {OutcommingMessage} res The response
31 * @param {Function} next Go to the next middleware
32 */
33
34exports.create = async function create(req, res, next) {
35 const { body } = req;
36 let task = new Todo(body);
37
38 try {
39 task = await task.save({ new: true });
40 } catch (e) {
41 return next(e);
42 }
43
44 return res.status(201).json(task);
45};
47
49/**
50 * List one Todo
51 * @controller One
52 * @param {Express.Request} req The request
53 * @param {OutcommingMessage} res The response
54 * @param {Function} next Go to the next middleware
55 */
56
57exports.one = async function one(req, res, next) {
58 const { taskId } = req.params;
59
60 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};
83
85/**
86 * Update todo
87 * @controller Update
88 * @param {Express.Request} req The request
89 * @param {OutcommingMessage} res The response
90 * @param {Function} next Go to the next middleware
91 */
92
93exports.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 }
102
103 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 }
116
117 return res.send(updatedTodo);
118};
120
122/**
123 * Remove todo
124 * @controller Remove
125 * @param {Express.Request} req The request
126 * @param {OutcommingMessage} res The response
127 * @param {Function} next Go to the next middleware
128 */
129
130exports.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;
140
141 try {
142 deletedTask = await Todo.deleteOne({ _id: taskId });
143 } catch (e) {
144 next(e);
145 }
146
147 return res.send(deletedTask);
148};
Edit this page on GitHub