Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 22 additions & 6 deletions pkg/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Deno.test("with destructuring", async (t) => {

Deno.test("with single command", async (t) => {
await t.step("works with multiple commands", async () => {
const p = new Pipeline(client);
const p = new Pipeline({ client });
p.set(newKey(), randomID());
const res = await p.exec();
assertEquals(res.length, 1);
Expand All @@ -41,7 +41,8 @@ Deno.test("with single command", async (t) => {
Deno.test("when chaining in a for loop", async (t) => {
await t.step("works", async () => {
const key = newKey();
const res = await new Pipeline(client).set(key, randomID()).get(key).exec();
const res = await new Pipeline({ client }).set(key, randomID()).get(key)
.exec();

assertEquals(res.length, 2);
});
Expand All @@ -50,7 +51,7 @@ Deno.test("when chaining in a for loop", async (t) => {
Deno.test("when chaining inline", async (t) => {
await t.step("works", async () => {
const key = newKey();
const p = new Pipeline(client);
const p = new Pipeline({ client });
for (let i = 0; i < 10; i++) {
p.set(key, randomID());
}
Expand All @@ -62,20 +63,35 @@ Deno.test("when chaining inline", async (t) => {

Deno.test("when no commands were added", async (t) => {
await t.step("throws", async () => {
await assertRejects(() => new Pipeline(client).exec());
await assertRejects(() => new Pipeline({ client }).exec());
});
});

Deno.test("when one command throws an error", async (t) => {
await t.step("throws", async () => {
const p = new Pipeline(client).set("key", "value").hget("key", "field");
const p = new Pipeline({ client }).set("key", "value").hget("key", "field");
await assertRejects(() => p.exec());
});
});

Deno.test("transaction", async (t) => {
await t.step("works", async () => {
const key = newKey();
const value = randomID();
const tx = new Pipeline({ client, multiExec: true });
tx.set(key, value);
tx.get(key);
tx.del(key);

const [ok, storedvalue, deleted] = await tx.exec<["OK", string, number]>();
assertEquals(ok, "OK");
assertEquals(storedvalue, value);
assertEquals(deleted, 1);
});
});
Deno.test("use all the things", async (t) => {
await t.step("works", async () => {
const p = new Pipeline(client);
const p = new Pipeline({ client });

const persistentKey = newKey();
const persistentKey2 = newKey();
Expand Down
18 changes: 12 additions & 6 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ import { ZMScoreCommand } from "./commands/zmscore.ts";
* **Examples:**
*
* ```ts
* const p = redis.pipeline()
* const p = redis.pipeline() // or redis.multi()
* p.set("key","value")
* p.get("key")
* const res = await p.exec()
Expand All @@ -168,11 +168,17 @@ export class Pipeline {
private client: Requester;
private commands: Command<unknown, unknown>[];
private commandOptions?: CommandOptions<any, any>;
constructor(client: Requester, commandOptions?: CommandOptions<any, any>) {
this.client = client;
private multiExec: boolean;
constructor(opts: {
client: Requester;
commandOptions?: CommandOptions<any, any>;
multiExec?: boolean;
}) {
this.client = opts.client;

this.commands = [];
this.commandOptions = commandOptions;
this.commandOptions = opts.commandOptions;
this.multiExec = opts.multiExec ?? false;
}

/**
Expand All @@ -191,9 +197,9 @@ export class Pipeline {
if (this.commands.length === 0) {
throw new Error("Pipeline is empty");
}

const path = this.multiExec ? ["multi-exec"] : ["pipeline"];
const res = (await this.client.request({
path: ["pipeline"],
path,
body: Object.values(this.commands).map((c) => c.command),
})) as UpstashResponse<any>[];

Expand Down
23 changes: 22 additions & 1 deletion pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,28 @@ export class Redis {
*
* @see {@link Pipeline}
*/
pipeline = () => new Pipeline(this.client, this.opts);
pipeline = () =>
new Pipeline({
client: this.client,
commandOptions: this.opts,
multiExec: false,
});

/**
* Create a new transaction to allow executing multiple steps atomically.
*
* All the commands in a transaction are serialized and executed sequentially. A request sent by
* another client will never be served in the middle of the execution of a Redis Transaction. This
* guarantees that the commands are executed as a single isolated operation.
*
* @see {@link Pipeline}
*/
multi = () =>
new Pipeline({
client: this.client,
commandOptions: this.opts,
multiExec: true,
});

/**
* @see https://redis.io/commands/append
Expand Down