Organizing Code with Models and Controllers: structuring backend with models, services and controllers; data models for users/products using MongoDB (Mongoose); basic CRUD operations using Express and MongoDB

સુઘડ backend જવાબદારીઓ અલગ પાડે છે: models તમારા data નો આકાર નક્કી કરે છે (MongoDB માટે Mongoose ના schemas દ્વારા), controllers route નું logic સાચવે છે, અને Mongoose તમને documents બનાવવા, વાંચવા, સુધારવા અને કાઢી નાખવા માટે સ્વચ્છ methods આપે છે.

12 min read · 8 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Backend ને આકાર આપો

FestConnect હવે ખરો data સાચવે છે: users, events, registrations. Data અને logic વધે કે તરત જ એક જ file વાળો server ગૂંચવાડો બની જાય છે. સારી રીતે ગોઠવાયેલો backend બે જવાબદારીઓ અલગ પાડે છે: data કેવો દેખાય છે (models) અને દરેક route પર શું થાય છે (controllers).

Database માટે FestConnect MongoDB વાપરે છે (જે તમે સેમેસ્ટર 5 માં જોયું હતું), અને તે પણ Mongoose નામની library દ્વારા, જે data ના આકાર નક્કી કરે છે અને create, read, update તથા delete માટે સ્વચ્છ methods આપે છે. આ પાઠ models, controllers અને Mongoose સાથેનું CRUD આવરી લે છે, જેથી backend વધે તો પણ વ્યવસ્થિત રહે.

Theory

Mongoose સાથે models

Mongoose એ Node માં MongoDB માટેની ODM (Object Data Modelling) library છે. તમે તમારા data નું વર્ણન Schema વડે કરો છો, એટલે કે fields અને એમના પ્રકારો, અને પછી એને Model માં ફેરવો છો. એ model જ તે collection સુધીનું તમારું દ્વાર છે: તમે એના વડે documents બનાવો છો અને એમના પર query કરો છો.

MongoDB documents (JSON જેવા પદાર્થો) ને collections માં સંઘરે છે (લવચીક rows વાળાં tables જેવાં). Mongoose નું schema MongoDB ની લવચીકતા પર રચના અને ચકાસણી ઉમેરે છે, જેથી દરેક Event પાસે યોગ્ય પ્રકારનું name અને date હોય જ. દરેક એકમ માટે (User, Event, Registration) સ્પષ્ટ model વ્યાખ્યાયિત કરવું એ વ્યવસ્થિત backend નો પાયો છે.

Practical

Mongoose નું model

const mongoose = require('mongoose');

const eventSchema = new mongoose.Schema({
  name:  { type: String, required: true },
  date:  { type: Date,   required: true },
  seats: { type: Number, default: 50 },
});

// schema ને model માં ફેરવો ('events' collection):
const Event = mongoose.model('Event', eventSchema);
module.exports = Event;

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

CRUD અને controllers

Model તમને સ્વચ્છ CRUD methods આપે છે, જે Promises પાછાં આપે છે (એટલે async અને await વાપરો):

  • Create: Event.create({...}) (અથવા new Event({...}).save()).
  • Read: Event.find() (બધાં) અથવા Event.findById(id) (એક).
  • Update: Event.findByIdAndUpdate(id, {...}).
  • Delete: Event.findByIdAndDelete(id).

Controller એ જગ્યા છે જ્યાં તમે route ના જવાબમાં આ methods બોલાવો છો. Controllers ને પોતાની files માં રાખવાથી (અને models ને એમની files માં) તમારી route ની વ્યાખ્યાઓ પાતળી રહે છે, એટલે કે route ફક્ત એટલું જ કહે કે 'POST /api/events એ createEvent પર જાય', અને ખરું logic controller પાસે રહે. સ્પષ્ટ વિભાજન, અને વધારવામાં સહેલું.

Practical

Model વાપરતું controller (CRUD)

const Event = require('../models/event');

// બનાવવું
exports.createEvent = async (req, res) => {
  const event = await Event.create(req.body);
  res.status(201).json(event);
};

// બધાં વાંચવાં
exports.getEvents = async (req, res) => {
  const events = await Event.find();
  res.json(events);
};

// id પ્રમાણે કાઢી નાખવું
exports.deleteEvent = async (req, res) => {
  await Event.findByIdAndDelete(req.params.id);
  res.status(204).send();
};

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Mongoose પર ચાલતા backend માં Schema ની ભૂમિકા શી છે?

  1. એ Express નો server ચાલુ કરે છે
  2. એ MongoDB ના collection માટે data નો આકાર નક્કી કરે છે (fields અને એમના પ્રકારો), અને એને CRUD માટે વપરાતા Model માં ફેરવાય છે
  3. એ CORS સંભાળે છે
  4. એ route જેવું જ છે
Show the answer

એ MongoDB ના collection માટે data નો આકાર નક્કી કરે છે (fields અને એમના પ્રકારો), અને એને CRUD માટે વપરાતા Model માં ફેરવાય છે

Mongoose નું Schema MongoDB ના collection માટે તમારા data ની રચના નક્કી કરે છે, એટલે કે fields અને એમના પ્રકારો (તથા ચકાસણી), અને તમે એને એવા Model માં ફેરવો છો જે CRUD methods આપે છે (create, find, update, delete). વિકલ્પ A ખોટો છે: server ચાલુ કરવાનું કામ Express માં app.listen કરે છે, જેને schemas સાથે કંઈ લેવાદેવા નથી. વિકલ્પ C ખોટો છે: CORS ને middleware સંભાળે છે, schema નહીં. વિકલ્પ D સ્તરો ભેળવી દે છે: route એ URL અને method ને handler સાથે જોડે છે, જ્યારે schema data નું વર્ણન કરે છે; બંને app ના જુદા ભાગોમાં રહે છે (routes અને controllers સામે models). Schema data નો આકાર વર્ણવે છે, અને Model ક્રિયાઓ આપે છે.

Think first

MongoDB schema વગરનું હોય તો Mongoose ના schemas શા માટે ઉમેરવા?

MongoDB તમને કોઈ પણ આકારનાં documents સંઘરવા દે છે. તો એની ઉપર Mongoose નું schema શા માટે લાદવું? વિચારીને પછી tap કરો.

Show the answer

કારણ કે MongoDB ની લવચીકતા શક્તિશાળી છે પણ શિસ્ત વગર જોખમી છે, અને Mongoose નું schema તમને રચના, ચકાસણી અને સ્પષ્ટતા આપે છે અને છતાં ફાયદા જાળવી રાખે છે. MongoDB પોતે તો એક જ collection માં સાવ જુદા જુદા આકારનાં documents ખુશીથી સંઘરી લેશે, જેમ કે એક event જેમાં name અને date હોય, બીજું જેમાં date જ ન હોય, અને ત્રીજું જેમાં 'date' ખરી તારીખને બદલે string તરીકે સંઘરાયું હોય. એ છૂટ ઝડપથી અવ્યવસ્થિત અને અસંગત data તરફ દોરી જાય છે, જેના પર તમારો code ભરોસો ન કરી શકે, અને પછી જ્યારે તમે માની લો કે દરેક event પાસે યોગ્ય date છે ત્યારે ભૂલો થાય છે. Mongoose નું schema તમારો ધારેલો આકાર જાહેર કરે છે: name એ ફરજિયાત String છે, date એ ફરજિયાત Date છે, અને seats એ Number છે જેની મૂળભૂત કિંમત 50 છે. હવે Mongoose આવતા data ને એ આકાર સામે ચકાસે છે, અને જે documents બંધ ન બેસે એમને નકારે છે કે ચિહ્નિત કરે છે, જેથી તમારું collection સુસંગત રહે અને તમારો code એના પર ભરોસો કરી શકે. Schema code વાંચનાર કોઈ પણ માટે data ના ઢાંચાનું દસ્તાવેજીકરણ પણ કરે છે (એક નજરે દેખાય કે Event એટલે શું), સમજદાર મૂળભૂત કિંમતો અને પ્રકારનું રૂપાંતર આપે છે, અને hooks તથા સંબંધો જેવી સુવિધાઓ પણ શક્ય બનાવે છે. મહત્ત્વનું એ કે તમે MongoDB ના ફાયદા ગુમાવતા નથી, કારણ કે તમે schema ને સમય જતાં બદલી શકો છો, અને નીચે MongoDB ઝડપી તથા JSON સાથે ભળતું રહે છે. એટલે Mongoose એ MongoDB ની લવચીકતા પર જાણી જોઈને, પોતાની મરજીથી ઉમેરેલું વ્યવસ્થાનું સ્તર છે: જ્યાં છૂટ જોઈએ ત્યાં છૂટ, અને જ્યાં ખાતરી જોઈએ ત્યાં ખાતરી. મરજીથી લીધેલી રચના મૂળભૂત અંધાધૂંધી કરતાં ચડિયાતી છે.

Summary

Key takeaways

  • Models (data ના આકાર) ને controllers (route નું logic) થી અલગ પાડીને backend વ્યવસ્થિત કરો.
  • Mongoose એ Node માં MongoDB માટેની ODM છે: Schema વ્યાખ્યાયિત કરો (fields અને પ્રકારો), અને એને Model માં ફેરવો.
  • MongoDB JSON જેવાં documents ને collections માં સંઘરે છે; અને schema એના પર રચના તથા ચકાસણી ઉમેરે છે.
  • Model સાથે CRUD: create (create કે save), read (find કે findById), update (findByIdAndUpdate), delete (findByIdAndDelete); આ બધાં Promises પાછાં આપે છે (async અને await વાપરો).
  • Controllers routes ના જવાબમાં model ને બોલાવે છે; અને એમને અલગ files માં રાખવાથી routes પાતળાં રહે છે.
  • Mongoose ના schemas MongoDB ની લવચીકતા પર ચકાસણી અને સુસંગતતા ઉમેરે છે.
  • યાદ રાખવાની કડી: models data ને આકાર આપે, controllers logic સાચવે, અને Mongoose CRUD આપે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Building Web App Components and Backend Integration

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Organizing Code with Models and Controllers: structuring backend with models, services and controllers; data models for users/products using MongoDB (Mongoose); basic CRUD operations using Express and MongoDB · Fundamentals of Full Stack Web Development (Major-15-01) · Gri-Learn