Datastore – Storage for AppEngine



Datastore – Storage for AppEngine

1 0


cssi-appengine-datastore-02

Second level tutorial on Datastore use in AppEngine

On Github luisibanezcssi / cssi-appengine-datastore-02

Datastore

Storage for AppEngine

Created by Luis Ibanez / @luisibanez

Datastore

Native database

in AppEngine

Manually

Inspecting

Datastore

Launching AppEngine App

dev_appserver.py --port 9080 --admin_port 9000 .
                    
Checking for updates to the SDK.
Starting API server at: http://localhost:59242
Starting module "default" running at: http://localhost:9080
Starting admin server at: http://localhost:9000
                    

AppEngine Admin

http://localhost:9000

AppEngine Admin Interface

AppEngine Datastore Interface

AppEngine Datastore Interface

AppEngine Datastore Interface

Datastore

Keys

AppEngine Datastore Interface

Record Key and Id

Get with Key

key = student_instance.put()
                    
student_instance_ref = key.get()
                    
key.get().name
                    
self.response.write(key.get().name)
                    

Key to Id

key = student_instance.put()
                    
id = key.id()
                    
student = Student.get_by_id(id)
                    

Id to Key

key = ndb.Key('Student', id)
                    
key.get()
                    
key.get().name
                    

Deleting

Delete with Key

key.delete()
                    

Keys in URLs

Get id and navigate to that url

self.redirect('/view_student?key=%s' % key.urlsafe())
                    

Get an entity from an ID

key = ndb.Key(urlsafe=self.request.get("key"))
student = key.get()
                    

Querying

Full Query

student_query = Student.query()
                    
student_data = student_query.fetch()
                    
template_values = {
    'students' : student_data
}
                    
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
                    

Selective

Querying

Query Filters

student_query = Student.query()
                    
student_query = student_query.filter(Student.name == 'Larry')
                    
student_data = student_query.fetch()
                    

Query Filters

student_query = Student.query()
                    
student_query = student_query.filter(Student.university == 'MIT')
                    
student_data = student_query.fetch()
                    

Querying

& Sorting

Sorted Query

student_query = Student.query()
                    
student_query = student_query.filter(Student.university == 'MIT')
                    
student_query = student_query.order(Student.age)
                    
student_data = student_query.fetch()
                    

Reverse Sorted Query

student_query = Student.query()
                    
student_query = student_query.filter(Student.university == 'MIT')
                    
student_query = student_query.order(-Student.age)
                    
student_data = student_query.fetch()
                    

Querying

& Limits

Limited Query

student_query = Student.query()
                    
student_query = student_query.filter(Student.university == 'MIT')
                    
student_data = student_query.fetch(15)
                    

All Together

student_query = Student.query()
                    
student_query = student_query.filter(Student.university == 'MIT')
                    
student_query = student_query.order(-Student.age)
                    
student_data = student_query.fetch(15)
                    

Query

Documentation

https://cloud.google.com/appengine/docs/python/ndb/queryclass

Model

https://cloud.google.com/appengine/docs/python/ndb/modelclass

RELATIONSHIPS

The Lunchbox is Mine!

Lunchbox Model

class Lunchbox(ndb.Model):
                    
    foods = ndb.StringProperty(repeated=True)
                    
    drink = ndb.StringProperty()
                    
    insulated = ndb.BooleanProperty()
                    
    logo = ndb.BlobProperty()
                    

Kindergartener Model

class Kindergartener(ndb.Model):
                    
    name = ndb.StringProperty()
                    
    birthday = ndb.DateProperty()
                    
    fav_food = ndb.StringProperty()
                    
    lunchbox_key = ndb.KeyProperty(Lunchbox)
                    

Make it so!

Create & Relate

lunchbox_instance = Lunchbox(
    foods = ["Apple", "Sandwich", "Brocoli"],
    drink = "Carrot Juice",
    insulated = True,
    logo = "string with logo image encoded")
                    
my_lunchbox_key = lunchbox_instance.put()
                    

Create & Relate

kid_instance = Kindergartener(
    name = "Bart Simpson",
    birthday = datetime.strptime('Jun 1 2005', '%b %d %Y'),
    fav_food = "Cheeseburger",
    lunchbox_key = my_lunchbox_key)
                    
kid_key = kid_instance.put()
                    

Get My

Lunchbox

Query

kid_query = Kindergartener.query()
kid_query = kid_query.filter(Kindergartener.name == 'Bart Simpson')
                    
kid_data = kid_query.fetch()
                    
my_lunchbox_key = kid_data[0].lunchbox_key
my_lunchbox = my_lunchbox_key.get()
                    

Whose

Lunchbox

Is this?

Query

kid_query = Kindergartener.query()
kid_query = kid_query.filter(
                Kindergartener.lunchbox_key == my_lunchbox_key)
                    
kid_data = kid_query.fetch()
                    
kid_name = kid_data[0].name
                    

One

To

Many

Unus pro omnibus!

One to Many

class Team(ndb.Model):
    city = ndb.StringProperty()
    name = ndb.StringProperty()
                    
class Player(ndb.Model):
    team = ndb.KeyProperty(kind='Team')
    last_name= ndb.StringProperty()
    position = ndb.StringProperty()
                    

One to Many

da_bulls= Team(city='Chicago', name='Bulls')
bulls_key = da_bulls.put()
                    
drose = Player(team=bulls_key, last_name='Rose', position='Point Guard')
joakim = Player(team=bulls_key, last_name='Noah', position='Center')
                    
drose.put()
joakim.put()
                    

Querying

player_query = Player.query(Player.team == bulls_key)
                    
player_data = player_query.fetch()
                    
for player in player_data:
    self.response.out.write('<p>'+player.last_name+'</p>')
                    

Omnes pro uno

One to Many

class Player(ndb.Model):
    last_name= ndb.StringProperty()
    position = ndb.StringProperty()
                    
class Team(ndb.Model):
    city = ndb.StringProperty()
    name = ndb.StringProperty()
    player_keys = ndb.KeyProperty(kind='Player', repeated=True)
                    

One to Many

drose = Player(last_name='Rose', position='Point Guard')
drose_key = drose.put()
                    
joakim = Player(last_name='Noah', position='Center')
joakim_key = joakim.put()
                    
da_bulls = Team(city='Chicago', name='Bulls')
da_bulls.player_keys.append(drose_key)
da_bulls.player_keys.append(joakim_key)
da_bulls.put()
                    

Querying

bulls_query = Team.query(Team.name == 'Bulls')
bulls_data = bulls_query.fetch()
                    
da_bulls = bulls_data[0]
                    
for player_key in da_bulls.player_keys:
     self.response.out.write('<p>'+player_key.get().last_name+'</p>')
                    

Many

To

Many

Many to Many

class Fan(ndb.Model):
    first_name = ndb.StringProperty()
    last_name = ndb.StringProperty()
    home_city = ndb.StringProperty()
                    
class FanTeam(ndb.Model):
    fan = ndb.KeyProperty(Fan)
    team = ndb.KeyProperty(Team)
                    

Many to Many

nicki = Fan(first_name='Nicki', last_name='Anselmo', home_city='New Orleans').put()
norah = Fan(first_name='Norah', last_name='Solo', home_city='New York').put()
                    
FanTeam(fan=nicki, team=da_bulls).put()
FanTeam(fan=norah, team=da_bulls).put()
                    

Many to Many

bulls_query = FanTeam.query()
bulls_query = bulls_query.filter(FanTeam.team==da_bulls)
bulls_fans = bulls_query.fetch()
                    
for fan_team in bulls_fans:
     fan_key = fan_team.get().fan
     self.response.out.write('<p>'+fan_key.get().last_name+'</p>')
                    
Datastore Storage for AppEngine Created by Luis Ibanez / @luisibanez