Getting started with the Firestore in Datastore mode API

This page provides a short exercise in building a command-line TaskList application with the Firestore in Datastore mode API. The TaskList application stores, lists, updates, and removes tasks.

Prerequisites

  1. Ability to write and run a command line application in the programming languages used in this topic
    In addition to a basic understanding of how to develop applications, you should be able to download and install additional libraries before attempting this tutorial.
  2. A Google Cloud project with the Datastore mode API enabled
    Applications that use Datastore mode are associated with a Google Cloud project with the Datastore mode API enabled. This project provides authentication credentials you use in your application to identify it to Google and authorize its use of the Datastore mode API.
    Follow these instructions to create a project, enable the Datastore mode API for it, and set up your local development environment with authentication credentials using the gcloud auth login command. Make a note of the project's ID, which you'll use later on.

Installation and setup

Install client libraries and configure any additional settings for your development environment.

C#

  1. Ensure you have Visual Studio (version 2013 or later) installed.
  2. Download the TaskList sample application from the samples repository.
  3. Extract the files from the zip into a directory in your Documents folder.
  4. In Visual Studio, open the file dotnet-docs-samples-master\datastore\api\Datastore.sln.
  5. In Visual Studio's Solution Explorer window, right-click the TaskList project and choose Set as StartUp Project.
  6. Right-click the TaskList project again and choose Properties.
  7. In the Properties window, click Debug and type the ID of your Google Cloud project into the Command line arguments: box.

    Visual Studio Debug Window

  8. Click File and then click Save to save your changes.

  9. Run the application! Press F5 on your keyboard.

Go

  1. Clone the TaskList sample application.

    go get github.com/GoogleCloudPlatform/golang-samples/datastore/tasks
    
  2. Change directories to where you cloned the sample:

    cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/datastore/tasks
    
  3. At a command prompt, run the following, where <project-id> is the ID of your Google Cloud project.

    export DATASTORE_PROJECT_ID=<project-id>
    

    (Windows users: use set instead of export.)

  4. Run the application!

    go run tasks.go
    

Java

  1. Ensure you have Maven and Java (version 8 or later) installed.

  2. Download the TaskList sample application from the samples repository.

  3. At a command prompt, extract the download:

    unzip main.zip
    
  4. Change directories to the TaskList application:

    cd java-datastore-main/samples/snippets
    
  5. Run the following, where <project-id> is the ID of your Google Cloud project.

    gcloud config set project <project-id>
    
  6. Compile and run the application!

    mvn clean compile
    mvn exec:java -Dexec.mainClass="com.google.datastore.snippets.TaskList"
    

Node.js

  1. Prepare your environment for Node.js development.

  2. Download the TaskList sample application from the samples repository.

  3. Extract the download:

    unzip master.zip
    
  4. Change directories to the TaskList application:

    cd nodejs-datastore-master/samples
    
  5. Install the dependencies and link the application:

    npm install
    
  6. At a command prompt, run the following, where <project-id> is the ID of your Google Cloud project.

    export GCLOUD_PROJECT=<project-id>
    

    (Windows users: use set instead of export.)

  7. Run the application!

    node tasks.js
    

PHP

  1. Ensure you have PHP (version 5.6 or later) and Composer installed.
  2. Download the TaskList sample application from the samples repository.
  3. Extract the download:

    unzip master.zip
    
  4. Change directories to the TaskList application:

    cd php-docs-samples-master/datastore/tutorial
    
  5. Install dependencies.

    composer install
    
  6. Run the application!

    php src/list_tasks.php
    

Python

  1. Ensure you have Python (version 2.7.9 or later), pip, and virtualenv installed.
  2. Activate a virtualenv session.

    virtualenv venv
    source venv/bin/activate
    
  3. Download the TaskList sample application from the samples repository.

  4. Extract the download:

    unzip master.zip
    
  5. Change directories to the TaskList application:

    cd python-docs-samples-master/datastore/cloud-client
    
  6. Install dependencies:

    pip install -r requirements.txt
    
  7. Run the application! Use the ID of your Google Cloud project for <project-id>.

    python tasks.py new project-id
    

Ruby

  1. Ensure you have Ruby and Bundler installed.

  2. Download the TaskList sample application from the samples repository.

  3. Extract the download:

    unzip master.zip
    
  4. Change directories to the TaskList application:

    cd google-cloud-ruby-master/google-cloud-datastore/samples
    
  5. Install the dependencies:

    bundle install
    
  6. At a command prompt, run the following, where <project-id> is the ID of your Google Cloud project.

    export GOOGLE_CLOUD_PROJECT=<project-id>
    

    (Windows users: use set instead of export.)

  7. Run the application!

    bundle exec ruby tasks.rb
    

Creating an Authorized Service Object

In order to make authenticated requests to Google Cloud APIs using the Google APIs Client libraries, you must:

  • Fetch the credential to use for requests.
  • Create a service object that uses that credential.

You can then make API calls by calling methods on the Datastore mode service object.

For this example, you'll fetch Application Default Credentials from the environment, and pass it as an argument to create the service object.

Here's the call to create the authorized Datastore mode service object:

C#

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Create an authorized Datastore service using Application Default Credentials.
_db = DatastoreDb.Create(projectId);
// Create a Key factory to construct keys associated with this project.
_keyFactory = _db.CreateKeyFactory("Task");

Go

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import (
	"context"
	"log"

	"cloud.google.com/go/datastore"
)

func createClient(projectID string) (*datastore.Client, error) {
	ctx := context.Background()
	client, err := datastore.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("Could not create datastore client: %v", err)
	}
	// Note: call the following from main() to ensure the client
	// properly frees all resources.
	// defer client.Close()
	return client, nil
}

Java

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Create an authorized Datastore service using Application Default Credentials.
private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

// Create a Key factory to construct keys associated with this project.
private final KeyFactory keyFactory = datastore.newKeyFactory().setKind("Task");

Node.js

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/latest/guides/authentication
const {Datastore} = require('@google-cloud/datastore');

// Creates a client
const datastore = new Datastore();

PHP

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

use Google\Cloud\Datastore\DatastoreClient;

/**
 * Create a Cloud Datastore client.
 *
 * @param string $projectId The Google Cloud project ID.
 */
function build_service(string $projectId)
{
    $datastore = new DatastoreClient(['projectId' => $projectId]);
    return $datastore;
}

Python

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.

To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from google.cloud import datastore

def create_client(project_id):
    return datastore.Client(project_id)

Ruby

To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the