Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions models/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,39 @@ const tutorialSchema = {

// eslint-disable-next-line no-unused-vars
const Tutorial = connector.model("Tutorial", tutorialSchema);

/// CRUD Operations ///

// Add a new tutorial to the database
async function create(tutorialData) {
const tutorial = new Tutorial(tutorialData);
const tutorialDoc = await tutorial.save();
return tutorialDoc;
}

// Retrieve tutorials based on a given filter and limit
async function read(filter, limit = 1) {
const tutorialDoc = await Tutorial.find(filter).limit(limit);
return tutorialDoc;
}

// Update tutorials based on a given filter and update data
async function update(filter, updateObject, options = { multi: true }) {
const updateResult = await Tutorial.updateMany(filter, { $set: updateObject }, options);
return updateResult.acknowledged;
}

// Delete tutorials based on a given filter
async function remove(filter) {
const deleteResult = await Tutorial.deleteMany(filter).exec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there exec() function used here? I don't think we need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of it as confirming your request to delete items from the database. Once you press "Go" (by using .exec()). Should i change it and make another commit or let it be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be fine

return deleteResult.acknowledged;
}

// Export the CRUD functions
export default {
create,
read,
update,
remove,
};