Created
June 23, 2020 11:21
-
-
Save anchitarnav/5e9df2e329c8cd85d72089166a100917 to your computer and use it in GitHub Desktop.
GCP/ GCLOUD create, read, delete, update for datastore (fiorstore in datastore mode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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