|
12 | 12 | # limitations under the License.
|
13 | 13 |
|
14 | 14 | import datetime
|
| 15 | +from time import sleep |
15 | 16 |
|
16 | 17 | from google.cloud import firestore
|
17 | 18 | import google.cloud.exceptions
|
@@ -603,6 +604,114 @@ def cursor_paginate():
|
603 | 604 | return next_query
|
604 | 605 |
|
605 | 606 |
|
| 607 | +def listen_document(): |
| 608 | + db = firestore.Client() |
| 609 | + # [START listen_document] |
| 610 | + |
| 611 | + # Create a callback on_snapshot function to capture changes |
| 612 | + def on_snapshot(doc_snapshot, changes, read_time): |
| 613 | + for doc in doc_snapshot: |
| 614 | + print(u'Received document snapshot: {}'.format(doc.id)) |
| 615 | + |
| 616 | + doc_ref = db.collection(u'cities').document(u'SF') |
| 617 | + |
| 618 | + # Watch the document |
| 619 | + doc_watch = doc_ref.on_snapshot(on_snapshot) |
| 620 | + # [END listen_document] |
| 621 | + |
| 622 | + # Creating document |
| 623 | + data = { |
| 624 | + u'name': u'San Francisco', |
| 625 | + u'state': u'CA', |
| 626 | + u'country': u'USA', |
| 627 | + u'capital': False, |
| 628 | + u'population': 860000 |
| 629 | + } |
| 630 | + doc_ref.set(data) |
| 631 | + sleep(3) |
| 632 | + # [START detach_listener] |
| 633 | + # Terminate watch on a document |
| 634 | + doc_watch.unsubscribe() |
| 635 | + # [START detach_listener] |
| 636 | + |
| 637 | + |
| 638 | +def listen_multiple(): |
| 639 | + db = firestore.Client() |
| 640 | + # [START listen_multiple] |
| 641 | + |
| 642 | + # Create a callback on_snapshot function to capture changes |
| 643 | + def on_snapshot(col_snapshot, changes, read_time): |
| 644 | + print(u'Callback received query snapshot.') |
| 645 | + print(u'Current cities in California:') |
| 646 | + for doc in col_snapshot: |
| 647 | + print(u'{}'.format(doc.id)) |
| 648 | + |
| 649 | + col_query = db.collection(u'cities').where(u'state', u'==', u'CA') |
| 650 | + |
| 651 | + # Watch the collection query |
| 652 | + query_watch = col_query.on_snapshot(on_snapshot) |
| 653 | + |
| 654 | + # [END listen_multiple] |
| 655 | + # Creating document |
| 656 | + data = { |
| 657 | + u'name': u'San Francisco', |
| 658 | + u'state': u'CA', |
| 659 | + u'country': u'USA', |
| 660 | + u'capital': False, |
| 661 | + u'population': 860000 |
| 662 | + } |
| 663 | + db.collection(u'cities').document(u'SF').set(data) |
| 664 | + sleep(1) |
| 665 | + query_watch.unsubscribe() |
| 666 | + |
| 667 | + |
| 668 | +def listen_for_changes(): |
| 669 | + db = firestore.Client() |
| 670 | + # [START listen_for_changes] |
| 671 | + |
| 672 | + # Create a callback on_snapshot function to capture changes |
| 673 | + def on_snapshot(col_snapshot, changes, read_time): |
| 674 | + print(u'Callback received query snapshot.') |
| 675 | + print(u'Current cities in California: ') |
| 676 | + for change in changes: |
| 677 | + if change.type.name == "ADDED": |
| 678 | + print(u'New city: {}'.format(change.document.id)) |
| 679 | + elif change.type.name == "MODIFIED": |
| 680 | + print(u'Modified city: {}'.format(change.document.id)) |
| 681 | + elif change.type.name == "REMOVED": |
| 682 | + print(u'Removed city: {}'.format(change.document.id)) |
| 683 | + |
| 684 | + col_query = db.collection(u'cities').where(u'state', u'==', u'CA') |
| 685 | + |
| 686 | + # Watch the collection query |
| 687 | + query_watch = col_query.on_snapshot(on_snapshot) |
| 688 | + |
| 689 | + # [END listen_for_changes] |
| 690 | + mtv_document = db.collection(u'cities').document(u'MTV') |
| 691 | + # Creating document |
| 692 | + mtv_document.set({ |
| 693 | + u'name': u'Mountain View', |
| 694 | + u'state': u'CA', |
| 695 | + u'country': u'USA', |
| 696 | + u'capital': False, |
| 697 | + u'population': 80000 |
| 698 | + }) |
| 699 | + |
| 700 | + # Modifying document |
| 701 | + mtv_document.update({ |
| 702 | + u'name': u'Mountain View', |
| 703 | + u'state': u'CA', |
| 704 | + u'country': u'USA', |
| 705 | + u'capital': False, |
| 706 | + u'population': 90000 |
| 707 | + }) |
| 708 | + |
| 709 | + # Delete document |
| 710 | + mtv_document.delete() |
| 711 | + sleep(1) |
| 712 | + query_watch.unsubscribe() |
| 713 | + |
| 714 | + |
606 | 715 | def cursor_multiple_conditions():
|
607 | 716 | db = firestore.Client()
|
608 | 717 | # [START cursor_multiple_conditions]
|
|
0 commit comments