Skip to content

Commit

Permalink
Update deps and add array snippets
Browse files Browse the repository at this point in the history
Change-Id: I293bd04d67957b554970fecb537d064541ab141e
  • Loading branch information
samtstern committed Sep 4, 2018
1 parent c47a538 commit 5ad041a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
6 changes: 3 additions & 3 deletions firestore/extend-with-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "Cloud Functions and Firestore",
"main": "index.js",
"dependencies": {
"firebase-functions": "^1.0.0",
"firebase-admin": "^5.13.0",
"@google-cloud/firestore": "^0.16.1"
"@google-cloud/firestore": "^0.16.1",
"firebase-admin": "^6.0.0",
"firebase-functions": "^1.0.0"
}
}
56 changes: 51 additions & 5 deletions firestore/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ function updateDocument(db) {
});
}

function updateDocumentArray(db) {
// [START update_document_array]
var admin = require('firebase-admin');
// ...
var washingtonRef = db.collection('cities').doc('DC');

// Atomically add a new region to the "regions" array field.
var arrUnion = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
var arrRm = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayRemove('east_coast')
});
// [END update_document_array]

return Promise.all([arrUnion, arrRm]).then(res => {
console.log('Update array: ', res);
});
}

function updateDocumentMany(db) {
// [START update_document_many]
var cityRef = db.collection('cities').doc('DC');
Expand Down Expand Up @@ -444,19 +465,24 @@ function exampleData(db) {

var setSf = citiesRef.doc('SF').set({
name: 'San Francisco', state: 'CA', country: 'USA',
capital: false, population: 860000 });
capital: false, population: 860000,
regions: ['west_coast', 'norcal'] });
var setLa = citiesRef.doc('LA').set({
name: 'Los Angeles', state: 'CA', country: 'USA',
capital: false, population: 3900000 });
capital: false, population: 3900000,
regions: ['west_coast', 'socal'] });
var setDc = citiesRef.doc('DC').set({
name: 'Washington, D.C.', state: null, country: 'USA',
capital: true, population: 680000 });
capital: true, population: 680000,
regions: ['east_coast'] });
var setTok = citiesRef.doc('TOK').set({
name: 'Tokyo', state: null, country: 'Japan',
capital: true, population: 9000000 });
capital: true, population: 9000000,
regions: ['kanto', 'honshu'] });
var setBj = citiesRef.doc('BJ').set({
name: 'Beijing', state: null, country: 'China',
capital: true, population: 21500000 });
capital: true, population: 21500000,
regions: ['jingjinji', 'hebei'] });
// [END example_data]

return Promise.all([setSf, setLa, setDc, setTok, setBj]);
Expand Down Expand Up @@ -605,6 +631,18 @@ function queryAndFilter(db) {
});
}

function arrayFilter(db) {
var citiesRef = db.collection('cities');
// [START array_contains_filter]
var westCoastCities = citiesRef.where('regions', 'array-contains', 'west_coast');
// [END array_contains_filter]

return westCoastCities.get()
.then(res => {
console.log('West Coast get: ', res);
});
}

function orderAndLimit(db) {
var citiesRef = db.collection('cities');
// [START order_limit]
Expand Down Expand Up @@ -916,6 +954,10 @@ describe('Firestore Smoketests', () => {
return updateDocument(db);
});

it('should update array fields in a document', () => {
return updateDocumentArray(db);
});

it('should update many document', () => {
return updateDocumentMany(db);
});
Expand Down Expand Up @@ -962,6 +1004,10 @@ describe('Firestore Smoketests', () => {
it('should query and filter', () => {
return queryAndFilter(db);
});

it('should query and filter an array', () => {
return arrayFilter(db);
});

it('should order and limit', () => {
return orderAndLimit(db);
Expand Down
2 changes: 1 addition & 1 deletion firestore/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "ISC",
"dependencies": {
"@google-cloud/firestore": "^0.16.1",
"firebase-admin": "^5.13.1"
"firebase-admin": "^6.0.0"
},
"devDependencies": {
"mocha": "^3.3.0"
Expand Down
2 changes: 1 addition & 1 deletion firestore/solution-aggregation/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "^5.13.0",
"firebase-admin": "^6.0.0",
"firebase-functions": "^1.0.1"
},
"private": true
Expand Down

0 comments on commit 5ad041a

Please sign in to comment.