Using Cloud Firestore in Datastore Mode

Cloud Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. It is the newest version of Cloud Datastore and introduces several improvements over Cloud Datastore.

We recommend using Cloud Firestore in Datastore mode for databases that will be used primarily by App Engine apps. For more information about Cloud Firestore modes, see Choosing between Native Mode and Datastore mode.

This document describes how to use the Google Cloud Client Library to store and retrieve data in a Datastore mode database.

Prerequisites and setup

Follow the instructions in "Hello, World!" for Python on App Engine to set up your environment and project, and to understand how Python apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.

Clone the repository

Download (clone) the sample:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/flexible/datastore

Edit project configuration and set dependencies

Include the google-cloud-datastore library in requirements.txt. This is the client library for Datastore mode.

Flask==1.1.1
google-cloud-datastore==1.9.0
gunicorn==19.9.0

Application code

The sample application logs, retrieves, and displays visitor IPs. You can see that a log entry is a simple two-field class that is given the type visit, and is saved to Datastore mode using the put command. Then, the ten most recent visits are retrieved in descending order using the query() command.

@app.route('/')
def index():
    ds = datastore.Client()

    user_ip = request.remote_addr

    # Keep only the first two octets of the IP address.
    if is_ipv6(user_ip):
        user_ip = ':'.join(user_ip.split(':')[:2])
    else:
        user_ip = '.'.join(user_ip.split('.')[:2])

    entity = datastore.Entity(key=ds.key('visit'))
    entity.update({
        'user_ip': user_ip,
        'timestamp': datetime.datetime.utcnow()
    })

    ds.put(entity)

    query = ds.query(kind='visit', order=('-timestamp',))

    results = [
        'Time: {timestamp} Addr: {user_ip}'.format(**x)
        for x in query.fetch(limit=10)]

    output = 'Last 10 visits:\n{}'.format('\n'.join(results))

    return output, 200, {'Content-Type': 'text/plain; charset=utf-8'}

Using index.yaml files

The sample app performs simple queries. More elaborate Datastore mode queries require one or more indexes, which you must specify in an index.yaml file that you upload along with your app. This file may be created manually, or generated automatically while testing your app locally.

Local testing

If you need to develop and test your application locally, you can use the Datastore mode emulator.

For more information

For complete information on Datastore mode, including optimizations and concepts, see the Cloud Firestore in Datastore mode documentation.

Var denne side nyttig? Giv os en anmeldelse af den:

Send feedback om...

App Engine flexible environment for Python docs