cachapa / firedart

A dart-native implementation of the Firebase Auth and Firestore SDKs
https://pub.dev/packages/firedart
Apache License 2.0
174 stars 62 forks source link

Timestamp Class Not Available - For Query and CRUD #127

Closed simeonangelov94 closed 9 months ago

simeonangelov94 commented 11 months ago

Hello,

To my understanding there is no Timestamp class that can be utilized using Firedart. I tried querying or creating documents with substitute conversions but Firebase is still not able to read it as Timestamp.I would appreciate if anybody can propose a solution.

Thank you for your help.

Best, Simeon

daveparks23 commented 9 months ago

You can just bring in the Timestamp class from cloud_firestore flutter package as a work around for now. Pretty simple class.

cachapa commented 9 months ago

Can you give an example of what a query using Timestamp should look like? This may be as simple as exporting the generated Timestamp class, but I'd like to create a test for it to make sure.

simeonangelov94 commented 9 months ago

My workaround was building my own API for the specific query.

FirebaseFirestore.instance.collection('Collection1').where('Date1', isGreaterThan: Timestamp.fromDate(Date1)).get();

For my REST API I used

extension Timestamp on DateTime { String toTimestamp() { return add(Duration(days: 0)).toIso8601String() + 'Z'; } }

which worked.

cachapa commented 9 months ago

That's just writing a DateTime object directly to the database, which from my testing Firebase stores as a timestamp on their side.

This code works for me:

  test('Timestamp query', () async {
    await firestore
        .document('test/query')
        .set({'test_field': DateTime.now()});
    var query = await firestore
        .collection('test')
        .where('test_field', isLessThan: DateTime.now())
        .get();
    expect(query.isNotEmpty, true);
  });

Am I missing something?

daveparks23 commented 8 months ago

That's just writing a DateTime object directly to the database, which from my testing Firebase stores as a timestamp on their side.

This code works for me:

  test('Timestamp query', () async {
    await firestore
        .document('test/query')
        .set({'test_field': DateTime.now()});
    var query = await firestore
        .collection('test')
        .where('test_field', isLessThan: DateTime.now())
        .get();
    expect(query.isNotEmpty, true);
  });

Am I missing something?

I just realized this also. But I haven't tested querying. So your saying if you query against a Timestamp using DateTime it will work as expected?