Storing 'shared' items MongoDB in application with RESTful API -
i creating application in users can share articles.
currently, when user
shares article
, adds article
id of shared item array called shares
on user
.
var userschema = { // `shareditems` list of article ids user has shared shareditems: { type: array } };
however, because need way query shared items multiple user ids (similar news feed sort of query), decided go down route of creating separate collection shared items.
var shareschema = { // id of user shared article userid: { type: string }, // id of article shared articleid: { type: string }, datecreated: { type: date } };
to tie in application code, when user
updated, there check whether or not shareditems
array on user
updated – if is, task delegated shares
collection add or remove matches respectively. have done because, when user shares item, client-side application has worry making post
request user
resource, rather making post
user
and share
resources. trouble here relying on replication, , worry various reasons, there inaccuracies.
the need shareditems
array on user
entity because, when articles loaded, application must detect items have been shared logged in user. application parses response each loaded article
, checks whether or not current user
has article
id in shareditems
array – if does, adds property of isshared
article.
the other alternative achieving of — can think of — remove shareditems
array user schema, , add sort of authentication api, get
requests article
resource aware of current user is, , thus, back-end can worry checking whether or not current user has shared article
sent in response, instead of doing parsing on client-side.
the trouble have not want api require authentication. articles should accessible users, logged in or out (get
on article
resource). however, want logged in users able share articles (post
share
resource, or post
user
resource delegates creation/deletions of share documents).
how handle in terms of mongodb collections , schemas?
i suggest sharecount
field in get /article
resource (to prevent unauthenticated exposure), , use asynchronous (and authenticated) api call get /user
can use mark articles shared after initial load.
if this, get /article
resource doesn't need additional work behind scenes, , every request same information (with sharecount
). when user logged in design care mark shared.
Comments
Post a Comment