Skip to content

Commit 5ad041a

Browse files
committed
Update deps and add array snippets
Change-Id: I293bd04d67957b554970fecb537d064541ab141e
1 parent c47a538 commit 5ad041a

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

firestore/extend-with-functions/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"description": "Cloud Functions and Firestore",
44
"main": "index.js",
55
"dependencies": {
6-
"firebase-functions": "^1.0.0",
7-
"firebase-admin": "^5.13.0",
8-
"@google-cloud/firestore": "^0.16.1"
6+
"@google-cloud/firestore": "^0.16.1",
7+
"firebase-admin": "^6.0.0",
8+
"firebase-functions": "^1.0.0"
99
}
1010
}

firestore/main/index.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@ function updateDocument(db) {
248248
});
249249
}
250250

251+
function updateDocumentArray(db) {
252+
// [START update_document_array]
253+
var admin = require('firebase-admin');
254+
// ...
255+
var washingtonRef = db.collection('cities').doc('DC');
256+
257+
// Atomically add a new region to the "regions" array field.
258+
var arrUnion = washingtonRef.update({
259+
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
260+
});
261+
// Atomically remove a region from the "regions" array field.
262+
var arrRm = washingtonRef.update({
263+
regions: admin.firestore.FieldValue.arrayRemove('east_coast')
264+
});
265+
// [END update_document_array]
266+
267+
return Promise.all([arrUnion, arrRm]).then(res => {
268+
console.log('Update array: ', res);
269+
});
270+
}
271+
251272
function updateDocumentMany(db) {
252273
// [START update_document_many]
253274
var cityRef = db.collection('cities').doc('DC');
@@ -444,19 +465,24 @@ function exampleData(db) {
444465

445466
var setSf = citiesRef.doc('SF').set({
446467
name: 'San Francisco', state: 'CA', country: 'USA',
447-
capital: false, population: 860000 });
468+
capital: false, population: 860000,
469+
regions: ['west_coast', 'norcal'] });
448470
var setLa = citiesRef.doc('LA').set({
449471
name: 'Los Angeles', state: 'CA', country: 'USA',
450-
capital: false, population: 3900000 });
472+
capital: false, population: 3900000,
473+
regions: ['west_coast', 'socal'] });
451474
var setDc = citiesRef.doc('DC').set({
452475
name: 'Washington, D.C.', state: null, country: 'USA',
453-
capital: true, population: 680000 });
476+
capital: true, population: 680000,
477+
regions: ['east_coast'] });
454478
var setTok = citiesRef.doc('TOK').set({
455479
name: 'Tokyo', state: null, country: 'Japan',
456-
capital: true, population: 9000000 });
480+
capital: true, population: 9000000,
481+
regions: ['kanto', 'honshu'] });
457482
var setBj = citiesRef.doc('BJ').set({
458483
name: 'Beijing', state: null, country: 'China',
459-
capital: true, population: 21500000 });
484+
capital: true, population: 21500000,
485+
regions: ['jingjinji', 'hebei'] });
460486
// [END example_data]
461487

462488
return Promise.all([setSf, setLa, setDc, setTok, setBj]);
@@ -605,6 +631,18 @@ function queryAndFilter(db) {
605631
});
606632
}
607633

634+
function arrayFilter(db) {
635+
var citiesRef = db.collection('cities');
636+
// [START array_contains_filter]
637+
var westCoastCities = citiesRef.where('regions', 'array-contains', 'west_coast');
638+
// [END array_contains_filter]
639+
640+
return westCoastCities.get()
641+
.then(res => {
642+
console.log('West Coast get: ', res);
643+
});
644+
}
645+
608646
function orderAndLimit(db) {
609647
var citiesRef = db.collection('cities');
610648
// [START order_limit]
@@ -916,6 +954,10 @@ describe('Firestore Smoketests', () => {
916954
return updateDocument(db);
917955
});
918956

957+
it('should update array fields in a document', () => {
958+
return updateDocumentArray(db);
959+
});
960+
919961
it('should update many document', () => {
920962
return updateDocumentMany(db);
921963
});
@@ -962,6 +1004,10 @@ describe('Firestore Smoketests', () => {
9621004
it('should query and filter', () => {
9631005
return queryAndFilter(db);
9641006
});
1007+
1008+
it('should query and filter an array', () => {
1009+
return arrayFilter(db);
1010+
});
9651011

9661012
it('should order and limit', () => {
9671013
return orderAndLimit(db);

firestore/main/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"license": "ISC",
1212
"dependencies": {
1313
"@google-cloud/firestore": "^0.16.1",
14-
"firebase-admin": "^5.13.1"
14+
"firebase-admin": "^6.0.0"
1515
},
1616
"devDependencies": {
1717
"mocha": "^3.3.0"

firestore/solution-aggregation/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"logs": "firebase functions:log"
1010
},
1111
"dependencies": {
12-
"firebase-admin": "^5.13.0",
12+
"firebase-admin": "^6.0.0",
1313
"firebase-functions": "^1.0.1"
1414
},
1515
"private": true

0 commit comments

Comments
 (0)