Manage nested list of entities within entities in Google Cloud Datastore

后端 未结 1 1740
长发绾君心
长发绾君心 2021-01-20 04:33

I am new to Datastore and I am trying to create a simple app that tracks books borrowing.

I would like the DB schema to be as follows:

books:
 book_i         


        
1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 04:54

    In general this is not a scalable approach: every time a user borrows a book you'd have to re-write both the user and book entities, which will get progressively slower as both entities will keep growing.

    I'd suggest a different approach: add a new type to your schema, let's call it borrowed_book, representing a book boorowed by a user:

    borrowed_book:
      book_id
      user_id
      timestamp
    

    Now every time a user borrows a book you'd simply create one such borrowed_book entity, pointing to both the book and the user. No changes to the user or the book entities. And no nesting required.

    Side note: I'd place the firstname and email properties under the user entity type, they don't really belong to the borrowing event where they would be duplicated every time such event occurs for the same user.

    Also try to not get confused by the ancestry - it is not required for establishing relationships, see E-commerce Product Categories in Google App Engine (Python)

    0 讨论(0)
提交回复
热议问题