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

前端 未结 3 1907
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 15:38

I am having trouble understanding how to structure an ancestor tree with several decedents. Suppose I have a model like this (every Entity has a Long id):



        
3条回答
  •  情歌与酒
    2021-01-23 15:47

    First of all, I agree with Konqi when he says that you should carefully design your ancestor/keys model to get the right level of consistency that you want, taking in consideration the trade-offs of write/sec throughput.

    Assuming that you still want the user -> post -> comment design, to deal with datastore ancestor key references like this, we came to a solution that is almost similar to your idea. We create string representations of the keys. For your specific case, it would be:

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

    We also have a convenience type to encapsulate that representation, called IdRef.

    Everytime an Entity is retrieved from the datastore store we construct this representation and for every API request we translate it from the given string.

    Using that approach, we can easily expose APIs like that:

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

    I maintain an opensource project that, among other features, solves the problem you are mentioning. It is a Java DSL designed to expose RESTful APIs from your appengine datastore models.

    If you are interested, here is a gist that exemplifies your use case.

提交回复
热议问题