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

前端 未结 3 1909
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  猫巷女王i
    2021-01-23 15:50

    Based on the info provided you have two options:

    • Pass full keys around and design your API around that
    • decouple your entities ancestor relationships, so that no entity has a parent

    Note: after writing this all out, I realized the end API looks the same really either way:

    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} 
    

    Full keys

    In this case, {key} is the websafe key.

    Advantages:

    • you can maintain transactional and consistency control
    • allows you to change parent entity relationship later without migrating old data and breaking old links (in my experience this is a massive win)
    • Allows you to mix kinds (e.g. Have a Post kind and a Post2 kind, or an ImagePost kind - useful for polymorphism or for breaking migrations)

    Decouple entities as ancestors

    In this case {key} is the id, and there is no entity hierarchy

    Dis/Advantages:

    • Simplicity
    • You need to infer the kind based on the URL
    • listing posts for a user or comments for a post would always be eventual
    • no migration path if you need to introduce transaction groups
    Overall benefits of structuring your API like this:
    • internal coupling for consistency and transactionality not exposed through API
    • aligns with restful API as resource concept
    • supports either model

    In my experience you should absolutely do the first option, you will probably get your data model wrong a few times, and being able to change/migrate it is an absolute win.

提交回复
热议问题