Skip to content

Commit

Permalink
Have client.connect() return a Promise<RedisClient> (#2602)
Browse files Browse the repository at this point in the history
* Connect returns the instance of the client

* Added a test

* No auto setup

* Added a bit of docs

* fix the return type, test, and the docs

* fix return type

* Update packages/client/lib/client/index.spec.ts

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

---------

Co-authored-by: Leibale Eidelman <me@leibale.com>
  • Loading branch information
franciscop and leibale committed Sep 18, 2023
1 parent 5a10826 commit fb255eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 3 additions & 5 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 Down
13 changes: 13 additions & 0 deletions packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ describe('Client', () => {
});
});

describe('connect', () => {
testUtils.testWithClient('connect should return the client instance', async client => {
try {
assert.equal(await client.connect(), client);
} finally {
if (client.isOpen) await client.disconnect();
}
}, {
...GLOBAL.SERVERS.PASSWORD,
disableClientSetup: true
});
});

describe('authentication', () => {
testUtils.testWithClient('Client should be authenticated', async client => {
assert.equal(
Expand Down
5 changes: 3 additions & 2 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ export default class RedisClient<
});
}

connect(): Promise<void> {
async connect() {
// see comment in constructor
this.#isolationPool ??= this.#initiateIsolationPool();
return this.#socket.connect();
await this.#socket.connect();
return this as unknown as RedisClientType<M, F, S>;
}

async commandsExecutor<C extends RedisCommand>(
Expand Down

0 comments on commit fb255eb

Please sign in to comment.