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

Improve Connection Typings #11418

Merged
merged 4 commits into from
Feb 24, 2022
Merged

Conversation

Uzlopak
Copy link
Collaborator

@Uzlopak Uzlopak commented Feb 17, 2022

This PR extracts the Connection Typings

With the first commit I extract the typings from Connection from index.d.ts into a new Connection.d.ts.
With the second commit I do the actual improvements.

The order of the function overloads are according to Typescript Do's and Don'ts, so basically by most arity to least arity.
Attributes like pass, user, models etc. were set to readonly. nobody should use them directly for manipulating the internals of mongoose. If there is some reason to modify them, a developer should use the corresponding methods.

Just check the second commit to see relevant changes.

Also added typings tests.

collection?: string,
options?: CompileModelOptions
): U;
model<T>(name: string, schema?: Schema<T>, collection?: string, options?: CompileModelOptions): Model<T>;
Copy link
Collaborator Author

@Uzlopak Uzlopak Feb 17, 2022

Choose a reason for hiding this comment

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

Question to all:
shouldnt we do here:
model<T extends AnyObject = AnyObject>

With the current typings we are forcing a dev to type .model(). I think better DX is to make it optional for the dev.

* and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
*/
startSession(options: mongodb.ClientSessionOptions | undefined | null, callback: Callback<mongodb.ClientSession>): void;
startSession(callback: Callback<mongodb.ClientSession>): void;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added this, because in startSession we check if first argument is function and if so use it as callback.

*/
readonly readyState: ConnectionStates;

/** Sets the value of the option `key`. */
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

here we had the information that set is the same as connection.options[key] = value.

The typings do not expose the options attribute, so I removed this remark here and the same for get

* - 3 = disconnecting
* - 99 = uninitialized
*/
readonly readyState: ConnectionStates;
Copy link
Collaborator Author

@Uzlopak Uzlopak Feb 17, 2022

Choose a reason for hiding this comment

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

using connectionStates instead of number

* The host name portion of the URI. If multiple hosts, such as a replica set,
* this will contain the first host name in the URI
*/
readonly host: string;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

please give me feedback regarding the readonly on host, user, pass etc.
Is it wrong? Is there actually a use case for having them writeable?

If I am wrong, then I would like to fix it before we have regression reports

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think readonly should be fine, modifying host is a noop.

schema?: Schema<T, U, TQueryHelpers>,
collection?: string,
options?: CompileModelOptions
): U;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ReturnType U seems odd. Should it be Model or does U should extend from Model?

Copy link
Collaborator

Choose a reason for hiding this comment

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

U should extend from Model, but I believe we removed that for perf reasons in #10349. U extends Model causes perf issues for the TS compiler

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.

2 participants