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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ Looking for a high-level library to handle object mapping? See [redis-om-node](h
```typescript
import { createClient } from 'redis';

const client = createClient();

client.on('error', err => console.log('Redis Client Error', err));

await client.connect();
const client = await createClient()
.on('error', err => console.log('Redis Client Error', err))
.connect();

await client.set('key', 'value');
const value = await client.get('key');
Expand All @@ -70,13 +68,6 @@ createClient({
});
```

You can create the client and connect it in a single line, but if you do so and it throws an error it will halt the whole Node process:

```typescript
// In this example an error connecting will throw
const client = await createClient().connect();
```

You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in the [client configuration guide](./docs/client-configuration.md).

To check if the the client is connected and ready to send commands, use `client.isReady` which returns a boolean. `client.isOpen` is also available. This returns `true` when the client's underlying socket is open, and `false` when it isn't (for example when the client is still connecting or reconnecting after a network error).
Expand Down
10 changes: 6 additions & 4 deletions packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ describe('Client', () => {
});

describe('connect', () => {
testUtils.testWithClient('connect returns an instance', async (client) => {
const client2 = await client.connect();
assert.equal(client, client2);
await client.disconnect();
testUtils.testWithClient('connect should return the clietn instance', async client => {
leibale marked this conversation as resolved.
Show resolved Hide resolved
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!

}, {
...GLOBAL.SERVERS.PASSWORD,
disableClientSetup: true
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export default class RedisClient<
});
}

async connect(): Promise<RedisClient<M, F, S>> {
async connect(): Promise<RedisClientType<M, F, S>> {
// see comment in constructor
this.#isolationPool ??= this.#initiateIsolationPool();
await this.#socket.connect();
Expand Down
Loading