1
- var db ;
1
+ let db ;
2
2
3
3
// [START create_counter]
4
4
function createCounter ( ref , num_shards ) {
5
- var batch = db . batch ( ) ;
5
+ const { collection, doc, writeBatch} = require ( "@firebase/firestore" ) ;
6
+
7
+ const batch = writeBatch ( db ) ;
6
8
7
9
// Initialize the counter document
8
10
batch . set ( ref , { num_shards : num_shards } ) ;
9
11
10
12
// Initialize each shard with count=0
11
13
for ( let i = 0 ; i < num_shards ; i ++ ) {
12
- let shardRef = ref . collection ( 'shards' ) . doc ( i . toString ( ) ) ;
14
+ const shardRef = doc ( collection ( ref , 'shards' ) , i . toString ( ) ) ;
13
15
batch . set ( shardRef , { count : 0 } ) ;
14
16
}
15
17
@@ -20,62 +22,72 @@ function createCounter(ref, num_shards) {
20
22
21
23
// [START increment_counter]
22
24
function incrementCounter ( db , ref , num_shards ) {
25
+ const { collection, doc, updateDoc, increment } = require ( "@firebase/firestore" ) ;
26
+
23
27
// Select a shard of the counter at random
24
- const shard_id = Math . floor ( Math . random ( ) * num_shards ) . toString ( ) ;
25
- const shard_ref = ref . collection ( 'shards' ) . doc ( shard_id ) ;
28
+ const shardId = Math . floor ( Math . random ( ) * num_shards ) . toString ( ) ;
29
+ const shardRef = doc ( collection ( ref , 'shards' ) , shardId ) ;
26
30
27
31
// Update count
28
- return shard_ref . update ( "count" , firebase . firestore . FieldValue . increment ( 1 ) ) ;
32
+ return updateDoc ( shardRef , "count" , increment ( 1 ) ) ;
29
33
}
30
34
// [END increment_counter]
31
35
32
36
// [START get_count]
33
- function getCount ( ref ) {
37
+ async function getCount ( ref ) {
38
+ const { collection, getDocs } = require ( "@firebase/firestore" ) ;
39
+
34
40
// Sum the count of each shard in the subcollection
35
- return ref . collection ( 'shards' ) . get ( ) . then ( snapshot => {
36
- let total_count = 0 ;
37
- snapshot . forEach ( doc => {
38
- total_count += doc . data ( ) . count ;
39
- } ) ;
41
+ const snapshot = await getDocs ( collection ( ref , 'shards' ) ) ;
40
42
41
- return total_count ;
43
+ let totalCount = 0 ;
44
+ snapshot . forEach ( doc => {
45
+ totalCount += doc . data ( ) . count ;
42
46
} ) ;
47
+
48
+ return totalCount ;
43
49
}
44
50
// [END get_count]
45
51
46
52
describe ( "firestore-solution-counters" , ( ) => {
47
53
before ( ( ) => {
48
- var config = {
54
+ const { initializeApp } = require ( "@firebase/app" ) ;
55
+ const { getFirestore } = require ( "@firebase/firestore" ) ;
56
+
57
+ const config = {
49
58
apiKey : "AIzaSyArvVh6VSdXicubcvIyuB-GZs8ua0m0DTI" ,
50
59
authDomain : "firestorequickstarts.firebaseapp.com" ,
51
60
projectId : "firestorequickstarts" ,
52
61
} ;
53
- var app = firebase . initializeApp ( config , "solution-counters " ) ;
54
- db = firebase . firestore ( app ) ;
62
+ const app = initializeApp ( config , "solution-arrays " ) ;
63
+ db = getFirestore ( app ) ;
55
64
} ) ;
56
65
57
66
describe ( "solution-counters" , ( ) => {
58
67
it ( "should create a counter" , ( ) => {
59
68
// Create a counter with 10 shards
60
- return createCounter ( db . collection ( 'counters' ) . doc ( ) , 10 ) ;
69
+ const { collection, doc } = require ( "@firebase/firestore" ) ;
70
+
71
+ return createCounter ( doc ( collection ( db , 'counters' ) ) , 10 ) ;
61
72
} ) ;
62
73
63
- it ( "should increment a counter" , ( ) => {
74
+ it ( "should increment a counter" , async ( ) => {
64
75
// Create a counter, then increment it
65
- let ref = db . collection ( 'counters' ) . doc ( ) ;
66
- return createCounter ( ref , 10 ) . then ( ( ) => {
67
- return incrementCounter ( db , ref , 10 ) ;
68
- } ) ;
76
+ const { collection, doc } = require ( "@firebase/firestore" ) ;
77
+
78
+ const ref = doc ( collection ( db , 'counters' ) ) ;
79
+ await createCounter ( ref , 10 )
80
+ await incrementCounter ( db , ref , 10 ) ;
69
81
} ) ;
70
82
71
- it ( "should get the count of a counter" , ( ) => {
83
+ it ( "should get the count of a counter" , async ( ) => {
72
84
// Create a counter, increment it, then get the count
73
- let ref = db . collection ( 'counters' ) . doc ( ) ;
74
- return createCounter ( ref , 10 ) . then ( ( ) => {
75
- return incrementCounter ( db , ref , 10 ) ;
76
- } ) . then ( ( ) => {
77
- return getCount ( ref ) ;
78
- } ) ;
85
+ const { collection , doc } = require ( "@firebase/firestore" ) ;
86
+
87
+ const ref = doc ( collection ( db , 'counters' ) ) ;
88
+ await createCounter ( ref , 10 ) ;
89
+ await incrementCounter ( db , ref , 10 ) ;
90
+ await getCount ( ref ) ;
79
91
} ) ;
80
92
} ) ;
81
93
} ) ;
0 commit comments