Google Cloud Datastore

It’s a database in the Cloud!

What We Will Be Doing

We’ve already made sites with templates.

We will make an app that stores (and retrieves!) data on the cloud. In AppEngine land, one of the services that stores and retrives data is called Datastore.

The Plan

We’re going to learn by doing! Doing the Hogwarts Lab in the curriculum, that is. So clone the CSSI Labs Repository if you have not yet done so. Open up your editor and navigate to appEngine-DataStore/labs/hogwarts-query.

Also, open up the curriculum to the DataStore Intro Page.

What’s in This Lab?

The datastore will be holding:

Kinds
The Kinds are Student, Wand, House, Course, Enrollment, Teacher
Entities
Each individual student, wand, house, etc. is an Entity.
Properties
Entities have properties. For example every student has a student id, first name, and last name. Houses have a name, mascot, and list of students.
Exercise: What is the difference between an entity and a kind? If you know the Harry Potter universe, give further examples of entities (you don’t have to look at code to do this). If you don’t know Harry Potter, think of entities in a different domain.

Set Up

Once the repo is cloned and you have navigated to the directory, get oriented with the project files:

    .
    ├── README.md
    ├── app.yaml
    ├── hogwarts_models.py
    ├── main.py
    ├── seed_hogwarts_db.py
    └── templates
        └── houselist.html

First, let’s all peek at app.yaml and main.py. Ask if there are any structural things in those files you haven’t seen before.

Next, browse the file hogwarts_models.py.

Exercise: At your tables, draw pictures of what these Kinds are, and how they seem to be related to each other.

Next, go back to main.py and look more carefully at the LoadDataHandler. It calls a function. Where does the function that it calls come from? Find that file and browse it too.

Concepts

In the file seed_hogwarts_db.py, what do you think that:

.put() will write an object into DataStore. Keys are DataStore’s way of identifying an object. The result of .put() is a key that you can use in your Python code to work with the object you just added. It’s okay to be confused now. If so, revisit the idea after the code along.

Code Along

STEP 1 Go to proper folder, and:

$ dev_appserver.py app.yaml --clear_datastore=yes --enable_console=True
Note that command was more complex than before, right?

STEP 2 In your browser, go to: http://localhost:8080/seed-data.

Exercise: What does that do and why?

STEP 3 Now go to your interactive console, http://localhost:8000/. Practice listing entities. Cross reference with the code in the seed data file.

Exercise: There are some properties in the datastore viewer that you did not add explicitly. What are they?

STEP 4 Delete any code in the interactive console, and enter and run:

from google.appengine.ext import ndb
from hogwarts_models import Student, House, Teacher, Enrollment, Wand, Course
print(Teacher.query().fetch())
Exercise: What did the output look like?
Note something interesting: .query() makes a query, but .fetch() gets all the results from the query

STEP 5 Let’s write some queries and learn as we go.

As we encounter these queries, write down some key words that you encounter (e.g., query, fetch, get, count, filter, order, ...)
We will so some together and you can work on some on your own or with a partner. Don’t forget to help each other. Don’t forget the PEP as well.
For CSSI, we use the NDB library, not the Firestore! Documentation is here.

Connecting DataStore to the App

The first connection is done for you! Look in main.py to see the route, and the actual database call!

Find out the app url before revealing 🙂

http://localhost:8080/houses.

Exercise: Add a students handler.

More Queries Please

Now You Can Do Anything

The lab is infinitely expandable.

Oh One More Thing

That lab we did...was run locally at localhost:8080 with the development app server. When you run locally, the datastore is actually saved on your machine. In order to really store your data on the cloud, you just have to deploy your app.

Exercise: Once you add some CSS and a few new routes, and perhaps forms to add new entities, and you’ve tested things pretty well, go ahead and make an AppEngine project in your Google Cloud account and deploy the app.

Summary

We’ve covered:

  • Kind, Entity, Property
  • Datastore models
  • put
  • query
  • fetch
  • get
  • filter
  • order
  • DataStore Viewer
  • Interactive Console
  • Connecting apps to DataStore