Skip to content

Instantly share code, notes, and snippets.

@tseaver
Created January 29, 2019 17:21
Show Gist options
  • Save tseaver/41144415460ad26c012359d657a32680 to your computer and use it in GitHub Desktop.
Save tseaver/41144415460ad26c012359d657a32680 to your computer and use it in GitHub Desktop.
Reproduce gcp-7215
import os
import pprint
from google.oauth2 import service_account
from google.cloud import firestore_v1beta1
def prepare_document(client):
document = client.collection('test').document()
document.set({
"foo":{
"bar": 'baz',
},
'other': True,
})
snapshot = document.get()
print(f"\nPrepared document: {snapshot.id}")
pprint.pprint(snapshot.to_dict())
return snapshot.reference
def update_document(document):
field_updates = {
'foo.test': 'test',
'foo.now': firestore_v1beta1.SERVER_TIMESTAMP,
}
document.update(field_updates)
snapshot = document.get()
print(f"\nUpdated document: {snapshot.id}")
pprint.pprint(snapshot.to_dict())
def main():
creds_file = os.environ.get('FIRESTORE_APPLICATION_CREDENTIALS')
creds = service_account.Credentials.from_service_account_file( creds_file)
client = firestore_v1beta1.Client(
credentials=creds, project=creds.project_id)
document = prepare_document(client)
try:
update_document(document)
finally:
document.delete()
print(f"\nDeleted document: {document.id}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment