Skip to content

Instantly share code, notes, and snippets.

@feroult
Last active January 20, 2016 11:51
Show Gist options
  • Save feroult/bd65f5c1b1d4b7f76bdd to your computer and use it in GitHub Desktop.
Save feroult/bd65f5c1b1d4b7f76bdd to your computer and use it in GitHub Desktop.
YAWP! Hierarchy Mapping

Note: You should consider when to use ancestor/key design with care. You'll get a limit of 1 write/sec for each entity group.

First, map your entity endpoints:

  @Endpoint(path = "/users")
  public class User {
      @Id
      IdRef<User> id;
  
  	// other fields
  }
  
  @Endpoint(path = "/posts")
  public class Post {
      @Id
      private IdRef<Post> id;
  
  	@ParentId
  	IdRef<User> userId;
  
  	// other fields
  }

  @Endpoint(path = "/comments")
  public class Comment {
      @Id
      IdRef<Comment> id;
  
      @ParentId
      IdRef<Post> postId;
  
      // other fields
  }

Then, an example on how to create an action for a given comment:

  public class CommentLikeAction extends Action<Comment> {
      @PUT
      public void like(IdRef<Comment> id) {
          Comment comment = id.fetch();
  		comment.like();
  		yawp.save(comment);
      }
  }

This will create the following HTTP API:

PUT /users/{userId}/posts/{postId}/comments/{commentId}/like

Or, from the YAWP!'s Javascript client you may call:

  yawp('/users/{userId}/posts/{postId}/comments/{commentId}').put('like').done( function() {} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment