How to create API methods in Google App Engine that have several grandfathers / ancestors

I am having trouble understanding how to structure a tree of ancestors with several decades. Suppose I have a model like this (each object has Long id):

User
    -Post
        -Comment

Where Commentis the grandson User.

Which is really annoying, CommentI need to insert; I need to generate a key Post. And to generate the key, PostI also need to know the identifier User:

Key<Post> postKey = Key.create(Key.create(User.class, userId), Post.class, postId);

This is a problem for me, because when trying to insert a comment into the data store, I need to also pass to userId and postId only to generate the key Post.

Similarly, it is annoying to try to get one post, because I need to pass both userId and postId to generate the key.

I am looking for the best way to structure my model and API methods without having to pass all these ancestor identifiers to my methods . I considered saving websafeKey in each Post and Comment object as a property similar to this:

String websafeKey = Key.create(Key.create(User.class, userId), Post.class, postId).getString();
Key<Post> key = Key.create(websafeKey);

Then I could use a key for each post and comment (and other children of these entities) right there in Entity. Then, probably, I would not have to pass all these ancestor identifiers to my API methods all the time.

Not sure if this is a good idea.

+1
source share
3

:

  • API
  • ,

: , , API :

GET /user/{key} - get user info
POST /user/{key}/post/ - create post 
GET /post/{key} - get post
POST /post/{key}/comment/ - create comment
GET /comment/{key} 

{key} - websafe.

:

  • , ( ).
  • (, Post Post2 ImagePost - ).

{} - ,

Dis/:

  • , URL
  • ,
API:
  • , API
  • API

, , , , / - .

+2

, Konqi, , / , write/sec .

, - , → → , datastore , , , . . , :

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

, IdRef<T>.

, , API .

, API:

GET /users/{userId}/posts/{postId}/comments/{commentId}
POST /users/{userId}/posts/{postId}/comments/{commentId}
DELETE /users/{userId}/posts/{postId}/comments/{commentId}

, , , , . Java DSL, API RESTful .

, gist, .

+1

@feroult, . .

, , , . "" . , , , API RESTful , . , , .

, , . , , . , . /users/ {userId}/posts/{postId}/comments/{commentId} RESTful api.

, , , . . Google Datastore .

This means that the problem can be reduced to one of the semantics. Perhaps the trade-off is to create an equivalent factory client key that can generate the key from the ancestor identifiers and then send it to the server. In this case, repetition can be avoided and the RESTful API will still be clean.

It would be interesting to hear other thoughts.

0
source

All Articles