1
votes

I've migrated a 1st Gen GAE Python 2 app to 2nd Gen GAE Python 3. It was not easy but everything seems to be working.

I'd now like to migrate from google-cloud-ndb to google-cloud-datastore. With google-cloud-datastore, is it ok to subclass Entity (which is a dict subclass) in a similar way to subclassing ndb.Model?

For example google-cloud-ndb code looks like this:

from google.cloud import ndb

class Foo(ndb.Model):
    x = ndb.StringProperty(default='42')

    def do_something(self):
        pass

Is this the right way to migrate the above to google-cloud-datastore?:

from google.cloud.datastore.entity import Entity

class Foo(Entity):

    def __init__(self):
        super().__init__()
        self['x'] = '42'

    def do_something(self):
        pass

I haven't seen any example code like this so I'd like to confirm that this is a good practice, especially with overriding __init__.

Even better, is there a way to subclass Entity so that I could access the data as object attributes (foo.x) instead of as a dictionary (foo['x'])? That would make migration much easier.

1
Hi there, have you had any chance to go through the following codelab? Step 5 bullet 3 shares some good info about how the migration from Cloud ndb to datastore should be. codelabs.developers.google.com/codelabs/… - Antonio Ramirez
@AntonioRamirez, yes, I read that before asking my question. - gaefan

1 Answers

0
votes

I suspect that the solution in my question is not a good one and that something like this would be better:

from google.cloud.datastore.entity import Entity

class Foo():

    x: str

    def __init__(self):
        super().__init__()
        self.x = '42'

    def do_something(self):
        pass

    def to_dict(self):
        return {'x': self.x}

    @staticmethod
    def from_dict(self, data):
        f = Foo()
        f.x = data['x']
        return f

Would be great to get feedback from someone who has gone through this already.