|
16 | 16 | # See the License for the specific language governing permissions and |
17 | 17 | # limitations under the License. |
18 | 18 |
|
19 | | -# Let's start by importing the demo module and initializing our connection. |
| 19 | +# Let's start by importing the demo module and initializing our client. |
20 | 20 | from gcloud import datastore |
21 | 21 | from gcloud.datastore import demo |
22 | 22 |
|
23 | | -demo.initialize() |
24 | | - |
25 | | -# Let's import the package containing our helper classes: |
| 23 | +client = datastore.Client(dataset_id=demo.DATASET_ID) |
26 | 24 |
|
27 | 25 | # Let's create a new entity of type "Thing" and name it 'Toy': |
28 | | -key = datastore.Key('Thing') |
| 26 | +key = client.key('Thing') |
29 | 27 | toy = datastore.Entity(key) |
30 | 28 | toy.update({'name': 'Toy'}) |
31 | 29 |
|
32 | 30 | # Now let's save it to our datastore: |
33 | | -datastore.put(toy) |
| 31 | +client.put(toy) |
34 | 32 |
|
35 | 33 | # If we look it up by its key, we should find it... |
36 | | -print(datastore.get(toy.key)) |
| 34 | +print(client.get(toy.key)) |
37 | 35 |
|
38 | 36 | # And we should be able to delete it... |
39 | | -datastore.delete(toy.key) |
| 37 | +client.delete(toy.key) |
40 | 38 |
|
41 | 39 | # Since we deleted it, if we do another lookup it shouldn't be there again: |
42 | | -print(datastore.get(toy.key)) |
| 40 | +print(client.get(toy.key)) |
43 | 41 |
|
44 | 42 | # Now let's try a more advanced query. |
45 | 43 | # First, let's create some entities. |
|
52 | 50 | (6789, 'Computer', 13)] |
53 | 51 | sample_keys = [] |
54 | 52 | for id, name, age in SAMPLE_DATA: |
55 | | - key = datastore.Key('Thing', id) |
| 53 | + key = client.key('Thing', id) |
56 | 54 | sample_keys.append(key) |
57 | 55 | entity = datastore.Entity(key) |
58 | 56 | entity['name'] = name |
59 | 57 | entity['age'] = age |
60 | | - datastore.put(entity) |
| 58 | + client.put(entity) |
61 | 59 | # We'll start by look at all Thing entities: |
62 | | -query = datastore.Query(kind='Thing') |
| 60 | +query = client.query(kind='Thing') |
63 | 61 |
|
64 | 62 | # Let's look at the first two. |
65 | 63 | print(list(query.fetch(limit=2))) |
|
74 | 72 | print(list(query.fetch())) |
75 | 73 |
|
76 | 74 | # Now delete them. |
77 | | -datastore.delete_multi(sample_keys) |
| 75 | +client.delete_multi(sample_keys) |
78 | 76 |
|
79 | 77 | # You can also work inside a transaction. |
80 | 78 | # (Check the official docs for explanations of what's happening here.) |
81 | | -with datastore.Transaction() as xact: |
| 79 | +with client.transaction() as xact: |
82 | 80 | print('Creating and saving an entity...') |
83 | | - key = datastore.Key('Thing', 'foo') |
| 81 | + key = client.key('Thing', 'foo') |
84 | 82 | thing = datastore.Entity(key) |
85 | 83 | thing['age'] = 10 |
86 | 84 | xact.put(thing) |
87 | 85 |
|
88 | 86 | print('Creating and saving another entity...') |
89 | | - key2 = datastore.Key('Thing', 'bar') |
| 87 | + key2 = client.key('Thing', 'bar') |
90 | 88 | thing2 = datastore.Entity(key2) |
91 | 89 | thing2['age'] = 15 |
92 | 90 | xact.put(thing2) |
93 | 91 |
|
94 | 92 | print('Committing the transaction...') |
95 | 93 |
|
96 | 94 | # Now that the transaction is commited, let's delete the entities. |
97 | | -datastore.delete_multi([key, key2]) |
| 95 | +client.delete_multi([key, key2]) |
98 | 96 |
|
99 | 97 | # To rollback a transaction, just call .rollback() |
100 | | -with datastore.Transaction() as xact: |
101 | | - key = datastore.Key('Thing', 'another') |
| 98 | +with client.transaction() as xact: |
| 99 | + key = client.key('Thing', 'another') |
102 | 100 | thing = datastore.Entity(key) |
103 | 101 | xact.put(thing) |
104 | 102 | xact.rollback() |
105 | 103 |
|
106 | 104 | # Let's check if the entity was actually created: |
107 | | -created = datastore.get(key) |
| 105 | +created = client.get(key) |
108 | 106 | print('yes' if created else 'no') |
109 | 107 |
|
110 | 108 | # Remember, a key won't be complete until the transaction is commited. |
111 | 109 | # That is, while inside the transaction block, thing.key will be incomplete. |
112 | | -with datastore.Transaction() as xact: |
113 | | - key = datastore.Key('Thing') # partial |
| 110 | +with client.transaction() as xact: |
| 111 | + key = client.key('Thing') # partial |
114 | 112 | thing = datastore.Entity(key) |
115 | 113 | xact.put(thing) |
116 | 114 | print(thing.key) # This will still be partial |
117 | 115 |
|
118 | 116 | print(thing.key) # This will be complete |
119 | 117 |
|
120 | 118 | # Now let's delete the entity. |
121 | | -datastore.delete(thing.key) |
| 119 | +client.delete(thing.key) |
0 commit comments