Skip to content

Commit 9beafb7

Browse files
committed
Solution counters
1 parent 1bb37fa commit 9beafb7

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

firestore/test.solution-counters.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var db;
1+
let db;
22

33
// [START create_counter]
44
function createCounter(ref, num_shards) {
5-
var batch = db.batch();
5+
const { collection, doc, writeBatch} = require("@firebase/firestore");
6+
7+
const batch = writeBatch(db);
68

79
// Initialize the counter document
810
batch.set(ref, { num_shards: num_shards });
911

1012
// Initialize each shard with count=0
1113
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());
1315
batch.set(shardRef, { count: 0 });
1416
}
1517

@@ -20,62 +22,72 @@ function createCounter(ref, num_shards) {
2022

2123
// [START increment_counter]
2224
function incrementCounter(db, ref, num_shards) {
25+
const { collection, doc, updateDoc, increment } = require("@firebase/firestore");
26+
2327
// 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);
2630

2731
// Update count
28-
return shard_ref.update("count", firebase.firestore.FieldValue.increment(1));
32+
return updateDoc(shardRef, "count", increment(1));
2933
}
3034
// [END increment_counter]
3135

3236
// [START get_count]
33-
function getCount(ref) {
37+
async function getCount(ref) {
38+
const { collection, getDocs } = require("@firebase/firestore");
39+
3440
// 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'));
4042

41-
return total_count;
43+
let totalCount = 0;
44+
snapshot.forEach(doc => {
45+
totalCount += doc.data().count;
4246
});
47+
48+
return totalCount;
4349
}
4450
// [END get_count]
4551

4652
describe("firestore-solution-counters", () => {
4753
before(() => {
48-
var config = {
54+
const { initializeApp } = require("@firebase/app");
55+
const { getFirestore } = require("@firebase/firestore");
56+
57+
const config = {
4958
apiKey: "AIzaSyArvVh6VSdXicubcvIyuB-GZs8ua0m0DTI",
5059
authDomain: "firestorequickstarts.firebaseapp.com",
5160
projectId: "firestorequickstarts",
5261
};
53-
var app = firebase.initializeApp(config, "solution-counters");
54-
db = firebase.firestore(app);
62+
const app = initializeApp(config, "solution-arrays");
63+
db = getFirestore(app);
5564
});
5665

5766
describe("solution-counters", () => {
5867
it("should create a counter", () => {
5968
// 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);
6172
});
6273

63-
it("should increment a counter", () => {
74+
it("should increment a counter", async () => {
6475
// 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);
6981
});
7082

71-
it("should get the count of a counter", () => {
83+
it("should get the count of a counter", async () => {
7284
// 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);
7991
});
8092
});
8193
});

0 commit comments

Comments
 (0)