ORM for google cloud datastore

半城伤御伤魂 提交于 2019-12-05 01:29:40

问题


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

It looks like datastore in GAE but without ORM (object relation model). May I used the same ORM model as datastore on GAE for Cloud Datastore? or Is there any ORM support can be found for Cloud Datastore?


回答1:


Google Cloud Datastore only provides a low-level API (proto and json) to send datastore RPCs.

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




回答2:


App Engine Datastore high level APIs, both first party (db, ndb) and third party (objectify, slim3), are built on top of low level APIs:

  • datastore_rpc for Python
  • DatastoreService/AsyncDatastoreService for Java

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.

The high level API code itself should not have to change (much).




回答3:


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.

It is available on nuget, called "pogo", and the source is hosted here - 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);
}



回答4:


This is not an exact answer and We already know Google is working for NDB Library, I couldn't wait for that.

What I tried is to write NDB Properties not listed in datastore_v1_pb2.py, such as GeoPt.

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

in this case, if we read the entity by GCD lowlevel API, returns like following.

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. and now We can write GeoPt property something like this.

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.




回答5:


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.

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



来源:https://stackoverflow.com/questions/16642012/orm-for-google-cloud-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!