Giter Club home page Giter Club logo

snippets-node's Introduction

Firebase Node.js Snippets

This repository holds code snippets used in Node.js documentation on firebase.google.com.

Contributing

We love contributions! See CONTRIBUTING.md for guidelines.

Build Status

Actions Status

snippets-node's People

Contributors

aahventures avatar abeisgoat avatar benwhitehead avatar billyjacobson avatar craiglabenz avatar danieljbruce avatar dependabot[bot] avatar dharmarajx24 avatar dpebot avatar egilmorez avatar jenperson avatar jhuleatt avatar jimpei avatar jlara310 avatar kevinthecheung avatar kossnocorp avatar kroikie avatar mayeedwin avatar michelepatrassi avatar morganchen12 avatar naseemkullah avatar nicolasgarnier avatar peili avatar petermiles avatar petterw03 avatar ricardohbin avatar rsgowman avatar samtstern avatar shweta345 avatar xxdd13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snippets-node's Issues

Nested Objects with ArrayUnion

The documentation and examples do not mention if updating nested objects works with arrayUnion

This is the document's example:

var frankDocRef = db.collection("users").doc("frank");
frankDocRef.set({
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", subjects: ["recess", "math"] },
    age: 12
});

// To update age and favorite color:
db.collection("users").doc("frank").update({
    "age": 13,
    "favorites.color": "Red"
})

does this work or not?

// does this work?
db.collection("users").doc("frank").update({
    "favorites.subjects": FieldValue.arrayUnion("history")
})

What if token has expired when delete collection?

This question is about the deleting collections and subcollections solution (Doc).

As the document describes, our function should get token from environment variables and pass it to the delete function, but the token would expire and the request would fail.
Any advice on how to keep the token valid?

Thank you.

Scheduled backup needs to be updated to Node10

Node.js 8 support for the App Engine Standard environment is being deprecated, requiring you to upgrade your App Engine Standard applications to Node.js 10 by October 1, 2019. After said date, you will be unable to deploy Node.js 8 versions to your applications.

Error: Functions did not deploy properly

I am trying to deploy delete sample code given. I get the "Error: Functions did not deploy properly" error even though I followed all the steps in the ReadMe doc

Not possible to run a recursive delete function using 'firebase-tools' like in the suggested solution

Hi,

It is not possible to deploy a cloud function running a similar code as here: https://github.com/firebase/snippets-node/tree/master/firestore/solution-deletes

Whenever the 'firebase-tools' package is required from the cloud functions context the following error is thrown during the deployment:
Error: ENOENT: no such file or directory, mkdir '/www-data-home/.config/configstore'

However, if testing locally with the firebase function emulator the same code works without problems.

Could you please help me deploy my code?

Kind regards,

Set it in env

const serviceAccount = require('./path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();

How can I set it in .env or system environment?
e.g.

 admin.initializeApp({ 
   credential: admin.credential.cert(process.env.SERVICE_ACCOUNT_KEY) 
 });

Importing Documents From Data Exports

Is there a way to import specific Firestore Database Documents under an exported Collection from Firebase Storage?

Observed

After creating a JavaScript AppEngine Cron Job according to the Schedule Export documentation there does not seem to be a way to import specific Documents, only Collections.

Expected

In-Short: The ability to export a specified Document representing a user.

The Firestore database of the app is currently structured with a users Collection at the highest level, and then a Document representing the user containing meta data, saved content, etc. A daily Cron Job has been scheduled with the documentation above in order to back up all of the Firestore data including the user data in the event users' data is inadvertently corrupted during development or maliciously in an attack.

However, from the Import specific collections documentation it appears only a Collection can be imported.

Existing Data Structure

screen shot 2019-01-25 at 5 25 25 pm

  • users collection
    • user_one document
      - actions collection
      ...
      - categories collection
      ...
      - user data fields
    • user_two document
      ...

Potential Solution if Importing Document Impossible

Refactoring the Firestore Database structure to add additional layers of Collections/Documents in order for each User to be represented by a unique Collection. The downside with the new data structure is you have a unnecessary additional users document 2nd level.

New Data Structure

  • users collection
    • users document
      • user_one collection
        • actions document
          ...
        • categories document
          ...
        • user data document w/ fields
          ...
      • user_two collection
        ...

I believe that this cloud function is not idempotent

exports.aggregateRatings = functions.firestore
.document('restaurants/{restId}/ratings/{ratingId}')
.onWrite((change, context) => {
// Get value of the newly added rating
var ratingVal = change.after.data().rating;
// Get a reference to the restaurant
var restRef = db.collection('restaurants').doc(context.params.restId);
// Update aggregations in a transaction
return db.runTransaction(transaction => {
return transaction.get(restRef).then(restDoc => {
// Compute new number of ratings
var newNumRatings = restDoc.data().numRatings + 1;
// Compute new average rating
var oldRatingTotal = restDoc.data().avgRating * restDoc.data().numRatings;
var newAvgRating = (oldRatingTotal + ratingVal) / newNumRatings;
// Update restaurant info
return transaction.update(restRef, {
avgRating: newAvgRating,
numRatings: newNumRatings
});
});
});
});

If it's retried, I think it's possible to get a wrong value for newNumRatings.

need verify if query result exist

var citiesRef = db.collection('cities');
var query = citiesRef.where('capital', '==', true).get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
})
.catch(err => {
console.log('Error getting documents', err);
});

var citiesRef = db.collection('cities');
var query = citiesRef.where('capital', '==', true).get()
    .then(snapshot => {
        if(snapshot.size>0)
                  snapshot.forEach(doc => {
                          console.log(doc.id, '=>', doc.data());
                  });
       else
                  console.log("no documents");
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

Importing Specific Collections

Is it possible to import specific collections from Cloud Storage if you do not define specific collections in the export cron.yaml file, but rather export all collections?

In the sample documentation specific collections are named in order to export in the cron.yaml file.

cron:
- description: "Daily Cloud Firestore Export"
 url: /cloud-firestore-export?outputUriPrefix=gs://BUCKET_NAME[/PATH]
target: cloud-firestore-admin
schedule: every 24 hours

Is it feasible to export all collections, yet still import specific collections later on if needed like so.

Example use case

Backing up all of an app's data, and then restoring the collections for a specific user.

gcloud alpha firestore import gs://BUCKET_NAME/USER/USER_NAME

Q: same backup cronjob, multiple projects

Hi,

First off apologies if this is not the right place to ask this. But I am implementing the recommended firestore backup appengine cronjob in multiple projects and would like to avoid duplicating code, e.g. current setup:

appengine
├── README.md
├── gcpproject1
│   └── firestore-backup
│       ├── app.js
│       ├── app.yaml
│       ├── cron.yaml
│       ├── deploy.sh
│       └── package.json
└── gcpproject2
    └── firestore-backup
        ├── app.js
        ├── app.yaml
        ├── cron.yaml
        ├── deploy.sh
        └── package.json

Where only the deploy command in deploy.sh (which targets correct project) and url (query parameters outputUriPrefix and collections in particular) are the only differences in both folders.
What would be the recommended best practice to avoid duplicating the code? Symlinks to the common files?

array-contains not working

 var citiesRef = db.collection('cities');
  var westCoastCities = citiesRef.where('regions', 'array-contains',
    'west_coast');
  return westCoastCities.get()
    .then(res => {
      console.log('West Coast get: ', res);
    });

rising this exception
Argument "opStr" is not a valid FieldComparison. Operator must be one of "<", "<=", "==", ">", or ">="

Add a warning about including `mintAdminToken`

Thanks for these amazing snippets but I feel like there should be a warning about including this in any production environment since the combination of mintAdminToken and recursiveDelete would enable to anyone blasting an entire production database.

I suggest you create an example that isn't copy-pasteable.

exports.mintAdminToken = functions.https.onCall(async (data, context) => {

Cloud Firestore Scheduled Backups

Thank you very much for the snippit of code. It was exactly what I needed. What is the best way to control who can run the export endpoint?

https://<MY_PROJECT_ID>.appspot.com/cloud-firestore-export?outputUriPrefix=gs://<MY_STORAGE_LOCATION>/backups

In a perfect world I would like to restrict access to that endpoint to only the cron job. Is that possible?

[solution-scheduled-backups] Shutdown cleanly when asked to?

My colleague has mentioned that:

It's usually necessary to add process.on('SIGINT', () => process.exit());
to have the server shutdown cleanly when asked to. You can test by running the process in a > docker container with -it and using CTRL+C to ask it to stop.

Are there any reasons to not add this to the app engine service? If not I could make a PR.

Syntax highlighting not working correctly

Summary

As a side effect of #46, the current documentation syntax highlighting is not working as expected some parts get syntax highlighting but all that include let are not, I've attached the screenshot below for reference.

Preview

Screenshot from 2019-06-09 14-57-19

WriteBatch.update requires at least 3 arguments

Hi there! I'm getting this error
Error [FirebaseError]: Function WriteBatch.update() requires at least 3 arguments, but was called with 2 arguments.
According to the documentation and examples therein, writeBatch.update() needs only 2 arguments: the document reference and the data object.
My Working Environment:
Ubuntu 18.04
Node v12.4.0
npm 6.9.0
firebase 6.2.3

Error code 204 - Cron job is deployed successfully but fails to run.

Expected

Defined Firebase Cloud Storage directory gs://my-project-name.appspot.com/ to be populated with all of the collections in the my-project-name's Firestore database when the deployed Cron job is ran on GCP with the implementation followed by the Schedule data exports documentation.

Observed

Cron job is deployed successfully, but fails to run. The logs are showing Error code 204.

screen shot 2019-01-24 at 11 24 13 am

Logs

2019-01-24 11:19:47.818 PST GET 500 0 B 998 ms AppEngine-Google; (+http://code.google.com/appengine) /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/ 0.1.0.1 - - [24/Jan/2019:11:19:47 -0800] "GET /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/ HTTP/1.1" 500 - - "AppEngine-Google; (+http://code.google.com/appengine)" "cloud-firestore-admin.[my-project-name].appspot.com" ms=998 cpu_ms=482 cpm_usd=0 loading_request=1 instance=00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b app_engine_release=1.9.71 trace_id=65811179272fc9c721d10e6cab7cdf70

{
    httpRequest: {
    status:  500   
}
insertId:  "5c4a0fd4000c78b4e510bd7a"  
labels: {
    clone_id:  "00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b"   
}
logName:  "projects/[my-project-name]/logs/appengine.googleapis.com%2Frequest_log"  
operation: {
    first:  true   
    id:  "5c4a0fd300ff0c7e86eb62e71c0001737e636f696e76657273652d6d656469612d73746167696e670001323031393031323474313130333331000100"   
    last:  true   
    producer:  "appengine.googleapis.com/request_id"   
}
protoPayload: {
    @type:  "type.googleapis.com/google.appengine.logging.v1.RequestLog"   
    appEngineRelease:  "1.9.71"   
    appId:  "s~[my-project-name]"   
    endTime:  "2019-01-24T19:19:48.816961Z"   
    finished:  true   
    first:  true   
    host:  "cloud-firestore-admin.[my-project-name].appspot.com"   
    httpVersion:  "HTTP/1.1"   
    instanceId:  "00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b"   
    instanceIndex:  -1   
    ip:  "0.1.0.1"   
    latency:  "0.998139s"   
    line: [
        0: {
            logMessage:  "This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application."     
            severity:  "INFO"     
            time:  "2019-01-24T19:19:48.816504Z"     
        }
    1: {
        logMessage:  "A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 204)"      
        severity:  "ERROR"     
        time:  "2019-01-24T19:19:48.816507Z"     
    }
]
    megaCycles:  "482"   
    method:  "GET"   
    pendingTime:  "0.010711670s"   
    requestId:  "5c4a0fd300ff0c7e86eb62e71c0001737e636f696e76657273652d6d656469612d73746167696e670001323031393031323474313130333331000100"   
    resource:  "/cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/"   
    startTime:  "2019-01-24T19:19:47.818822Z"   
    status:  500   
    taskName:  "6c799fe7c1a404ec22117967738fcca9"   
    taskQueueName:  "__cron"   
    traceId:  "65811179272fc9c721d10e6cab7cdf70"   
    traceSampled:  true   
    urlMapEntry:  "auto"   
    userAgent:  "AppEngine-Google; (+http://code.google.com/appengine)"   
    versionId:  "20190124t110331"   
    wasLoadingRequest:  true   
 }
 receiveTimestamp:  "2019-01-24T19:19:48.824360342Z"  
     resource: {
         labels: {
             module_id:  "default"    
             project_id:  "my-project-name"    
             version_id:  "20190124t110331"    
             zone:  "us17"    
        }
        type:  "gae_app"   
    }
        severity:  "ERROR"  
        timestamp:  "2019-01-24T19:19:47.818822Z"  
        trace:  "projects/[my-project-name]/traces/65811179272fc9c721d10e6cab7cdf70"  
        traceSampled:  true  
    }

2019-01-24 11:19:48.816 PST
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.
2019-01-24 11:19:48.816 PST
A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 204)

Configuration

  1. Billing is enabled for both the GCP and Firebase project.
  2. A Firebase Storage bucket is currently created and in use to store mp3 and txt files under a directory named content with a new empty directory named backups created to store the results of the export Cron job.

screen shot 2019-01-24 at 10 20 20 am

  1. The following permissions have been enabled as recommended in the section Configure access permissions.

screen shot 2019-01-24 at 10 17 02 am

  1. The GCP project is defined as default.
    screen shot 2019-01-24 at 12 05 35 pm

  2. The app.yaml, and app.js code has been copied as is.

  3. For the package.json the name, version, and description attributes have been changed specific to the project.

  4. In the cron.yaml the description and url attributes are customized. For url the following has been attempted in order to export all collections:

    • /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups

Attempted Solutions

  1. Version code: Incrementing the version code on subsequent deploys.
  2. cron.yaml url:
    • Adding / after directory: /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/
    • Adding collections query but leaving it empty: /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups&collections
  3. IAM Permissions:
    • Adding Cloud Datastore Import Export Admin permission to my user account [email protected] since that is the email account associated to the project under gcloud config configurations list.
    • Adding the Storage Admin to @appspot.gserviceaccount.com in addition to the Owner permission.

Function execution took ****** ms, finished with status: 'connection error'

This code gives me this error:

Function execution took 11879 ms, finished with status: 'connection error'

How can I fix this?

Code:

exports.businessUpdated = functions.firestore.document('businesses/{uid}').onCreate(event => {
// Get the note document
const note = event.data.data();

// Add an 'objectID' field which Algolia requires
note.objectID = event.params.noteId;

// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(note);

 });

`--token` is deprecated

When using the snippet I get a warning

Authenticating with `--token` is deprecated and will be removed in a future major version of `firebase-tools`. Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started

at this line:

token: functions.config().fb.token

Is that because I am using it in a wrong way or is the snipped outdated? How would I use the described service account key in the case of this snippet? I appreciate your help, I am still new to GCP!!

Environment:
firebase-tools: 11.20.0
Node: 16

Steps to reproduce:
use the snipped as described

Expected behaviour:
Execute recursive delete of path

Actual behaviour:
Same, but with deprecation warning

Incorrect Comment?

var data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'DC'
var setDoc = db.collection('cities').doc('LA').set(data);

The comment says that the code creates a document with the name 'DC', but the path is for a document with the name 'LA'. I believe the comment should say 'LA'.

Sorry if this has already been noticed, I'm still new to this.

In refrence to https://github.com/firebase/snippets-node/tree/master/firestore/solution-scheduled-backups

Hi Samstern,
I tried to use your repository for the firestore backup using cron.yml. It is creating the bucket but writing 0 bytes.
I have a question about this line:
const url = https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments;
I have hardcoded like this: const url = 'https://firestore.googleapis.com/v1beta1/projects/cp-gaa-dev-dragonfly-arc/databases/(default):exportDocuments'

Please let me know what I am doing wrong.
Thanks
BC

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.