<br />

<br />

<br />

<br />

<br />

<br />

Firestore is a NoSQL document database built for automatic scaling,
high performance, and ease of application development. As the successor to
Datastore, Firestore is the recommended database for all new
applications. Firestore offers multiple interfaces, including a
Datastore-compatible API to support legacy code built on
Datastore.

This document describes how to use the
[Cloud Client Libraries](https://docs.cloud.google.com/datastore/docs/reference/libraries.md.txt) to store and
retrieve data in a Datastore mode database.

You can use the sample application in this guide for any
[supported](https://docs.cloud.google.com/appengine/docs/flexible/lifecycle/support-schedule.md.txt#python) version of
Python by specifying the runtime version
and operating system in your [`app.yaml`](https://docs.cloud.google.com/appengine/docs/flexible/python/runtime.md.txt#newversions)
file.

## Prerequisites and setup

- [Set up your environment and project](https://docs.cloud.google.com/appengine/docs/flexible/setting-up-environment.md.txt) to understand how 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:

<br />

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

<br />

## Edit project configuration and set dependencies

<br />

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

    Flask==3.1.3; python_version >= '3.9'
    google-cloud-datastore==2.20.2
    gunicorn==23.0.0

<br />

## Application code

<br />

The sample application logs, retrieves, and displays visitor IPs. You can see
that a log entry is a two-field class that is given the type `visit`,
and is saved to Datastore mode using the
[put](https://docs.cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.md.txt#google_cloud_datastore_client_Client_put)
command. Then, the ten most recent visits are retrieved in descending order
using the `query()` command.

```python
@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.now(tz=datetime.timezone.utc),
        }
    )

    ds.put(entity)
    query = ds.query(kind="visit", order=("-timestamp",))

    results = []
    for x in query.fetch(limit=10):
        try:
            results.append("Time: {timestamp} Addr: {user_ip}".format(**x))
        except KeyError:
            print("Error with result format, skipping entry.")

    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 queries. More elaborate Datastore mode queries require one or more [indexes](https://docs.cloud.google.com/datastore/docs/concepts/indexes.md.txt), which you must specify in an [`index.yaml` file](https://docs.cloud.google.com/appengine/docs/flexible/configuring-datastore-indexes-with-index-yaml.md.txt) 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](https://docs.cloud.google.com/datastore/docs/tools/datastore-emulator).

## For more information

For complete information on Datastore mode, including
optimizations and concepts, see the
[Firestore in Datastore mode documentation](https://docs.cloud.google.com/datastore/docs/overview).