parse.com - how to implement the likes post pattern in parse -


given standard use case of user able post.

and using parse example

var user = parse.user.current(); var relation = user.relation("likes"); relation.add(post); user.save(); 

what's best way keep count of total likes per post? should i:

  1. increment total likes counter on post object beforesave cloud code hook on user looks obj.isdirty("likes") , figures out object added somehow (i'm not sure find post added in case)

  2. create separate likes object in parse , count queries against (probably not because parse recommends against doing count queries)

  3. increment likes counter on product client-side @ same time user adding post users relation (security issue , synchronization issue?)

its good, thoughtful question. looking @ ideas:

  1. seems natural put likes counter on post, , natural maintain on beforesave of user. you're right dirty info (as far know) doesn't tell enough to-many side of relation new. i'd exclude otherwise fine idea because of this.

  2. also reasonable consider "join table", , believe parse doing anyway to-many relation. benefit can use count query. unaware parse recommends against doing count queries. you'd have maintain table, , step users posts or posts users, think workable.

  3. nothing wrong having client increment on post. you're right again means you'll permitting clients write posts not own, posts' acl have weaker. wouldn't worry synch... if needed moonshot precision on timing liking posts , counting likes, design else entirely, without parse.com.

i think i'd go idea (4). idea (3) run in cloud function. (which manual form of beforesave, idea (1)).

parse.cloud.define("userlikespost", function(request, response) {     // can leave posts acl restricted creator     parse.cloud.usemasterkey();     var post;     var postid = request.params.postid;     var query = new parse.query("post");     query.get(postid).then(function(result) {         post = result;         post.increment("likecount", 1);         return post.save();     }).then(function() {         var user = request.user;         user.add("likes", post);         return user.save();     }).then(function(result) {         response.success(result);     }, function(error) {         response.error(error);     }); }); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -