This recipe shows you how to read and write an entity in Cloud Datastore from a Cloud Function.
View the source code.
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/functions/datastore
-
Create a Cloud Storage Bucket to stage our deployment:
gsutil mb gs://YOUR_BUCKET_NAME
- Replace
YOUR_BUCKET_NAME
here and in subsequent commands with the name of your Cloud Storage Bucket.
- Replace
-
Ensure the Cloud Datastore API is enabled:
Click here to enable the Cloud Datastore API
-
Deploy the "get" function with an HTTP trigger:
gcloud alpha functions deploy get --stage-bucket YOUR_BUCKET_NAME --trigger-http
-
Deploy the "set" function with an HTTP trigger:
gcloud alpha functions deploy set --stage-bucket YOUR_BUCKET_NAME --trigger-http
-
Deploy the "del" function with an HTTP trigger:
gcloud alpha functions deploy del --stage-bucket YOUR_BUCKET_NAME --trigger-http
-
Call the "set" function to create a new entity:
gcloud alpha functions call set --data '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}'
or
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/set"
- Replace
[YOUR_REGION]
with the region where your function is deployed. - Replace
[YOUR_PROJECT_ID]
with your Google Cloud Platform project ID.
- Replace
-
Call the "get" function to read the newly created entity:
gcloud alpha functions call get --data '{"kind":"Task","key":"sampletask1"}'
or
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
- Replace
[YOUR_REGION]
with the region where your function is deployed. - Replace
[YOUR_PROJECT_ID]
with your Google Cloud Platform project ID.
- Replace
-
Call the "del" function to delete the entity:
gcloud alpha functions call del --data '{"kind":"Task","key":"sampletask1"}'
or
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/del"
- Replace
[YOUR_REGION]
with the region where your function is deployed. - Replace
[YOUR_PROJECT_ID]
with your Google Cloud Platform project ID.
- Replace
-
Call the "get" function again to verify it was deleted:
gcloud alpha functions call get --data '{"kind":"Task","key":"sampletask1"}'
or
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
- Replace
[YOUR_REGION]
with the region where your function is deployed. - Replace
[YOUR_PROJECT_ID]
with your Google Cloud Platform project ID.
- Replace