Skip to content

Commit

Permalink
fix(firestore, types): allow FieldValues, Date and Timestamp in doc s…
Browse files Browse the repository at this point in the history
…et and update (#5901)

* Allow setting FieldValues and allow Date in place of Timestamp
* Add tests
  • Loading branch information
Yonom authored Dec 13, 2021
1 parent f7313b9 commit 5f4eadf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/firestore/__tests__/firestore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import firestore, { firebase } from '../lib';
import firestore, { firebase, FirebaseFirestoreTypes } from '../lib';

const COLLECTION = 'firestore';

Expand Down Expand Up @@ -306,5 +306,27 @@ describe('Storage', function () {
},
});
});

it('does not throw when Date is provided instead of Timestamp', async function () {
type BarType = {
myDate: FirebaseFirestoreTypes.Timestamp;
};

const docRef = firebase.firestore().doc<BarType>(`${COLLECTION}/bar`);
await docRef.set({
myDate: new Date(),
});
});

it('does not throw when serverTimestamp is provided instead of Timestamp', async function () {
type BarType = {
myDate: FirebaseFirestoreTypes.Timestamp;
};

const docRef = firebase.firestore().doc<BarType>(`${COLLECTION}/bar`);
await docRef.set({
myDate: firestore.FieldValue.serverTimestamp(),
});
});
});
});
15 changes: 13 additions & 2 deletions packages/firestore/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export namespace FirebaseFirestoreTypes {
* @param data A map of the fields and values for the document.
* @param options An object to configure the set behavior.
*/
set(data: T, options?: SetOptions): Promise<void>;
set(data: SetValue<T>, options?: SetOptions): Promise<void>;

/**
* Updates fields in the document referred to by this `DocumentReference`. The update will fail
Expand All @@ -448,7 +448,7 @@ export namespace FirebaseFirestoreTypes {
*
* @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document.
*/
update(data: Partial<{ [K in keyof T]: T[K] | FieldValue }>): Promise<void>;
update(data: Partial<SetValue<T>>): Promise<void>;

/**
* Updates fields in the document referred to by this DocumentReference. The update will fail if
Expand Down Expand Up @@ -2080,6 +2080,17 @@ export namespace FirebaseFirestoreTypes {
*/
useEmulator(host: string, port: number): void;
}

/**
* Utility type to allow FieldValue and to allow Date in place of Timestamp objects.
*/
export type SetValue<T> = T extends Timestamp
? Timestamp | Date // allow Date in place of Timestamp
: T extends object
? {
[P in keyof T]: SetValue<T[P]> | FieldValue; // allow FieldValue in place of values
}
: T;
}

declare const defaultExport: ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<
Expand Down

1 comment on commit 5f4eadf

@vercel
Copy link

@vercel vercel bot commented on 5f4eadf Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.