Skip to content

Commit 2e0e600

Browse files
suguru03keroxp
authored andcommitted
feat: add db option and remove a deprecated option (#32)
resolves: #30
1 parent 874c249 commit 2e0e600

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

redis.ts

+21-37
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ export type RedisConnectOptions = {
15481548
hostname: string;
15491549
port?: number | string;
15501550
tls?: boolean;
1551+
db?: number;
15511552
};
15521553

15531554
/**
@@ -1557,44 +1558,27 @@ export type RedisConnectOptions = {
15571558
* const conn = connect({hostname: "127.0.0.1", port: 6379})// -> tcp, 127.0.0.1:6379
15581559
* const conn = connect({hostname: "redis.proxy", port: 443, tls: true}) // -> TLS, redis.proxy:443
15591560
*/
1560-
export async function connect(
1561-
opts: string | RedisConnectOptions
1562-
): Promise<Redis> {
1563-
let conn: Deno.Conn;
1564-
if (typeof opts === "string") {
1565-
console.warn(
1566-
yellow(
1567-
"deno-redis: connect(addr) is now deprecated and will be removed in v0.5.0 (now v0.4.x)"
1568-
)
1569-
);
1570-
const [h, p] = opts.split(":");
1571-
if (!p) {
1572-
throw new Error("redis: port must be specified");
1573-
}
1574-
const dialOptions: DialOptions = { port: parseInt(p) };
1575-
if (h) {
1576-
dialOptions.hostname = h;
1577-
}
1578-
conn = await Deno.dial(dialOptions);
1579-
} else {
1580-
const { hostname } = opts;
1581-
const port = parseInt(`${opts.port}`);
1582-
if (!Number.isSafeInteger(port)) {
1583-
throw new Error("deno-redis: opts.port is invalid");
1584-
}
1585-
if (opts.tls) {
1586-
conn = await Deno.dialTLS({
1587-
hostname,
1588-
port
1589-
});
1590-
} else {
1591-
conn = await Deno.dial({
1592-
hostname,
1593-
port
1594-
});
1595-
}
1561+
export async function connect({
1562+
hostname,
1563+
port,
1564+
tls,
1565+
db,
1566+
}: RedisConnectOptions): Promise<Redis> {
1567+
const dialOpts: DialOptions = {
1568+
hostname,
1569+
port: typeof port === "string" ? parseInt(port) : port ?? 6379
1570+
};
1571+
if (!Number.isSafeInteger(dialOpts.port)) {
1572+
throw new Error("deno-redis: opts.port is invalid");
1573+
}
1574+
const conn: Deno.Conn = tls
1575+
? await Deno.dialTLS(dialOpts)
1576+
: await Deno.dial(dialOpts);
1577+
const client = await create(conn, conn, conn);
1578+
if (db) {
1579+
await client.select(db);
15961580
}
1597-
return create(conn, conn, conn);
1581+
return client;
15981582
}
15991583

16001584
export function create(

redis_test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ test(async function testConcurrent() {
128128
assertEquals(c, "c");
129129
});
130130

131+
test(async function testDb0Option() {
132+
const key = "exists";
133+
await redis.set(key, "aaa");
134+
const exists1 = await redis.exists(key);
135+
assertEquals(exists1, 1);
136+
const client2 = await connect({ ...addr, db: 0 });
137+
const exists2 = await client2.exists(key);
138+
assertEquals(exists2, 1);
139+
});
140+
141+
test(async function testDb1Option() {
142+
const key = "exists";
143+
await redis.set(key, "aaa");
144+
const exists1 = await redis.exists(key);
145+
assertEquals(exists1, 1);
146+
const client2 = await connect({ ...addr, db: 1 });
147+
const exists2 = await client2.exists(key);
148+
assertEquals(exists2, 0);
149+
});
150+
131151
[Infinity, NaN, "", "port"].forEach(v => {
132152
test(`invalid port: ${v}`, () => {
133153
assertThrowsAsync(

0 commit comments

Comments
 (0)