FR: Support Firestore Timestamp
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.
It's on the immediate roadmap and will be part of the next major release.
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.
Yes. That will be supported.
How does one access the Timestamp type? admin.firestore().Timestamp, and a few other things I've tried, don't seem to work.
This should work:
const admin = require('firebase-admin');
const timestamp = new admin.firestore.Timestamp(0, 10);
Thanks, Sebastian. Similarly, admin.firestore.Timestamp.fromDate(new Date()) works.
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.
Try 'updatedAt', '<', 'new Date()'
Please make sure to use the right types. It should be admin.firestore.Timestamp.fromDate(new Date()).
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 see the comment right before yours. You're calling Timestamp(new Date()) while schmidt-sebastian is calling Timestamp.fromDate(new Date()).
@SpaceNet The Timestamp constructor expects a pair of seconds/nanoseconds as arguments. If you want pass a Date, use Timestamp.fromDate() as @Ruberik suggested.