Skip to content

Instantly share code, notes, and snippets.

@anchitarnav
Created June 23, 2020 11:21
Show Gist options
  • Select an option

  • Save anchitarnav/5e9df2e329c8cd85d72089166a100917 to your computer and use it in GitHub Desktop.

Select an option

Save anchitarnav/5e9df2e329c8cd85d72089166a100917 to your computer and use it in GitHub Desktop.
GCP/ GCLOUD create, read, delete, update for datastore (fiorstore in datastore mode)
from google.cloud import ndb
client = ndb.Client()
# client = ndb.Client(project='your-project-name', namespace='your-namespace')
class Person(ndb.Model):
name = ndb.StringProperty()
age = ndb.IntegerProperty()
new = ndb.IntegerProperty()
with client.context():
# CREATE
person = Person(id='id_for_chris', name='Chris J', age=12)
key = person.put()
print(key)
# (READ) Find by ID
person = Person.get_by_id(id='id_for_chris') # return None if not found
print(person)
# (READ) Find by Query
qry = Person.query(Person.name == 'Chris J')
for x in qry.iter():
print(x)
# UPDATE
person.age = 13
person.put()
# DELETE
person = Person.get_by_id(id='id_for_chris')
person.key.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment