Adding a data layer to our Next.js app
In the traditional backend structure, we had the database layer, the services layer, and the routes layer. In a modern full-stack Next.js app, we don’t need the routes layer of our backend because we can directly interface with it in RSCs. So, we only need to have the database layer and a data layer to provide functions that access the database. Theoretically, we could directly access the database in RSCs, but it is best practice to have specific functions that access it in certain ways. Defining such functions allows us to clearly define what data is accessible (and thus avoid accidentally leaking too much information). They are also more reusable and make it easier to unit-test and find potential vulnerabilities (for example, via a penetration test) in the data layer.
To recap, there are three main data-handling approaches:
- HTTP APIs: We used these in previous chapters to implement our blog app. These can be useful when separate...