U
U
Urukhayy2017-08-16 10:18:55
Node.js
Urukhayy, 2017-08-16 10:18:55

Abstraction levels or is it possible to reduce the amount of code this way?

Task:
There is a back-end on Node + Express. We need to implement CRUD capability for three collections (human, tree, planet) in Mongo. All requests to work with the collection come from HTTP requests, through Express. Each collection has a different number of fields.
There are two options to implement:
1. Create 3 * 4 routers ( router.get(/create-planet), (/update-planet) etc. ) in Express, and also create 3 * 4 functions to work with Mongo . In general, manually write db.collection.insert many times, and so with all kinds of operations.
2. Raise the level of abstraction, and create only 1 router ( router.post(/multi-router)) and 1 super-abstract method for working with the database. JSON will come to this router, which will contain the type of operation, the name of the collection, and data for the operation. And the universal method will parse JSON, extract the type of operation and insert it into a database query:

db.collection[operationString]{operationParams} // псевдокод

That is, so that the programmer does not write either the name of the collections in the source code, or the type of operation with the collections. To take it all to the next level.
In general, the second way implies that the program will "create" itself at runtime, just a few lines of code, and all the work will be done by the input data. But there is no routine and no need to copy-paste lines of code with repeated calls. Is it normal? Or what's right?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
abberati, 2017-08-16
@abberati

The second option is devilishly unsafe, because. anything can come from the client. + you just transfer the complexity from the server to the client.

V
Vlad, 2017-08-16
@d1skort

And what will you do when it turns out that, for example, it is impossible to remove the planet?
And if it turns out that people can only update the last name, but not the number of hands?

K
Konstantin Kitmanov, 2017-08-16
@k12th

Fine. Only can it be better to take restify ?
PS I'm lying, restify is the same express, side view. Here: loopback.io/getting-started

Y
Yaroslav Sivakov, 2017-08-16
@yar3333

If you have a project where all objects are really the same in terms of processing by the server, then you can do it universally. But usually this does not happen - usually for each object you need to check relationships, access rights, calculate something that the client did not send, etc. Therefore, in a real project - definitely a simpler, but longer version with 3 controllers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question