I’m a front end developer…I’ve never done ANY backend, although I did take a course in databases once. (I have a comp sci degree; I’m a competent programmer, I just haven’t done any backend web stuff before.)
So I’m trying to figure out how the framework relates to the database. Presumably they’re set up separately, and the framework receives requests from the website and sends them to the database, then receives the response and handles it appropriately to deliver the content correctly? It’s basically a complex intermediary for all the web-app logic? Am I far off?
Need a good explanation of this.
There are many possibilities to connect a web-app to a database (and a lot of different databases to choose from).
I think you should first consider the normal case (as e.g. most used in the RoR community):
a relational DB and a ORM (the object relational mapper – in RoR it is ActiveRecord based on the active record pattern).
a very high-level (and superficial) introduction:
- the database is a separate entity and process.
- the app defines models that describe both the tables in the relational database (RoR will create the necessary SQL commands) and the the way the data in the database can be accessed and modified from the web-app using objects and properties.
- in your web-app-code you access the database only by interacting with those models. e.g. in RoR:
- the users make requests to the web-app
- the requests are handled in the controller.
- the controller creates or retrieves records (e.g. by invoking
find(<id>)
on the class object) from the DB. these records appear as objects (or arrays of objects). - the controller modifies them, and
- stores the result back to the DB (e.g. by invoking
save()
on the object). - the response e.g. shows the result to the user.
2
Presumably, as a front end developer, you know how your Rails controller handles standard HTTP requests. Rails MVP framework also has models. Rails has support for ActiveRecord models. These are basically classes that are linked to some underlining database. In your controller, you can make calls on this model to read or modify data in the database.
The way frameworks function in relation to the user’s request and the database is not very different from any tiny piece of code acting as an intermediary between the two. Any ‘web-app logic’ is situation between the two, be it a complex MVC/MVP framework or a 5 line PHP file. They function to determine the user’s request, make a connection to the database and retrieve stuff, and present this to the user.
A framework subdvidides several steps that often occur in this intermediary step. As the name implies MVP has Models that structure the data retrieved from the database in a useful way, a View where the end-result is given to the user, and a Presenter which ties these two together and may perform some additional logic.
Where as a website that doesn’t change can just serve static pages, a web application like amazon has got a lot to do to present a page that represents a product: put simply, it can retrieve the product details based on an identifier passed in and build a web page that displays those details.
I would say a web framework relates to a database in the following way:
“A web framework is used to build/run a program that requests from a webserver run to build responses that can use data stored in database.”