firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

FR: Support Firestore Timestamp

Open swftvsn opened this issue 8 years ago • 12 comments

Please please support Timestamp (See https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp) asap. We share code with backend and frontend, so we need to be able to upgrade both simultaneously or we need to duplicate a lot of code or write some mappers to all layers in the mean time.

swftvsn avatar Jun 20 '18 14:06 swftvsn

It's on the immediate roadmap and will be part of the next major release.

schmidt-sebastian avatar Jun 20 '18 19:06 schmidt-sebastian

Will this also enable code similar to this?

const date = new Date()
const timestamp = new TimeStamp(date)
collection.where('updatedAt', '<', timestamp)

updatedAt is of type timestamp in the database (Firestore), and I need to make a range query via firebase-admin.

plu avatar Jun 29 '18 11:06 plu

Yes. That will be supported.

schmidt-sebastian avatar Jun 29 '18 17:06 schmidt-sebastian

How does one access the Timestamp type? admin.firestore().Timestamp, and a few other things I've tried, don't seem to work.

Ruberik avatar Aug 18 '18 19:08 Ruberik

This should work:

const admin = require('firebase-admin');
const timestamp = new admin.firestore.Timestamp(0, 10);

schmidt-sebastian avatar Aug 18 '18 20:08 schmidt-sebastian

Thanks, Sebastian. Similarly, admin.firestore.Timestamp.fromDate(new Date()) works.

Ruberik avatar Aug 19 '18 01:08 Ruberik

        admin.initializeApp({
            credential: admin.credential.cert(environment.adminCredential as admin.ServiceAccount),
            storageBucket: environment.storageBucket
        });

        const firestore = admin.firestore();
        firestore.settings({ timestampsInSnapshots: true });

        (await firestore
            .collection(`/apps/${options.id}/flavors`)
            //.where('updatedAt', '<', Timestamp.fromDate(new Date()))
            .get())
            .docs
            .forEach(async flavor => {
                console.log(flavor.data());
            });

This gives a result of:

{ name: 'release/7.14.0',
  latestVersion:
   { name: '7.14.0-108876',
     bucket: 'laadkisten.appspot.com',
     updatedAt: Timestamp { _seconds: 1535115475, _nanoseconds: 796000000 },
     path: 'apps/kleinanzeigen-ios-sandbox/versions/release/7.14.0/7.14.0-108876/Kleinanzeigen-7.14.0-108876-sandbox-c0350efad-release7140.ipa' } }
{ latestVersion:
   { name: '7.13.0-108601',
     bucket: 'laadkisten.appspot.com',
     updatedAt: Timestamp { _seconds: 1534144939, _nanoseconds: 976000000 },
     path: 'apps/kleinanzeigen-ios-sandbox/versions/release_7_13_0_test_branch/7.13.0-108601/Kleinanzeigen-7.13.0-108601-sandbox-2188e01b5-release_7_13_0_test_branch.ipa' },
  name: 'release_7_13_0_test_branch' }

If I uncomment the .where('updatedAt', '<', Timestamp.fromDate(new Date())) invocation, I get zero results. Am I doing something wrong or are timestamps still not supported in a where clause?

Using firebase-admin 5.13.1.

plu avatar Aug 31 '18 08:08 plu

Try 'updatedAt', '<', 'new Date()'

Ruberik avatar Aug 31 '18 13:08 Ruberik

Please make sure to use the right types. It should be admin.firestore.Timestamp.fromDate(new Date()).

schmidt-sebastian avatar Aug 31 '18 19:08 schmidt-sebastian

I put below codes on the top of javascript(not typescript) file, then it got the error.

const admin = require('firebase-admin');
const today = new admin.firestore.Timestamp(new Date());

Error: Value for argument "seconds" is not a valid integer.
    at Object.validateInteger (/Users/app/functions/node_modules/@google-cloud/firestore/build/src/validate.js:191:19)
    at new Timestamp (/Users/app/functions/node_modules/@google-cloud/firestore/build/src/timestamp.js:54:20)

how can I use the Timestamp on a firebase function with javascript.

SpaceNet avatar Mar 03 '20 02:03 SpaceNet

@SpaceNet see the comment right before yours. You're calling Timestamp(new Date()) while schmidt-sebastian is calling Timestamp.fromDate(new Date()).

Ruberik avatar Mar 03 '20 03:03 Ruberik

@SpaceNet The Timestamp constructor expects a pair of seconds/nanoseconds as arguments. If you want pass a Date, use Timestamp.fromDate() as @Ruberik suggested.

schmidt-sebastian avatar Mar 03 '20 18:03 schmidt-sebastian