Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for set() with SetOptions when using FirestoreDataConverter #3254

Merged
merged 17 commits into from
Jun 26, 2020

Conversation

thebrianchen
Copy link

Porting over from node.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jun 22, 2020

Binary Size Report

Affected SDKs

  • @firebase/firestore

    Type Base (64d9ab6) Head (d0cfccc) Diff
    browser 248 kB 248 kB +186 B (+0.1%)
    esm2017 194 kB 194 kB +217 B (+0.1%)
    main 493 kB 494 kB +955 B (+0.2%)
    module 246 kB 246 kB +186 B (+0.1%)
  • @firebase/firestore/exp

    Type Base (64d9ab6) Head (d0cfccc) Diff
    main 37.4 kB 37.5 kB +130 B (+0.3%)
  • @firebase/firestore/lite

    Type Base (64d9ab6) Head (d0cfccc) Diff
    main 140 kB 140 kB +293 B (+0.2%)
  • @firebase/firestore/memory

    Type Base (64d9ab6) Head (d0cfccc) Diff
    browser 186 kB 186 kB +148 B (+0.1%)
    esm2017 145 kB 145 kB +175 B (+0.1%)
    main 363 kB 363 kB +631 B (+0.2%)
    module 184 kB 184 kB +148 B (+0.1%)
  • firebase

    Type Base (64d9ab6) Head (d0cfccc) Diff
    firebase-firestore.js 287 kB 287 kB +186 B (+0.1%)
    firebase-firestore.memory.js 226 kB 226 kB +148 B (+0.1%)
    firebase.js 820 kB 820 kB +186 B (+0.0%)

Test Logs

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

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

Basically LGTM, but we should provide a more helpful error message.

Comment on lines 8336 to 8345
/**
* Writes to the document referred to by the provided `DocumentReference`.
* If the document does not exist yet, it will be created. If you pass
* `SetOptions`, the provided data can be merged into the existing document.
*
* @param documentRef A reference to the document to be set.
* @param data An object of the fields and values for the document.
* @param options An object to configure the set behavior.
* @return This `Transaction` instance. Used for chaining method calls.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be not needed.

Copy link
Author

Choose a reason for hiding this comment

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

removed.

Comment on lines 158 to 161
set<T>(
documentRef: DocumentReference<T>,
data: T | Partial<T>,
options?: SetOptions
Copy link
Contributor

Choose a reason for hiding this comment

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

This catch-all override should not be needed in the type file.

Copy link
Author

Choose a reason for hiding this comment

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

removed.

Comment on lines 184 to 188
set<T>(
documentRef: DocumentReference<T>,
data: T | Partial<T>,
options?: SetOptions
): WriteBatch;
Copy link
Contributor

Choose a reason for hiding this comment

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

This catch-all override should not be needed in the type file.

Copy link
Author

Choose a reason for hiding this comment

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

removed.

set(data: T, options?: SetOptions): Promise<void>;
set(data: Partial<T>, options: SetOptions): Promise<void>;
set(data: T): Promise<void>;
set(data: T | Partial<T>, options?: SetOptions): Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

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

This catch-all override should not be needed in the type file.

Copy link
Author

Choose a reason for hiding this comment

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

removed.

batch.set(ref, { title: 'olive' }, { merge: true })
).to.throw(
'Function toFirestore() in WriteBatch.set() called with invalid data. Unsupported field value: undefined (found in field author)'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, this error message should prompt the user to provide a different converter.

Copy link
Author

Choose a reason for hiding this comment

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

I'll try looking into this some more in a subsequent PR. There doesn't seem to be an easy way to differentiate between the two possible converters once its been compiled down. The only way I can think of is parsing toFirestore.toString() and checking if the options parameter is included in the function. Do you see an easy implementation here?

Copy link
Contributor

Choose a reason for hiding this comment

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

The message is generated here:

functionName = 'toFirestore() in ' + functionName;

Isn't this message always wrong? The output of toFirestore() is invalid, not the input. Maybe change it to:

    functionName = functionName +  '() (converted by toFirestore()) ' +;

We need to change the way we pass the parenthesis though.

Since we are still waiting for sign off from @egilmorez, I vote we fix this in this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Done. Doing so cleans up applyFirestoreDataConverter() as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fantastic!

@changeset-bot
Copy link

changeset-bot bot commented Jun 25, 2020

🦋 Changeset is good to go

Latest commit: 57ed84b

We got this.

This PR includes changesets to release 10 packages
Name Type
firebase Minor
@firebase/firestore Minor
@firebase/firestore-types Minor
rxfire Major
@firebase/testing Patch
firebase-browserify-test Patch
firebase-package-typings-test Patch
firebase-messaging-selenium-test Patch
firebase-typescript-test Patch
firebase-webpack-test Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -314,14 +320,16 @@ export class UserDataReader {
methodName: string,
targetDoc: DocumentKey,
input: unknown,
options: firestore.SetOptions = {}
options: firestore.SetOptions = {},
hasConverter: boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Flip argument order to make optional argument go last.

Copy link
Author

Choose a reason for hiding this comment

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

done.

@@ -803,10 +813,16 @@ function createError(
reason: string,
methodName: string,
path?: FieldPath,
targetDoc?: DocumentKey
targetDoc?: DocumentKey,
hasConverter = false
Copy link
Contributor

Choose a reason for hiding this comment

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

Would prefer is this was not optional and the third argument. It is easy to miss setting otherwise.

Copy link
Author

Choose a reason for hiding this comment

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

done.

batch.set(ref, { title: 'olive' }, { merge: true })
).to.throw(
'Function toFirestore() in WriteBatch.set() called with invalid data. Unsupported field value: undefined (found in field author)'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fantastic!

@Feiyang1 Feiyang1 removed their assignment Jun 26, 2020
@egilmorez egilmorez requested a review from markarndt June 26, 2020 17:18
@thebrianchen thebrianchen assigned Feiyang1 and unassigned Feiyang1 Jun 26, 2020
Copy link
Collaborator

@markarndt markarndt left a comment

Choose a reason for hiding this comment

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

Quick question...

* `SetOptions`, the provided data can be merged into the existing document.
*
* @param documentRef A reference to the document to be set.
* @param data An object of the fields and values for the document.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry if I'm misreading this, but is SetOptions now required? Do we need a param for it?

Copy link
Author

Choose a reason for hiding this comment

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

We are adding an overload that allows to specify a data: Partial<T> parameter that must be accompanied by SetOptions. Using the data: T overload does not require SetOptions.

Copy link
Contributor

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

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

One style nit regarding backticks for code font, otherwise LG, thanks!

@@ -7859,9 +7859,11 @@ declare namespace firebase.firestore {
/**
* Called by the Firestore SDK to convert a custom model object of type T
* into a plain Javascript object (suitable for writing directly to the
* Firestore database).
* Firestore database). To use set() with `merge` and `mergeFields`,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the literal set() should have backticks.

Copy link
Author

Choose a reason for hiding this comment

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

done.

@@ -7859,9 +7859,11 @@ declare namespace firebase.firestore {
/**
* Called by the Firestore SDK to convert a custom model object of type T
* into a plain Javascript object (suitable for writing directly to the
* Firestore database).
* Firestore database). To use set() with `merge` and `mergeFields`,
* toFirestore() must be defined with `Partial<T>`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, suggest backticks.

Copy link
Author

Choose a reason for hiding this comment

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

done. thanks for the review!

@thebrianchen thebrianchen merged commit 39ca8ec into master Jun 26, 2020
@thebrianchen thebrianchen deleted the bc/set-override branch June 26, 2020 19:39
@github-actions github-actions bot mentioned this pull request Jul 6, 2020
@firebase firebase locked and limited conversation to collaborators Jul 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants