|
12 | 12 | # limitations under the License. |
13 | 13 |
|
14 | 14 | import argparse |
| 15 | +from datetime import datetime, timedelta, timezone |
15 | 16 | from pprint import pprint |
16 | 17 |
|
17 | 18 | from google.cloud import datastore # noqa: I100 |
@@ -56,11 +57,31 @@ def not_in_query(client): |
56 | 57 | return list(query.fetch()) |
57 | 58 |
|
58 | 59 |
|
| 60 | +def query_with_readtime(client): |
| 61 | + # [START datastore_snapshot_read] |
| 62 | + # Create a read time of 120 seconds in the past |
| 63 | + read_time = datetime.now(timezone.utc) - timedelta(seconds=120) |
| 64 | + |
| 65 | + # Fetch an entity at time read_time |
| 66 | + task_key = client.key('Task', 'sampletask') |
| 67 | + entity = client.get(task_key, read_time=read_time) |
| 68 | + |
| 69 | + # Query Task entities at time read_time |
| 70 | + query = client.query(kind="Task") |
| 71 | + tasks = query.fetch(read_time=read_time, limit=10) |
| 72 | + # [END datastore_snapshot_read] |
| 73 | + |
| 74 | + results = list(tasks) |
| 75 | + results.append(entity) |
| 76 | + |
| 77 | + return results |
| 78 | + |
| 79 | + |
59 | 80 | def main(project_id): |
60 | 81 | client = datastore.Client(project_id) |
61 | 82 |
|
62 | 83 | for name, function in globals().items(): |
63 | | - if name in ("main", "_preamble", "defaultdict") or not callable(function): |
| 84 | + if name in ("main", "_preamble", "defaultdict", "datetime", "timezone", "timedelta") or not callable(function): |
64 | 85 | continue |
65 | 86 |
|
66 | 87 | print(name) |
|
0 commit comments