I'm using mongodb, node.js and backbone.js to build an app that will serve as a learning experience for me. I'm interested to know what is best practice for fetching related objects from a REST API for this sort of thing.
我使用mongodb,节点。js和骨干。js构建一个app,作为我的学习体验。我想知道从REST API中获取相关对象的最佳实践是什么。
Let's say we have "post" objects and "user" objects, with posts having a "userId" property that links them to users.
假设我们有“post”对象和“user”对象,而post具有“userId”属性,可以将它们链接到用户。
Should you:
你应该:
- Fetch a post object on the client side. Inspect the "userId" property of the post and then separately fetch that user. This seems to be nice and simple in terms of server-side code, but could end up making lots of requests if things get complicated further down the line.
- 在客户端获取post对象。检查post的“userId”属性,然后分别获取该用户。就服务器端代码而言,这看起来很好,也很简单,但是如果事情变得更复杂的话,可能会导致大量的请求。
- Fetch a post object, and have the server "make the join" and give you the related user object, in the same request.
- 获取一个post对象,并让服务器“make the join”并在相同的请求中为您提供相关的用户对象。
Does anyone have some experience that they could share?
有没有人有他们可以分享的经验?
Thanks
谢谢
2 个解决方案
#1
2
It's hard to answer this without a specific UI story, but based on what you have provided, I'd say #1 is closer to the approach I would take. However, instead of 'inspecting' the post for the userId, provide the link to the user with the rel=user
and simply follow that link for your user resource. I prefer the HATEOAS paradigm where these resources are navigated through explicit links constructed by the server, not the client.
如果没有一个特定的UI故事,很难回答这个问题,但是根据您提供的内容,我想说#1更接近于我将要采用的方法。但是,不需要“检查”用户id的文章,只需使用rel=user向用户提供链接,并为用户资源跟踪该链接。我更喜欢HATEOAS模式,这些资源通过由服务器构建的显式链接进行导航,而不是客户端。
This obviously will result in more XHR's but if that is a concern then I'd suggest that your view is requiring a view model which serves it without joins. This is done by exposing a dedicated model that serves the client simply without making the client responsible for constructing data it needs. hth,
这显然会导致更多的XHR,但是如果这是一个问题,那么我建议您的视图需要一个没有连接的视图模型。这是通过公开一个专门的模型来实现的,该模型只服务于客户端,而不让客户端负责构建它需要的数据。hth,
mike
迈克
#2
1
MongoDB does not have a feature to "make the join" on the server, so the relevant question is probably more one of data modelling and whether it makes more sense for you to link or embed the related user data.
MongoDB没有在服务器上“进行连接”的特性,因此相关的问题可能更多的是数据建模,以及链接或嵌入相关用户数据是否更有意义。
Fetch a post object on the client side. Inspect the "userId" property
of the post and then separately fetch that user. This seems to be
nice and simple in terms of server-side code, but could end up making
lots of requests if things get complicated further down the line.
The closest to this option would be fetching the post and then querying for related information for userIDs mentioned in the post using the $in
operator. This may result in slow performance given a large number of userIDs, as effectively the server is still doing a query to look up each userID in the index.
最接近这个选项的是获取post,然后查询使用$in操作符在post中提到的相关信息。考虑到大量的userIDs,这可能会导致性能下降,因为服务器仍然在执行查询来查找索引中的每个userID。
Another common approach to consider is a hybrid option:
考虑的另一种常见方法是混合选择:
-
embed some minimal user data in the post, for example the userID (a database reference to their user data in another collection) and some details such as their user name.
在post中嵌入一些最小的用户数据,例如userID(在另一个集合中对用户数据的数据库引用)和一些细节,如用户名。
-
lookup additional details in an AJAX request as needed (for example, as a hover over the user name).
根据需要查找AJAX请求中的附加细节(例如,将鼠标悬停在用户名上)。
What works better really depends on your use case and the information you want to display on a page in your application.
更好的工作方式取决于您的用例和希望在应用程序中的页面上显示的信息。
#1
2
It's hard to answer this without a specific UI story, but based on what you have provided, I'd say #1 is closer to the approach I would take. However, instead of 'inspecting' the post for the userId, provide the link to the user with the rel=user
and simply follow that link for your user resource. I prefer the HATEOAS paradigm where these resources are navigated through explicit links constructed by the server, not the client.
如果没有一个特定的UI故事,很难回答这个问题,但是根据您提供的内容,我想说#1更接近于我将要采用的方法。但是,不需要“检查”用户id的文章,只需使用rel=user向用户提供链接,并为用户资源跟踪该链接。我更喜欢HATEOAS模式,这些资源通过由服务器构建的显式链接进行导航,而不是客户端。
This obviously will result in more XHR's but if that is a concern then I'd suggest that your view is requiring a view model which serves it without joins. This is done by exposing a dedicated model that serves the client simply without making the client responsible for constructing data it needs. hth,
这显然会导致更多的XHR,但是如果这是一个问题,那么我建议您的视图需要一个没有连接的视图模型。这是通过公开一个专门的模型来实现的,该模型只服务于客户端,而不让客户端负责构建它需要的数据。hth,
mike
迈克
#2
1
MongoDB does not have a feature to "make the join" on the server, so the relevant question is probably more one of data modelling and whether it makes more sense for you to link or embed the related user data.
MongoDB没有在服务器上“进行连接”的特性,因此相关的问题可能更多的是数据建模,以及链接或嵌入相关用户数据是否更有意义。
Fetch a post object on the client side. Inspect the "userId" property
of the post and then separately fetch that user. This seems to be
nice and simple in terms of server-side code, but could end up making
lots of requests if things get complicated further down the line.
The closest to this option would be fetching the post and then querying for related information for userIDs mentioned in the post using the $in
operator. This may result in slow performance given a large number of userIDs, as effectively the server is still doing a query to look up each userID in the index.
最接近这个选项的是获取post,然后查询使用$in操作符在post中提到的相关信息。考虑到大量的userIDs,这可能会导致性能下降,因为服务器仍然在执行查询来查找索引中的每个userID。
Another common approach to consider is a hybrid option:
考虑的另一种常见方法是混合选择:
-
embed some minimal user data in the post, for example the userID (a database reference to their user data in another collection) and some details such as their user name.
在post中嵌入一些最小的用户数据,例如userID(在另一个集合中对用户数据的数据库引用)和一些细节,如用户名。
-
lookup additional details in an AJAX request as needed (for example, as a hover over the user name).
根据需要查找AJAX请求中的附加细节(例如,将鼠标悬停在用户名上)。
What works better really depends on your use case and the information you want to display on a page in your application.
更好的工作方式取决于您的用例和希望在应用程序中的页面上显示的信息。