Such situation:

  1. There is a database on a server implemented in Java.
  2. There are objects Customer and Photo .
  3. Each Customer corresponds to a Photo .

How to implement this correctly in JavaScript for deserialization from JSON?
Does each Customer need to add the Photo property and the Photo property of the Customer ?

  • That is, "do I need to fully reproduce the server data scheme with all the links on the client?". Usually not, not necessary. In general, in this form the task is obscure. Try to formulate your problem in some natural language or a clear example. - Duck Learns to Take Cover
  • Thank you for responding. Now try ... Can I chat? - Nikolay Egorov
  • Try, there is access? chat.stackexchange.com/rooms/43051 - Duck Learns to Take Cover
  • one
    You need to send to the client what you need on the client. And the fact that it is logical to send on the relevant requests to the server API. At the request of the photo you need to send photos, on the request of the user data - send the user data. At the request "GIVE ME EVERYTHING" - forward all. But the query "give me everything" in theory should not be. Can one object be immediately sent by the property of another? - yes you can. I say, the problem is not quite clear, and I’m far from sure that you decide what you need to solve) - Duck Learns to Take Cover
  • one
    It is necessary to understand that "records in the database", "data structure on the backup" and "data structure on the client" are generally speaking independent entities. And even though they have a lot in common, they don’t have to be absolutely alike - Duck Learns to Hide

2 answers 2

No no need.

one.
The database will grow over time, and it will no longer be possible to transfer it to the client, not only in a reasonable time, and it may not work at all.

2
You did not specify what is your database. If MongoDB, then there itself built data structure already solved your question. If any SQL, see clause 3.

3
If the connection is один-к-одному , then it is enough to replace it with the object's attachment: Customer to attach Photo , or vice versa - it depends on what data the request is executed from ( need to specify the question ).

four.
Surely you need to pull data either by Customer or Photo . If you are building a pivot table, see clause 3. If you have a form with fields dependent on one another, then these are several requests to the server behind a chain of field dependencies (for example, if you chose a specific Customer , you only got Photo , and what else is needed for other fields dependent on it).

Shl
The answer is vague of course, but the question you have is the same is not specific ...

  • I almost got the answer to the question ... SQL database. Those. if I understood correctly, then in my Data object, which I use for sending, there should be Customer and Photo as separate objects and I transfer them, depending on the need. - Nikolay Egorov
  • @NikolayEgorov, I seriously suspect that you have what is called the hu-problems on the English stack. Meta.stackexchange.com/questions/66377/what-is-the-xy-problem This is when you try to solve not the real problem that exists, but the problem that you came up with in the course, hoping to solve the one that is. - Duck Learns to Take Cover
  • Nearly. As a rule, you do not need to send database records to the client - specific data are enough. For example, when requesting users with their avators, it’s enough to send { userId: 1, nick: "User2016", name:"Виктор", avator: "images/pic38547593.jpg" } where all properties are taken from the users table (but at the same time properties in there are more table entries, they are simply not needed in this query), except for the avator property that came from the associated photos table (and from all the table properties, for example, only the path to the thumbnail was taken). - t1nk
  • one
    And it seems to me that the problem here is not in addressing the database, but in misunderstanding how to drive a picture over the network) In general, you need to carefully interrogate in the chat room, with torture tools - Duck Learns to Hide
  • one
    @ t1nk not, the problem is how to implement this correctly in javascript. I only recently began to delve into it. I understood the essence of the answer .... It is not necessary to send the entire object with all the connections. Type: Data {Customer, Message, Photo .....}, and already here to invest concrete are necessary. So? Those. I transfer user data from the client to the server, and just a photo of it back? - Nikolay Egorov

Understood, if anyone is interested. The problem was in the cyclical nature of the fields (both the user has a photo and the photo has a user). I solved the problem using the Gson (google) library and the @Expose annotation (serialize = false / true, deserialize = false / true). Here is an excellent article http://websystique.com/java/json/gson-json-annotations-example/ Thank you all for your help.