Google App Engine:Go语言中的一对多关系

huangapple go评论90阅读模式
英文:

Google App Engine: One To Many relations in Go

问题

我目前正在决定为一个(小型)社交网络选择使用哪种技术栈。我希望使用golang和Google App Engine进行编码,因为这样可以得到干净、快速的代码,并享受现代云服务和托管的所有优势。

在GAE中使用golang,你可以如下实现一对多和/或多对一的关系。根据文档,只要自定义类型的属性符合文档中提到的要求,就可以在数据存储结构中使用它们。例如:

type Comment struct {
    Title    string  
    Text     string
    User     *User 
}

type Photo struct {
    Description   string
    Comments      []*Comment
}

在我看来,这样是可以的,我是对的吗?如果我是对的,我应该如何:

a) 用一次查询填充这个关系?

b) 以一种方便的方式向照片添加评论?(例如MongoDB的$push

最好的,
Michel

英文:

I am currently in the process on deciding which stack I'll use for a (small) social network . I would love to code it in golang on top of Google App Engine since this would result in clean, fast code with all the advantages of modern cloud services & hosting.

How would you implement one-to-many and/or many-to-one relationships in GAE with golang? According to the docs you can use your own types in datastore-structs, as long as their properties are the ones mentioned in the documentation. So for example:

type Comment struct {
    Title    string  
    Text     string
    User     *User 
}

type Photo struct {
    Description   string
    Comments      []*Comment
}

should work in my opinion, am I right? And if I am right, how can I:

a) populate this relationship in one query?

b) add comments to a photo in a convenient way? (such as MongoDBs '$push')

best,
Michel

答案1

得分: 2

在App Engine上,您可以使用两种实体类型来建模:

type Comment struct {
    Title    string  
    Text     string
    User     *User 
}

type Photo struct {
  Description   string
}

使用祖先路径将子评论与父照片关联起来。您可以通过单个查询获取照片的所有评论。您可以通过添加新的评论实体来添加评论。

根据文档,我认为您可以使用问题中的类型来存储照片(但我个人没有在实体中嵌套三个级别的经验)。要使用此设计添加评论,应用程序会获取照片(其中包括评论),将评论添加到照片中,并将带有所有评论的照片放回数据存储。

App Engine没有像Mongo的$push那样的实体修改操作符。

英文:

On App Engine, you can model this with two entity types:

type Comment struct {
    Title    string  
    Text     string
    User     *User 
}

type Photo struct {
  Description   string
}

Use an ancestor path to associate child comments with a parent photo. You can get all comments for a photo in a single query. You can add comments by putting new comment entities.

I think you can store a photo using the types in the question (it looks like you can according to the doc, but I have no personal experience with nesting three levels in an entity). To add a comment with this design, the application fetches the photo (this will include the comments), adds the comment to the photo, and puts the photo with all comments back to the datastore.

App Engine does not have entity modification operators like Mongo's $push.

huangapple
  • 本文由 发表于 2014年9月25日 18:16:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/26036037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定