Database – Relationship between REST APIs and Databases

apidatabaserestsqlweb-applications

I've been studying databases and rest APIs lately and I have a question about the relationship between the two.

Imagine I have a database with three tables, STUDENTS, ENROLLED, and CLASSES.

STUDENTS and CLASSES denote the entities students and classes, whereas the ENROLLED table denotes their relationship.

If I were to map this dataset in a rest api, would I just have 3 different CRUD routes with the three tables, eg ('/students', '/classes', '/enrolled')?

And that question goes for REST APIs in general — when you write/make a REST API, are you just constructing a 1:1 mapping of your database?

Just trying to really nail down my conceptual understanding of the relationship between the two.

Best Answer

And that question goes for REST APIs in general -- when you write/make a REST API, are you just constructing a 1:1 mapping of your database?

No.

The database is just an implementation detail.

You can write a bunch of resources that map directly to your domain objects, but that isn't particularly RESTful, in and of itself.

REST isn't about the domain, so much as it is about application state.

If the resources are the application state -- which is to say, if they are analogous to web pages, with links and so on to define for the client what can happen next, that's REST -- but application state probably doesn't look too much like your domain objects (though it may include representations of them in the hypermedia).

On the other hand, if the resources are just loaded by the application state -- if they are analogous to images, where the client learns what images to load by reading the hypermedia, then those routes probably aren't REST. They certainly don't need to be; you aren't telling clients to use these routes, you are telling them to point their browser at a web page that uses those routes.

Once you've got the representation of application state right, REST really doesn't care how you spell your routes, or organize them, or anything. REST only really cares that they are discovered by inspecting the hypermedia.

Related Topic