简体   繁体   English

用于谷歌云数据存储的 ORM

[英]ORM for google cloud datastore

https://developers.google.com/datastore/docs/overview https://developers.google.com/datastore/docs/overview

It looks like datastore in GAE but without ORM (object relation model).它看起来像 GAE 中的数据存储,但没有 ORM(对象关系模型)。 May I used the same ORM model as datastore on GAE for Cloud Datastore?我可以在 GAE for Cloud Datastore 上使用与数据存储相同的 ORM 模型吗? or Is there any ORM support can be found for Cloud Datastore?或者是否可以为 Cloud Datastore 找到任何 ORM 支持?

App Engine Datastore high level APIs, both first party (db, ndb) and third party (objectify, slim3), are built on top of low level APIs: App Engine Datastore 高级 API,包括第一方(db、ndb)和第三方(objectify、slim3),都是建立在低级 API 之上的:

Replacing the App Engine specific versions of these interfaces/classes to work on top of The Google Cloud Datastore API will allow you to use these high level APIs outside of App Engine.替换这些接口/类的 App Engine 特定版本以在Google Cloud Datastore API 之上工作将允许您在 App Engine 之外使用这些高级 API。

The high level API code itself should not have to change (much).高级 API 代码本身不应该(太多)更改。

Google Cloud Datastore only provides a low-level API (proto and json ) to send datastore RPCs. Google Cloud Datastore仅提供低级 API(protojson )来发送数据存储区 RPC。

NDB and similar higher level libraries could be adapted to use a lower level wrapper like googledatastore ( reference ) instead of google.appengine.datastore.datastore_rpc NDB和类似的更高级别的库可以适应使用较低级别的包装器,如googledatastore参考)而不是google.appengine.datastore.datastore_rpc

For the .NET folks out there, I just created pogo, which is a .NET ORM that supports saving POCOs to Google Cloud Datastore, and querying them using LINQ.对于那里的 .NET 人员,我刚刚创建了 pogo,它是一个 .NET ORM,支持将 POCO 保存到 Google Cloud Datastore,并使用 LINQ 查询它们。

It is available on nuget, called "pogo", and the source is hosted here - http://code.thecodeprose.com/pogo它在 nuget 上可用,称为“pogo”,源代码托管在此处 - http://code.thecodeprose.com/pogo

For example, here's an insert and a lookup:例如,这是一个插入和查找:

var unique = Guid.NewGuid().ToString();

var poco = new TestDepartment
{
    Id = unique,
    Code = 123,
    Director = new TestEmployee { FirstName = "Boss" }
};

using (var session = _datastore.OpenSession())
{
    session.Store(poco);
    session.SaveChanges();
    var lookupPoco = session.Load<TestDepartment>(unique).SingleOrDefault();

    Assert.AreEqual("Boss", lookupPoco.Director.FirstName);
}

and here's a query:这是一个查询:

using (var session = _datastore.OpenSession())
{
    var results = session.Query<TestEmployee>()
          .Where(t => t.HourlyRate > 50.0)
          .ToList();

    Assert.IsTrue(results.Count > 0);
}

This is not an exact answer and We already know Google is working for NDB Library , I couldn't wait for that.这不是一个确切的答案,我们已经知道 Google 正在为NDB Library工作,我等不及了。

What I tried is to write NDB Properties not listed in datastore_v1_pb2.py , such as GeoPt .我尝试的是编写datastore_v1_pb2.py未列出的 NDB 属性,例如GeoPt

class GCDFoo(ndb.Model):
    latlng = ndb.GeoPtProperty()

in this case, if we read the entity by GCD lowlevel API, returns like following.在这种情况下,如果我们通过 GCD 低级 API 读取实体,则返回如下。

name: "latlng"
value {
  entity_value {
    property {
      name: "x"
      value {
        double_value: 10.0
        indexed: false
      }
    }
    property {
      name: "y"
      value {
        double_value: 10.0
        indexed: false
      }
    }
  }
  meaning: 9
}

hm, I don't really know what does 'meaning' mean, but It was important to describe GeoPt.嗯,我真的不知道“意义”是什么意思,但描述 GeoPt 很重要。 and now We can write GeoPt property something like this.现在我们可以像这样编写 GeoPt 属性。

def make_geopt_value(lat,lng):
  entity = datastore.Entity()
  prop = entity.property.add()
  datastore.helper.set_property(prop,'x',lng,indexed=False)
  prop = entity.property.add()
  datastore.helper.set_property(prop,'y',lat,indexed=False)
  value = datastore.Value()
  datastore.helper.set_value(value,entity)
  value.meaning = 9
  return value

It worked for me, still don't know if it's right approach though.它对我有用,但仍然不知道这是否是正确的方法。 Anyway I hope my answer is helpful for someone who can't wait NDB Library.无论如何,我希望我的回答对等不及 NDB 库的人有所帮助。

You can take a look on this one, this one is written in Typescript你可以看看这个,这个是用Typescript写的

Description: ts-datastore-orm targets to provide a strong typed and structural ORM feature for Datastore (Firestore in Datastore mode).说明:ts-datastore-orm 旨在为 Datastore(Datastore 模式下的 Firestore)提供强类型和结构化 ORM 功能。

https://www.npmjs.com/package/ts-datastore-orm https://www.npmjs.com/package/ts-datastore-orm


import {BaseEntity, Batcher, Column, datastoreOrm, Entity, Transaction, DatastoreOrmDatastoreError, DatastoreOrmError} from "ts-datastore-orm";

@Entity({namespace: "testing", kind: "user", compositeIndexes: [{id: "desc"}, {string: "asc", ["object.name"]: "asc", __ancestor__: false}]})
export class User extends BaseEntity {
    @Column({generateId: true})
    public id: number = 0;

    @Column()
    public date: Date = new Date();

    @Column({index: true})
    public string: string = "";

    @Column()
    public number: number = 10;

    @Column()
    public buffer: Buffer = Buffer.alloc(1);

    @Column()
    public array: number[] = [];

    @Column({excludeFromIndexes: ["object.name"]})
    public object: any = {};

    @Column()
    public undefined: undefined = undefined;

    @Column()
    public null: null = null;
}


async function queryExamples() {
    const [user1, requestResponse1] = await User.query().runOnce();
    const [userList1, requestResponse2] = await User.findMany([1, 2, 3, 4, 5]);
    const [userList2, requestResponse3] = await User.findMany({ancestor: user1, ids: [1, 2, 3, 4, 5]});
    const [userList3, requestResponse4] = await User.query().run();

    const user2 = User.create({id: 1});
    const query = TaskGroup.query()
        .filter("number", "=", 10)
        .filterAny("anyColumn.name", ">", 5)
        .setAncestor(user2)
        .groupByAny("anyColumn.name")
        .orderAny("anyColumn.name", {descending: true})
        .offset(5)
        .limit(10);

    while (query.hasNextPage()) {
        const [entities] = await query.run();
    }

    // stream
    const stream = query.runStream()
        .on("data", (entity) => {
            //
        })
        .on("info", (info) => {
            //
        })
        .on("error", (error) => {
            //
        })
        .on("end", () => {
            //
        });
}

Yes.是的。 Not only can you use the same ORM models, Google Cloud Datastore allows you to read and write from your current App Engine apps storage.您不仅可以使用相同的 ORM 模型,Google Cloud Datastore 还允许您从当前的 App Engine 应用程序存储中读取和写入。

https://developers.google.com/datastore/docs/activate?hl=en#google_cloud_datastore_for_an_existing_app_engine_application https://developers.google.com/datastore/docs/activate?hl=en#google_cloud_datastore_for_an_existing_app_engine_application

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM