Answer the question
In order to leave comments, you need to log in
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} // псевдокод
Answer the question
In order to leave comments, you need to log in
The second option is devilishly unsafe, because. anything can come from the client. + you just transfer the complexity from the server to the client.
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?
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
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 questionAsk a Question
731 491 924 answers to any question