Skip to content

Conversation

@mamillastre
Copy link
Contributor

@mamillastre mamillastre commented Jul 17, 2024

Pull request checklist

Please check if your PR fulfills the following requirements:

  • The changes have been tested successfully.
  • A changeset has been created (npm run changeset).
  • I have read and followed the pull request guidelines.

Close #474, #443

Linked PR: #557

Usage example:

import { FirebaseFirestore, Timestamp, serverTimestamp } from "@capacitor-firebase/firestore";

FirebaseFirestore.setDocument({
    reference: "collection/doc",
    data: {
      timestamp: Timestamp.now(),
      fieldValue: serverTimestamp()
    },
 });

@mamillastre
Copy link
Contributor Author

Hi @robingenz,

For now, this PR is a prototype of the Timestamp & FieldValue management on Web and Android.

Do you have some time to make early feedbacks on this before I continue the development?

Especially on these points:

  • You advise me here to manage the data parsing from the native layer like the Bluetooth Low Energy Plugin. But I have to implement all the plugin methods, even those that are out of scope.
    Here, I use a Proxy. Tell me if you are ok with this solution.
  • When using the plugin, the firebase dependency is mandatory for the Timestamp & FieldValue usage. But I think you prefer avoiding this.
    In this case, I see two possibilities:
    1. Copying the Timestamp code from the firebase js SDK into the plugin
    2. Set the Timestamp implementation in a separated index.js file to avoid to install the firebase dependency if the timestamp is never used

Thank you

@robingenz
Copy link
Member

@mamillastre Thanks for the update. I'm currently on vacation and won't be back for another 2 weeks. I'll have a look at it as soon as i'm back.

@mamillastre
Copy link
Contributor Author

Hi @robingenz,
Do you have some time to do a mid-term review on this feature?
This feature will have a strong impact on the plugin.
Thank you.

@robingenz
Copy link
Member

Hi @mamillastre, I'll try to take a look at your PR over the next few weeks. The problem is that this is a complex feature that will probably keep me busy for several days.

@mamillastre mamillastre force-pushed the timestamp-fieldvalue branch from cab4468 to 21be503 Compare January 6, 2025 10:10
@mamillastre
Copy link
Contributor Author

I added all the timestamp & field values methods management for the web and android platforms.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 6, 2025

Open in StackBlitz

@capacitor-firebase/analytics

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/analytics@677

@capacitor-firebase/app

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/app@677

@capacitor-firebase/app-check

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/app-check@677

@capacitor-firebase/authentication

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/authentication@677

@capacitor-firebase/crashlytics

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/crashlytics@677

@capacitor-firebase/firestore

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/firestore@677

@capacitor-firebase/functions

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/functions@677

@capacitor-firebase/messaging

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/messaging@677

@capacitor-firebase/performance

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/performance@677

@capacitor-firebase/remote-config

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/remote-config@677

@capacitor-firebase/storage

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/storage@677

commit: 6705068

@mamillastre
Copy link
Contributor Author

Note on iOS: the solution to use the "toJSON()" method to serialize the timestamps and field values do not work on iOS (unlike Android).
The "toJSON()" method is not called on iOS when capacitor uses the "postMessage()" method to send data to the native platform.

Another solution must be found. For example:

  • Use the JSON.parse(JSON.stringify(data)) technique on the document data to force the serialization before calling the actual plugin method.
  • Do a deep traversal of the document data to serialize the needed data before calling the actual plugin method.

These solutions can have a negative impact on the plugin methods performances depending on the document size.

@robingenz robingenz self-assigned this Jan 7, 2025
@mamillastre mamillastre force-pushed the timestamp-fieldvalue branch from fc98ced to 5e4ca01 Compare January 9, 2025 13:51
@petermakeswebsites
Copy link

petermakeswebsites commented Feb 22, 2025

Is there an ETA on this? It looks like there's some pretty good progress, but I'm wondering if I should wait out or create a scrappy workaround for my particular use case.

@mamillastre
Copy link
Contributor Author

Hi @petermakeswebsites,
I intend to finalize this development soon, but I don't have much time to work on this.
Next, there will be a review phase.

I hope this feature can come with a 7.1 or 7.2 version. But there is no guarantee.

You can test this feature right now using this dependency:

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/firestore@677

@mamillastre
Copy link
Contributor Author

mamillastre commented Feb 25, 2025

This PR is now ready for review.

Here is an example of use:

import { FirebaseFirestore } from '@capacitor-firebase/firestore';
import { Timestamp } from "@capacitor-firebase/firestore/timestamp";
import { arrayRemove, arrayUnion, deleteField, increment, serverTimestamp } from "@capacitor-firebase/firestore/field-value";

const setDocumentWithTimestamp = async () => {
  await FirebaseFirestore.setDocument({
    reference: 'docs/timestamp',
    data: {
      now: Timestamp.now(),
      fromDate: Timestamp.fromDate(new Date("2024-03-24")),
      timestamp: new Timestamp(1711238400, 0)
    },
  });
};

const setDocumentWithFiledValues = async () => {
  await FirebaseFirestore.setDocument({
    reference: 'docs/field-value',
    data: {
      serverTimestamp: serverTimestamp(),
      increment: increment(1),
      fieldToDelete: deleteField(),
      arrayUnion: arrayUnion('newItem1', 'newItem2'),
      arrayRemove: arrayRemove('itemToRemove'),
    },
  });
};

I separate these functionalities into their own modules to avoid to include the firebase web dependency in the main bundle.

@mamillastre mamillastre marked this pull request as ready for review February 25, 2025 09:44
@mamillastre
Copy link
Contributor Author

Hi @robingenz, do you have some time to make the code review? Thank you

@robingenz
Copy link
Member

@mamillastre Yes, but I'm not happy with the PR yet. The Firebase JS SDK shouldn't be mandatory on Android and iOS. As soon as I have time, I'll try to come up with some alternative solutions. Until then, I'll leave this PR open for now.

@mamillastre
Copy link
Contributor Author

Thank you very much @robingenz.
In fact, the JS SDK is mandatory if importing @capacitor-firebase/firestore/timestamp or @capacitor-firebase/firestore/field-value.
It is difficult to avoid this when using the Timestamp object since it exposes some JS API.
I think it would be simpler for the field values.

@mamillastre mamillastre force-pushed the timestamp-fieldvalue branch from a612a83 to b4f51c6 Compare August 13, 2025 18:34
@mamillastre
Copy link
Contributor Author

The branch has been rebased on the 7.3.0 version.
I have also improved the timestamp & field-value packages imports to fix the errors on some build configurations ("moduleResolution": "node") and removed the bundle JS generation that included the plugin AND these side packages.

@niteshgrg
Copy link

Seems like there is good progress just wandering if @robingenz you will be able to prfioritize reviewing this or should i look for alternative time storage format.

@niteshgrg
Copy link

@mamillastre Any way to use this before it gets merged also. I can try it out in my project.

@mamillastre
Copy link
Contributor Author

@niteshgrg,
This project auto-generates the packages associated with a PR. you can install this feature using:

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/firestore@677

But be careful, it will follow any updates we make in this pull request.
Today, the branch is based on the 7.3.0 version.
If you want to prevent any errors related to the version, you can copy the package folder in your project.

@robingenz
Copy link
Member

@mamillastre There are also pre-releases for every single commit.

@mamillastre
Copy link
Contributor Author

@mamillastre There are also pre-releases for every single commit.

I didn't know that. Very useful ! Thank you.

For my last commit:

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/firestore@6705068

@niteshgrg
Copy link

Great thanks for this. Will incoorporate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(firestore): Timestamp values

4 participants