Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore moving to timestamps in Firebase functions

Yesterday, all my firebase functions started throwing the following warning:

The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:

const firestore = new Firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};  
firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:

// Old:   const date = snapshot.get('created_at');
// New:   const timestamp = snapshot.get('created_at');   const date =
timestamp.toDate();

Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will change to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

Now I want to init my firestore correctly according to this warning. i'm using typescript.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';    
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()

the firestore() that return from admin doesn't have a .settings() method as described in the warning nor it gets an object in the constructor. (It gets an App Object). so I have two questions:

  1. How can init my firestore to get the settings object?

  2. when I insert a date to a document or when I query a document do I also need to pass the Firestore.Timestamp object? or can i query/insert the normal JS Date object and it will get converted automatically?

Thanks.

EDIT:

i managed to solve it for http functions using :

if (!fireStoreDB){
    fireStoreDB = admin.firestore();
    fireStoreDB.settings(settings);
}

But it still is happening on firestore triggers. anyone knows how to give default settings to admin on :

admin.initializeApp(functions.config().firebase);

so it will not happen on firestore triggers?

like image 875
Shahar Wider Avatar asked Jul 19 '18 08:07

Shahar Wider


People also ask

How do I change firestore to timestamp?

To convert a Firestore Timestamp into a Javascript date, just call . toDate() on the Timestamp.

Can I use Realtime Database and firestore at the same time?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.

What is timestamp in firebase?

What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.


1 Answers

You are still receiving JS Date instead of Firestore Timestamp due to a bug... now fixed in Firebase Functions v2.0.2. See: https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.

For initialisation I've used admin.firestore().settings({timestampsInSnapshots: true}) as specified in warning message, so the warning has disappeared.

When you add a date to a Document, or use as a query parameter, you can use the normal JS Date object.

like image 142
marcogramy Avatar answered Sep 22 '22 00:09

marcogramy