Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Created May 12, 2022 11:23
Show Gist options
  • Save halitbatur/4b641bd64249880151a83082f35cc5eb to your computer and use it in GitHub Desktop.
Save halitbatur/4b641bd64249880151a83082f35cc5eb to your computer and use it in GitHub Desktop.
Discussion about MVC, DB Schema and CRUD

Discuss the points below with your teammates and add your answers below in the comments

  1. What is the best/most common file structure for backend NodeJS projects?
  2. What is the MVC architecture? Why we will be using it?
  3. Explore defining a schema in Mongoose, you can refer to this for more information (no answer in the comments required).
  4. What are the CRUD operations? add an example code of each CRUD operations in Mongoose.
@khaldarov
Copy link

khaldarov commented May 19, 2022

Ezgi Okur, Hafiz Mhammadah, Adnan Khaldar

What is the best/most common file structure for backend NodeJS projects?

  • JSON

What is the MVC architecture? Why will we be using it?

  • The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.

What are the CRUD operations? add an example code of each CRUD operations in Mongoose.

  • The mongos instances route queries and write operations to the shards in a sharded cluster.
  • Creat, Read, Update, Delete
  • const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
  • new Query().read('primary')
  • Model.where({ _id: id }).update({ title: 'words' });
  • Character.deleteOne({ name: 'Eddard Stark' }, () );

@mohammadibrah
Copy link

mohammadibrah commented May 19, 2022

Teammates: Nilay, Rasha, Mohammad Ibrahim

What is the best/most common file structure for backend NodeJS projects?
there some rules to best orgnizing our nodejs project, we could put some here:
Rule 1 – Organize your Files Around Features, Not Roles
Rule 2 – Don’t Put Logic in index.js Files
Rule 3 – Place Your Test Files Next to The Implementation
Rule 4 – Use a config Directory
Rule 5 – Put Your Long npm Scripts in a scripts Directory

What is the MVC architecture? Why we will be using it?

Three Levels Of MVC Model :

  1. Model –
    This level is considered the lowest level when compared with the View and Controller. It primarily represents the data to the user and defines the storage of all the application’s data objects.

  2. Views –
    This level is majorly associated with the User Interface(UI) and it is used to provide the visual representation of the MVC model. In simpler terms, this level deals with displaying the actual output to the user. It also handles the communication between the user (inputs, requests, etc.) and the controller.

  3. Controller –
    This level takes care of the request handler. It is often considered as the brain of the MVC system- a link, so to speak, between the user and the system. The controller completes the cycle of taking the user output, converting it into desired messages, and passing them on to the views(UI).
    and we will use it to orgnize our nodejs structure
    Explore defining a schema in Mongoose, you can refer to this for more information (no answer in the comments required).
    Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

What are the CRUD operations? add an example code of each CRUD operations in Mongoose.
there are operations about Creating, Reading, Updating, Deleting documents in mongodb
Create ex.

router.get('/save', function(req, res) {
    var newStudent = new StudentModel({StudentId:101, 
        Name:"Sam", Roll:1, Birthday:2001-09-08});

    newStudent.save(function(err, data) {
        if(err) {
            console.log(error);
        }
        else {
            res.send("Data inserted");
        }
    });
});

Read ex.
r```
outer.get('/findall', function(req, res) {
StudentModel.find(function(err, data) {
if(err){
console.log(err);
}
else{
res.send(data);
}
});
});


Update ex.
`router.post('/update', function(req, res) {
    StudentModel.findByIdAndUpdate(req.body.id, 
    {Name:req.body.Name}, function(err, data) {
        if(err){
            console.log(err);
        }
        else{
            res.send(data);
            console.log("Data updated!");
        }
    });  
});
`
Delete ex.
``router.get('/delete', function(req, res) {
    StudentModel.remove({StudentId:188}, 
    function(err, data) {
        if(err){
            console.log(err);
        }
        else{
            res.send(data);
        }
    });  
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment