When you set a field in a Firestore document to firebase.firestore.FieldValue.serverTimestamp(), what will be the value of that field immediately after the write operation completes on the client?
Think about how Firestore handles latency and synchronization between client and server.
When you write serverTimestamp(), Firestore sets a placeholder on the client. This placeholder is replaced asynchronously with the actual server time once the server confirms the write. This allows the client to show a temporary value while waiting for the server.
You want to update a Firestore document's lastModified field to the current server time. Which of the following update commands correctly sets the lastModified field to the server timestamp?
const docRef = firestore.collection('users').doc('user123'); // Which update command is correct?
Consider which method updates only the specified fields and which value represents the server timestamp.
Using update() with firebase.firestore.FieldValue.serverTimestamp() sets the field to the server time. set() replaces the whole document unless merge is used. new Date() uses client time, and Timestamp.now() is client-side timestamp, not server.
You are designing an audit log system in Firestore that records user actions with timestamps. You want to ensure timestamps are consistent and trustworthy. Which architecture choice best achieves this?
Think about trustworthiness and consistency of timestamps in distributed systems.
Using serverTimestamp() ensures all timestamps come from the server, avoiding client clock skew or manipulation. This is critical for audit logs. Cloud Functions can add timestamps but add complexity and latency. Client timestamps are unreliable.
You want to prevent clients from spoofing timestamps in Firestore writes. Which Firestore security rule best enforces that the createdAt field is set only to the server timestamp?
Consider how Firestore security rules access server time and request data.
In Firestore security rules, request.time is the server timestamp at the time of the request. Comparing createdAt to request.time ensures the client cannot spoof the timestamp. Other options either compare to client data or are always true.
When a Firestore client is offline and writes a document with a serverTimestamp() field, what is the best practice to handle the timestamp field in your app UI before the client reconnects?
Think about user experience and data consistency when server data is delayed.
Since server timestamps are placeholders offline, showing a loading indicator or placeholder informs users the timestamp is pending. Showing client time risks confusion. Hiding or nulling loses context or misleads users.