ndb_vs_db

5
N o t e b o o k : G o o g l e A p p E n g i n e C r e a t e d : 1 7 - 0 9 - 2 0 1 3 0 8 : 4 8 U R L : h t t p s : / / d o c s . g o o g l e . c o m / d o c u m e n t / d / 1 A e f y l b a d N 4 5 6 _ Z 7 B Z O p Z E X D q 8 c R 8 L Y u 7 Q g I 7 b t 5 V 0 I w / M o d e l c l a s s M o d e l c l a s s g o o g l e . a p p e n g i n e . e x t . d b n d b . m o d e l c l a s s M y M o d e l ( d b . M o d e l ) : f o o = d b . S t r i n g P r o p e r t y ( ) c l a s s M y M o d e l ( n d b . M o d e l ) : f o o = n d b . S t r i n g P r o p e r t y ( ) @ c l a s s m e t h o d d e f k i n d ( c l s ) : r e t u r n ' F o o ' @ c l a s s m e t h o d d e f _ g e t _ k i n d ( c l s ) : r e t u r n ' F o o ' M y M o d e l . k i n d ( ) M y M o d e l . _ g e t _ k i n d ( ) M y M o d e l . p r o p e r t i e s ( ) m o d e l _ i n s t a n c e . p r o p e r t i e s ( ) M y M o d e l . _ p r o p e r t i e s # N o ( ) ! ! m o d e l _ e n t i t y . _ p r o p e r t i e s M y E x p a n d o . d y n a m i c _ p r o p e r t i e s ( ) M y E x p a n d o . _ p r o p e r t i e s # N o ( ) ! ! E n t i t i e s g o o g l e . a p p e n g i n e . e x t . d b n d b . m o d e l M y M o d e l ( k e y _ n a m e = ' m y _ k e y ' ) M y M o d e l ( i d = ' m y _ k e y ' ) M y M o d e l ( k e y _ n a m e = ' m y _ k e y ' , p a r e n t = m o d e l _ i n s t a n c e ) M y M o d e l ( i d = ' m y _ k e y ' , p a r e n t = m o d e l _ i n s t a n c e . k e y ) k e y = m o d e l _ i n s t a n c e . k e y ( ) k e y = m o d e l _ i n s t a n c e . k e y # n o ( ) ! ! m o d e l _ i n s t a n c e = M y M o d e l ( f o o = ' f o o ' , b a r = ' b a r ' , b a z = ' b a z ' ) m o d e l _ i n s t a n c e = M y M o d e l ( f o o = ' f o o ' , b a r = ' b a r ' , b a z = ' b a z ' ) m o d e l _ i n s t a n c e . f o o = ' f o o ' m o d e l _ i n s t a n c e . b a r = ' b a r ' m o d e l _ i n s t a n c e . b a z = ' b a z ' m o d e l _ i n s t a n c e . f o o = ' f o o ' m o d e l _ i n s t a n c e . b a r = ' b a r ' m o d e l _ i n s t a n c e . b a z = ' b a z ' # o r a s h o r t c u t . . . m o d e l _ i n s t a n c e . p o p u l a t e ( f o o = ' f o o ' , b a r = ' b a r ' , b a z = ' b a z ' ) m o d e l _ i n s t a n c e . i s _ s a v e d ( ) # N o d i r e c t e q u i v a l e n t ; s e e h t t p : / / s t a c k o v e r f l o w . c o m / q u e s t i o n s / 1 2 0 8 3 2 5 4 / i s - i t - p o s s i b l e - t o - d e t e r m i n e - w i t h - n d b - i f - m o d e l - i s - p e r s i s t e n t - i n - t h e - d a t a s t o r e - o r / 1 2 0 9 6 0 6 6 # 1 2 0 9 6 0 6 6 f o r a p o s s i b l e s o l u t i o n G e t g o o g l e . a p p e n g i n e . e x t . d b n d b . m o d e l You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)

description

Google DataStorendb vs db

Transcript of ndb_vs_db

Page 1: ndb_vs_db

Notebook: Google App EngineCreated : 17-09-2013 08:48URL: https://docs.google.com/document/d/1AefylbadN456_Z7BZOpZEXDq8cR8LYu7QgI7bt5V0Iw/…

Model class

Model classgoogle.appengine.ext.db ndb.model

class MyModel(db.Model): foo = db.StringProperty()

class MyModel(ndb.Model): foo = ndb.StringProperty()

@classmethoddef kind(cls): return 'Foo'

@classmethoddef _get_kind(cls): return 'Foo'

MyModel.kind() MyModel._get_kind()

MyModel.properties()model_instance.properties()

MyModel._properties # No () !!model_entity._properties

MyExpando.dynamic_properties() MyExpando._properties # No () !!

Entitiesgoogle.appengine.ext.db ndb.model

MyModel(key_name='my_key') MyModel(id='my_key')

MyModel(key_name='my_key', parent=model_instance)

MyModel(id='my_key', parent=model_instance.key)

key = model_instance.key() key = model_instance.key # no () !!

model_instance = MyModel( foo='foo', bar='bar', baz='baz')

model_instance = MyModel( foo='foo', bar='bar', baz='baz')

model_instance.foo = 'foo'model_instance.bar = 'bar'model_instance.baz = 'baz'

model_instance.foo = 'foo'model_instance.bar = 'bar'model_instance.baz = 'baz'# or a shortcut...model_instance.populate( foo='foo', bar='bar', baz='baz')

model_instance.is_saved() # No direct equivalent; seehttp://stackoverflow.com/questions/12083254/is-it-possible-to-determine-with-ndb-if-model-is-persistent-in-the-datastore-or/12096066#12096066 for a possible solution

Getgoogle.appengine.ext.db ndb.model

You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)

Page 2: ndb_vs_db

MyModel.get_by_key_name('my_key') MyModel.get_by_id('my_key')

MyModel.get_by_id(42) MyModel.get_by_id(42)

db.get(key) key.get()

MyModel.get(key) key.get()

db.get(model_instance) model_instance.key.get()

db.get(list_of_keys) ndb.get_multi(list_of_keys)

db.get(list_of_instances) ndb.get_multi([x.key for x in list_of_instances])

MyModel.get_or_insert('my_key', parent=model_instance, foo='bar')

MyModel.get_or_insert('my_key', parent=model_instance.key, foo='bar')

Putgoogle.appengine.ext.db ndb.model

db.put(model_instance) model_instance.put()

db.put(list_of_model_instances) ndb.put_multi( list_of_model_instances)

Deletegoogle.appengine.ext.db ndb.model

model_instance.delete() model_instance.key.delete()

db.delete(model_instance) model_instance.key.delete()

db.delete(key) key.delete()

db.delete(list_of_model_instances) ndb.delete_multi([m.key for m in list_of_model_instances])

db.delete(list_of_keys) ndb.delete_multi(list_of_keys)

Propertiesgoogle.appengine.ext.db ndb.model

db.BlobProperty() ndb.BlobProperty()

db.BooleanProperty() ndb.BooleanProperty()

db.ByteStringProperty() ndb.BlobProperty(indexed=True)

db.CategoryProperty() ndb.StringProperty()

db.DateProperty() ndb.DateProperty()

db.DateTimeProperty() ndb.DateTimeProperty()

db.EmailProperty() ndb.StringProperty()

You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)

Page 3: ndb_vs_db

db.FloatProperty() ndb.FloatProperty()

db.GeoPtProperty() ndb.GeoPtProperty()

db.IMProperty() # No equivalent

db.IntegerProperty() ndb.IntegerProperty()

db.LinkProperty() ndb.StringProperty() (but beware the max size of 500 -- if you have longer urls, use ndb.TextProperty())

db.ListProperty(bool)db.ListProperty(float)db.ListProperty(int)db.ListProperty(db.Key)# etc.

ndb.BooleanProperty(repeated=True)ndb.FloatProperty(repeated=True)ndb.IntegerProperty(repeated=True)ndb.KeyProperty(repeated=True)# etc.

db.PhoneNumberProperty() ndb.StringProperty()

db.PostalAddressProperty() ndb.StringProperty()

db.RatingProperty() ndb.IntegerProperty()

db.ReferenceProperty(AnotherModel)model_instance.propMyModel.prop \ .get_value_for_datastore \ (model_instance)

ndb.KeyProperty(kind=AnotherModel)model_instance.prop.get()model_instance.prop

# Using the backreference setother = model_instance.propother.prop_set.fetch(N)

# No direct equivalent; emulation:other = model_instance.prop.get()MyModel.query( MyModel.prop == other.key).fetch(N)

db.SelfReferenceProperty() ndb.KeyProperty(kind='ThisModelClass')

db.StringProperty() ndb.StringProperty()

db.StringProperty(multiline=True) # Not supported; strings are always# allowed to contain '\n'

db.StringListProperty() ndb.StringProperty(repeated=True)

db.TextProperty() ndb.TextProperty()

db.TimeProperty() ndb.TimeProperty()

db.UserProperty() ndb.UserProperty()

blobstore.BlobReferenceProperty() ndb.BlobKeyProperty()

Building a Keygoogle.appengine.ext.db ndb.model

key = db.Key(encoded_key) key = ndb.Key(urlsafe=encoded_key)

key = db.Key.from_path( 'MyKind', 'some_id', 'MyKind', 'some_id')

key = ndb.Key( 'MyKind', 'some_id', 'MyKind', 'some_id')

key = db.Key.from_path( key = ndb.Key(

You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)

Page 4: ndb_vs_db

MyModel, 'some_id', parent=model_instance, namespace='my_namespace')

MyModel, 'some_id', parent=model_instance.key, namespace='my_namespace')

Key operationsgoogle.appengine.ext.db ndb.model

key.id_or_name() key.id()

key.id() key.integer_id()

key.name() key.string_id()

key.has_id_or_name() key.id() is None# or...model_instance.has_complete_key()

key.app(), key.namespace(),key.parent(), key.kind()

# same thing

str(key) key.urlsafe()

key.to_path() key.flat()

db.allocate_ids(MyModel, size) S, E = MyModel.allocate_ids(size)

db.allocate_id_range(MyModel,X,Y) S, E = MyModel.allocate_ids(max=Y)assert S <= X

Transactionsgoogle.appengine.ext.db ndb.model

db.run_in_transaction(function) ndb.transaction(function)

db.run_in_transaction( function, *args, **kwds)

ndb.transaction( lambda: function(*args, **kwds))

db.run_in_transaction_custom_retries(n, function)

ndb.transaction(function, retries=n)

opts = \ db.create_transaction_options(xg=True)db.run_in_transaction_options(opts, fun)

ndb.transaction(fun, xg=True)

Queriesgoogle.appengine.ext.db ndb.model

q = MyModel.all() q = MyModel.query()

for result in q.run(): ... for result in q.iter(): ...

q = MyModel.all() \ .filter('foo =', 'bar') \ .filter('baz >=', 'ding')

q = MyModel.query( MyModel.foo == 'bar', MyModel.baz >= 'ding')

q = MyModel.all()q.filter('foo =', 'bar')

q = MyModel.query()q = q.filter(MyModel.foo == 'bar')

You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)

Page 5: ndb_vs_db

q.filter('baz >=', 'ding')q.order('-foo')results = q.fetch(10)

q = q.filter(MyModel.baz >= 'ding')q = q.order(-MyModel.foo)results = q.fetch(10)

q.filter('__key__', k)# k is a db.Key instance

q = q.filter(MyModel._key == k)# k is an ndb.Key instance

a.filter('__key__ >=', k)# k is a db.Key instance

q = q.filter(MyModel._key >= k)# k is an ndb.Key instance

class MyExpando(Expando): passq = MyExpando.all()q.filter('foo =', 'bar')

class MyExpando(Expando): passq = MyExpando.query( ndb.GenericProperty('foo') == 'bar')

class Foo(Model): ...class Bar(Model): foo = ReferenceProperty(Foo)myfoo = <some Foo instance>for bar in myfoo.bar_set(): ...

class Foo(Model): ...class Bar(Model): foo = KeyProperty(kind=Foo)myfoo = <some Foo instance>for bar in \ Bar.query(Bar.foo == myfoo.key): ...

q = MyModel.all()q.ancestor(ancestor_key)

q = MyModel.query(ancestor=ancestor_key)

q = MyModel.all(keys_only=True)r = q.fetch(N)

r = MyModel.query() \ .fetch(N, keys_only=True)# Alternatively:q = MyModel.query( default_options=QueryOptions( keys_only=True))r = q.fetch(N)

q = MyModel.gql(...) # same thing

Cursorsq = MyModel.all()a = q.fetch(20)cur = q.cursor()

q = MyModel.query()a, cur, more = q.fetch_page(20)# (1)

q.with_cursor(cur)b = q.fetch(20)

b, cur, more = \ q.fetch_page(20, start_cursor=cur)

q.with_cursor(end_cursor=cur)b = q.fetch(20)

q.fetch(20, end_cursor=cur)

You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)