In google cloud console, I can use "Datastore > Entities" to update one entity at a time, or make SELECT
queries using GQL.
I need to update several entities and it would be impractical to do it one by one. Is there a way to do an update query or something similar to the "Interactive console" that exists in the "admin server" when I running my project in the local GAE server?
EDIT: Just for clarification, I want to do this without needing to deploy new code in my production server.
As noted by sergio franco, it's possible to use Cloud Functions to access the datastore and update the entities. It's still needed to deploy the function, but since it can all be done from the console, it's considerably simpler and faster than adding new code to the project and deploying the whole project.
It's important to remember that you'll be priced for each time the function in executed, no matter the size fo the function. More details here
# Function dependencies, for example:
# package>=version
google-cloud-datastore==1.8.0
from io import StringIO
from google.cloud import datastore
datastore_client = datastore.Client()
def update_books(request):
buffer = StringIO()
query = datastore_client.query(kind='Book')
query.add_filter('author', '=', 'Terry Pratchett')
data = query.fetch()
for book in data:
book['rating'] = '5 stars'
client.put(book)
buffer.write('Updated book {}'.format(book['title']))
return buffer.getvalue()
Anything returned by the function, will be printed in the "Output" section of the page.
Anything called with print
will be sent to the logs
You can access all the entities properties as if it was a dict.
Sources:
cloud functions python to access Datastore
https://googleapis.dev/python/datastore/latest/entities.html