管理Google Cloud Datastore中实体内的实体嵌套列表 [英] Manage nested list of entities within entities in Google Cloud Datastore

查看:96
本文介绍了管理Google Cloud Datastore中实体内的实体嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Datastore的新手,我试图创建一个简单的应用程序来跟踪借书。



我希望数据库模式如下所示:

p>

 书籍:
book_id
名称:
借阅:
时间戳:
user_id
名字
电子邮件

用户:
名称:
借用:
时间戳:
book_id

我使用控制台来配置数据库。



I已定义两种类型:书籍用户



商家逻辑:


  • 1本书可以借用n个用户。

  • 1个用户可以借用n本书



基本上,当用户借用一本书时,我希望发生以下两个插入:


  • 新的借用条目附加到Book类型的图书实体,其中当前时间戳记为key,user_id,firstname和email为属性

  • 新借款ed条目附加到ty用户的用户实体,并将当前时间戳记作为关键字,将book_id作为属性



我该如何实现这(管理嵌套列表儿童)与数据存储?通过嵌入式实体? Parent ref?



下面是我期望的一个例子:

  {
books:{
book1:{
name:book number 1,
borrowing:{
1234567890 :{
user_id:user1,
firstname:john,
email:john@example.com
},
2234567890:{
user_id:user2,
firstname:robin,
email:robin@example.com
}




users:{
user1:{
firstname: robin,
email:robin@example.com,
借入:{
1234567890:{
book_id:book1
},
3247829398:{
book_id:book99
}
}
},
...
}
}


解决方案

一般而言,这不是一种可扩展的方法:每次用户借用一本书时,都必须重写 user book 个实体,随着两个实体的不断增长,实体将逐渐变慢。



我会建议一种不同的方法:为您的模式添加一个新类型,我们称之为 borrowed_book ,表示由用户开发的一本书:

  borrowed_book:
book_id
user_id
时间戳

现在,每当用户借用一本书时,您只需创建一个这样的 borrowed_book 实体,指向到 book 用户。对用户 book 实体没有更改。 不需要嵌套



注意:我会将 firstname 电子邮件属性在用户实体类型下,它们并不真正属于借用事件,它们每次都会被复制同样的用户会发生这样的事件。



另外,不要让祖先感到困惑 - 它不是建立关系所必需的,请参阅 Google App Engine(Python)中的电子商务产品类别


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_id
  name:
  borrowing:
   timestamp:
    user_id
    firstname
    email

users:
 name:
 borrowed:
  timestamp:
   book_id

I am using the console to configure the DB.

I have defined two Kinds: Books and User

Business logic:

  • 1 book can be borrowed by n users.
  • 1 user can borrow n books

Basically, when a user borrows a book I want the two following inserts to occur:

  • a new borrowing entry is appended to the book entity of type Book, with the current timestamp as key and user_id, firstname and email as properties
  • a new borrowed entry is appended to the user entity of ty User, with the current timestamp as key and the book_id as property

How can I achieve this (managing nested lists children) with Datastore? Through embedded entities? Parent ref?

Here is an example of what I expect:

{
  "books": {
      "book1": {
          "name": "book number 1",
          "borrowing": {
              "1234567890": {
                  "user_id": "user1",
                  "firstname": "john",
                  "email": "john@example.com"
              },
              "2234567890": {
                  "user_id": "user2",
                  "firstname": "robin",
                  "email": "robin@example.com"
              }
          }
      }
      ...
  },
    "users": {
        "user1": {
            "firstname": "robin",
            "email": "robin@example.com",
            "borrowed": {
                "1234567890": {
                    "book_id": "book1"
                },
                "3247829398": {
                    "book_id": "book99"
                }
            }
        },
        ...
    }
}

解决方案

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)

这篇关于管理Google Cloud Datastore中实体内的实体嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆