Introduction

The acronym CRUD is commonly used in software development to describe the four basic functions of a database: Create, Read, Update, and Delete. These operations are fundamental to many applications.

The CRUD apps consist of creating new data (Create operation), reading existing data (Read operation), updating an existing record with new data (Update operation), and deleting an existing record (Delete operation).

CRUD: Definition, Operations, Benefits How it Works and More

What are 4 main CRUD operations?

CRUD operations are the four basic functions of a database: Create, Read, Update, and Delete.

  1. Create – adding new records to the database.
  2. Read – retrieving data from the database.
  3. Update – modifying existing records in the database.
  4. Delete – removing existing records from the database.

 

1. Create – Add new data

Create – The ability to add new data is the most basic function of a database management system. With this capability, you could collect any information at all. The Create operation has two parts: 1) Add a new record 2) Set the values for each field in that record.

For Example:

const createContact = asyncHandler(async (req, res) => {

  console.log(“The request body is :”, req.body);

  const { name, email, phone } = req.body;

  if (!name || !email || !phone) {

    res.status(400);

    throw new Error(“All fields are mandatory !”);

  }

  const contact = await Contact.create({

    name,

    email,

    phone,

    user_id: req.user.id,

  });

  res.status(201).json(contact);

});

2. Read – Retrieve existing data

The Read operation allows you to retrieve existing data from a database table or view. This is usually the second operation after creating a new database table or adding new entries. You can use the Read operation with other operations, such as Update or Delete, to retrieve specific records (or rows) from your database.

For Example:

const getContact = asyncHandler(async (req, res) => {

  const contact = await Contact.findById(req.params.id);

  if (!contact) {

    res.status(404);

    throw new Error(“Contact not found”);

  }

  res.status(200).json(contact);

});

3. Update – Modify existing data

Once data has been read from storage, it may need to be modified before being saved into a persistent storage system (for example, if a user checks out an item from a library). The update function allows users to change an existing record without deleting or creating an entirely new record for each change (such as when editing text within a document).

For Example:

const updateContact = asyncHandler(async (req, res) => {

  const contact = await Contact.findById(req.params.id);

  if (!contact) {

    res.status(404);

    throw new Error(“Contact not found”);

  }

  if (contact.user_id.toString() !== req.user.id) {

    res.status(403);

    throw new Error(“User don’t have permission to update other user contacts”);

  }

  const updatedContact = await Contact.findByIdAndUpdate(

    req.params.id,

    req.body,

    { new: true }

  );

  res.status(200).json(updatedContact);

});

4. Delete – Remove existing data

The delete function allows users to remove one or more records from their database. You should use this function to make room for new records or if a user has finished working with an item and no longer needs it. It’s important to note that when you delete a record, all of the associated data will also be removed from storage.

For Example:

const deleteContact = asyncHandler(async (req, res) => {

  const contact = await Contact.findById(req.params.id);

if (!contact) {

    res.status(404);

    throw new Error(“Contact not found”);

  }

  if (contact.user_id.toString() !== req.user.id) {

    res.status(403);

    throw new Error(“User don’t have permission to update other user contacts”);

  }

  await Contact.deleteOne({ _id: req.params.id });

  res.status(200).json(contact); });

Leave a Reply