0
0
Expressframework~10 mins

Repository pattern for data access in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Repository pattern for data access
Start
Controller calls Repository
Repository handles data logic
Repository calls Database
Database returns data
Repository returns data to Controller
Controller sends response
End
The controller asks the repository for data. The repository talks to the database and returns data back. The controller then sends the response.
Execution Sample
Express
class UserRepository {
  async getUser(id) {
    return db.findUserById(id);
  }
}

const repo = new UserRepository();

app.get('/user/:id', async (req, res) => {
  const user = await repo.getUser(req.params.id);
  res.json(user);
});
This code shows a repository class fetching a user by ID and the Express route using it to send JSON response.
Execution Table
StepActionInputRepository Method CalledDatabase CallReturned DataResponse Sent
1HTTP GET /user/5 receivedid=5NoNoNoNo
2Controller calls repo.getUser(5)id=5getUser(5)Yes: db.findUserById(5)NoNo
3Database returns user dataid=5getUser(5)db.findUserById(5){id:5, name:'Anna'}No
4Repository returns data to controllerid=5getUser(5)No{id:5, name:'Anna'}No
5Controller sends JSON responseid=5NoNoNo{id:5, name:'Anna'}
6Request completeid=5NoNoNoResponse sent
💡 Request ends after controller sends JSON response with user data.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
idundefined55555
userundefinedundefinedundefined{id:5, name:'Anna'}{id:5, name:'Anna'}{id:5, name:'Anna'}
Key Moments - 3 Insights
Why does the controller not access the database directly?
The controller calls the repository (see step 2 in execution_table) to keep data logic separate and easier to manage.
What happens if the database call fails?
The repository would handle errors before returning data to the controller, preventing direct database errors from reaching the controller.
Why is the repository method async?
Because database calls take time, the repository uses async to wait for data before returning it (step 3 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what data does the repository return to the controller at step 4?
A{id:5, name:'Anna'}
Bundefined
Cdb.findUserById(5)
DNo data
💡 Hint
Check the 'Returned Data' column at step 4 in execution_table.
At which step does the controller send the JSON response?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Response Sent' column in execution_table.
If the repository method was not async, what would happen?
AController would wait correctly for data
BController might get undefined before data arrives
CDatabase call would be faster
DNo change in behavior
💡 Hint
Refer to key_moments about async behavior and step 3 in execution_table.
Concept Snapshot
Repository pattern separates data logic from controllers.
Controller calls repository methods.
Repository handles database calls.
Repository methods are async to wait for data.
This keeps code clean and easier to maintain.
Full Transcript
The repository pattern in Express means the controller does not talk directly to the database. Instead, it calls a repository class that handles all data access. When a request comes in, the controller calls a repository method with needed parameters. The repository then calls the database asynchronously to get data. Once the data returns, the repository sends it back to the controller. The controller then sends the data as a JSON response. This separation helps keep code organized and easier to manage. The repository methods are async because database calls take time. The controller waits for the repository to finish before sending the response.