|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | | - |
15 | | -# pragma NO COVER |
16 | 14 | # Welcome to the gCloud Datastore Demo! (hit enter) |
17 | | - |
18 | 15 | # We're going to walk through some of the basics... |
19 | 16 | # Don't worry though. You don't need to do anything, just keep hitting enter... |
20 | 17 |
|
21 | | -# Let's start by importing the demo module and getting a dataset: |
| 18 | +# Let's start by importing the demo module and initializing our connection. |
22 | 19 | from gcloud.datastore import demo |
23 | | -dataset = demo.get_dataset() |
| 20 | +demo.initialize() |
24 | 21 |
|
25 | 22 | # Let's create a new entity of type "Thing" and name it 'Toy': |
26 | | -toy = dataset.entity('Thing') |
| 23 | +from gcloud.datastore.key import Key |
| 24 | +key = Key('Thing') |
| 25 | +from gcloud.datastore.entity import Entity |
| 26 | +toy = Entity(key) |
27 | 27 | toy.update({'name': 'Toy'}) |
28 | 28 |
|
29 | 29 | # Now let's save it to our datastore: |
30 | 30 | toy.save() |
31 | 31 |
|
32 | 32 | # If we look it up by its key, we should find it... |
33 | | -print(dataset.get_entities([toy.key()])) |
| 33 | +from gcloud.datastore import get_entities |
| 34 | +print(get_entities([toy.key])) |
34 | 35 |
|
35 | 36 | # And we should be able to delete it... |
36 | | -toy.delete() |
| 37 | +toy.key.delete() |
37 | 38 |
|
38 | 39 | # Since we deleted it, if we do another lookup it shouldn't be there again: |
39 | | -print(dataset.get_entities([toy.key()])) |
| 40 | +print(get_entities([toy.key])) |
40 | 41 |
|
41 | 42 | # Now let's try a more advanced query. |
| 43 | +# First, let's create some entities. |
| 44 | +SAMPLE_DATA = [ |
| 45 | + (1234, 'Computer', 10), |
| 46 | + (2345, 'Computer', 8), |
| 47 | + (3456, 'Laptop', 10), |
| 48 | + (4567, 'Printer', 11), |
| 49 | + (5678, 'Printer', 12), |
| 50 | + (6789, 'Computer', 13)] |
| 51 | +samples = [] |
| 52 | +for id, name, age in SAMPLE_DATA: |
| 53 | + key = Key('Thing', id) |
| 54 | + samples.append(key) |
| 55 | + entity = Entity(key) |
| 56 | + entity['name'] = name |
| 57 | + entity['age'] = age |
| 58 | + entity.save() |
42 | 59 | # We'll start by look at all Thing entities: |
43 | | -query = dataset.query().kind('Thing') |
| 60 | +from gcloud.datastore.query import Query |
| 61 | +query = Query(kind='Thing') |
44 | 62 |
|
45 | 63 | # Let's look at the first two. |
46 | | -print(query.limit(2).fetch()) |
| 64 | +print(list(query.fetch(limit=2))) |
47 | 65 |
|
48 | 66 | # Now let's check for Thing entities named 'Computer' |
49 | | -print(query.filter('name', '=', 'Computer').fetch()) |
| 67 | +query.add_filter('name', '=', 'Computer') |
| 68 | +print(list(query.fetch())) |
50 | 69 |
|
51 | 70 | # If you want to filter by multiple attributes, |
52 | | -# you can string .filter() calls together. |
53 | | -print(query.filter('name', '=', 'Computer').filter('age', '=', 10).fetch()) |
| 71 | +# you can call .add_filter multiple times on the query. |
| 72 | +query.add_filter('age', '=', 10) |
| 73 | +print(list(query.fetch())) |
| 74 | + |
| 75 | +# Now delete them. |
| 76 | +print([key.delete() for key in samples]) |
54 | 77 |
|
55 | 78 | # You can also work inside a transaction. |
56 | 79 | # (Check the official docs for explanations of what's happening here.) |
57 | | -with dataset.transaction(): |
| 80 | +from gcloud.datastore.transaction import Transaction |
| 81 | +with Transaction(): |
58 | 82 | print('Creating and savng an entity...') |
59 | | - thing = dataset.entity('Thing') |
60 | | - thing.key(thing.key().name('foo')) |
| 83 | + key = Key('Thing', 'foo') |
| 84 | + thing = Entity(key) |
61 | 85 | thing['age'] = 10 |
62 | 86 | thing.save() |
63 | 87 |
|
64 | 88 | print('Creating and saving another entity...') |
65 | | - thing2 = dataset.entity('Thing') |
66 | | - thing2.key(thing2.key().name('bar')) |
| 89 | + key2 = Key('Thing', 'bar') |
| 90 | + thing2 = Entity(key2) |
67 | 91 | thing2['age'] = 15 |
68 | 92 | thing2.save() |
69 | 93 |
|
70 | 94 | print('Committing the transaction...') |
71 | 95 |
|
72 | 96 | # Now that the transaction is commited, let's delete the entities. |
73 | | -print(thing.delete(), thing2.delete()) |
| 97 | +print(key.delete(), key2.delete()) |
74 | 98 |
|
75 | 99 | # To rollback a transaction, just call .rollback() |
76 | | -with dataset.transaction() as t: |
77 | | - thing = dataset.entity('Thing') |
78 | | - thing.key(thing.key().name('another')) |
| 100 | +with Transaction() as t: |
| 101 | + key = Key('Thing', 'another') |
| 102 | + thing = Entity(key) |
79 | 103 | thing.save() |
80 | 104 | t.rollback() |
81 | 105 |
|
82 | 106 | # Let's check if the entity was actually created: |
83 | | -created = dataset.get_entities([thing.key()]) |
| 107 | +created = get_entities([key]) |
84 | 108 | print('yes' if created else 'no') |
85 | 109 |
|
86 | 110 | # Remember, a key won't be complete until the transaction is commited. |
87 | | -# That is, while inside the transaction block, thing.key() will be incomplete. |
88 | | -with dataset.transaction(): |
89 | | - thing = dataset.entity('Thing') |
| 111 | +# That is, while inside the transaction block, thing.key will be incomplete. |
| 112 | +with Transaction(): |
| 113 | + key = Key('Thing') # partial |
| 114 | + thing = Entity(key) |
90 | 115 | thing.save() |
91 | | - print(thing.key()) # This will be partial |
| 116 | + print(thing.key) # This will still be partial |
92 | 117 |
|
93 | | -print(thing.key()) # This will be complete |
| 118 | +print(thing.key) # This will be complete |
94 | 119 |
|
95 | 120 | # Now let's delete the entity. |
96 | | -thing.delete() |
| 121 | +thing.key.delete() |
0 commit comments