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

Have client.connect() return a Promise<RedisClient> #2602

Merged
merged 7 commits into from
Sep 18, 2023
Merged

Have client.connect() return a Promise<RedisClient> #2602

merged 7 commits into from
Sep 18, 2023

Conversation

franciscop
Copy link
Contributor

@franciscop franciscop commented Aug 19, 2023

Description

Make the .connect() method to return a promise with an instance of itself, so we can do:

const client = await createClient().connect();

Issue: Have client.connect() return a Promise<client> instead of Promise<void>

As explained above, added a case that simplifies the creation of the client for those who want to initialize AND connect at the same time, while retaining the ability to do so separately as before otherwise.

Added a test to make sure the return of connet() is the same client instance. I noticed there was no test explicitly for .connect(), so created it under the describe('.connect()') name.


Checklist

  • Does npm test pass with this change (including linting)? (tests)
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

@franciscop franciscop marked this pull request as ready for review August 19, 2023 12:31
@codecov-commenter
Copy link

codecov-commenter commented Aug 22, 2023

Codecov Report

Patch coverage: 80.00% and project coverage change: -0.07% ⚠️

Comparison is base (259e9b2) 95.70% compared to head (b4b5fa8) 95.63%.
Report is 6 commits behind head on master.

❗ Current head b4b5fa8 differs from pull request most recent head 10b411e. Consider uploading reports for the commit 10b411e to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2602      +/-   ##
==========================================
- Coverage   95.70%   95.63%   -0.07%     
==========================================
  Files         456      456              
  Lines        4561     4564       +3     
  Branches      524      526       +2     
==========================================
  Hits         4365     4365              
- Misses        127      128       +1     
- Partials       69       71       +2     
Files Changed Coverage Δ
packages/client/lib/client/index.ts 92.53% <80.00%> (-1.05%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@leibale
Copy link
Collaborator

leibale commented Aug 24, 2023

@franciscop thanks for contributing! I made some changes, wanna review them? 🙏

Comment on lines 429 to 434
async connect() {
// see comment in constructor
this.#isolationPool ??= this.#initiateIsolationPool();
await this.#socket.connect();
return this;
return this as unknown as RedisClientType<M, F, S>;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks good! I am not familiar enough with TS to know if there's any meaningful difference here, but I've seen that style in the past so sounds good.

Comment on lines 111 to 116
testUtils.testWithClient('connect should return the clietn instance', async client => {
try {
assert.equal(await client.connect(), client);
} finally {
if (client.isOpen) await client.disconnect();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

much cleaner test, thanks for the update!

@franciscop
Copy link
Contributor Author

wow it's a honor to see the changes be the new main recommended way to connect to Redis! I went and submitted these changes as an alternative mode only, but yeah happy to see them up front!

For the new code: the types seem good, thanks for correcting them I'm not that familiar with TS. Same for the test, your update is much cleaner!

Unfortunately I cannot approve the PR since I'm not part of node-redis, but yes they all seem good!

Co-authored-by: Francisco Presencia <franciscop@users.noreply.github.com>
@leibale
Copy link
Collaborator

leibale commented Aug 29, 2023

I'll merge it and release it "soon" (along with a few more PRs) 🎉
Thanks a bunch! :)

@leibale leibale merged commit fb255eb into redis:master Sep 18, 2023
18 checks passed
@leibale
Copy link
Collaborator

leibale commented Sep 19, 2023

@franciscop redis@4.6.9/@redis/client@1.5.10 is on npm 🎉

@franciscop
Copy link
Contributor Author

Thanks!! 🥳🥳

@leppaott
Copy link

leppaott commented Jan 9, 2024

How about cluster.connect()?

@leibale
Copy link
Collaborator

leibale commented Jan 16, 2024

@leppaott in v5.. :)

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.

Have client.connect() return a Promise<client> instead of Promise<void>
4 participants