Skip to content

Commit

Permalink
Add length to pipeline (#656)
Browse files Browse the repository at this point in the history
* Add length to pipeline

* Run fmt on pipeline.ts
  • Loading branch information
ogzhanolguncu authored Oct 17, 2023
1 parent 8251125 commit 6a110e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
7 changes: 4 additions & 3 deletions examples/google-cloud-functions-with-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ npm install

2. Create a free Database on [upstash.com](https://console.upstash.com/redis)
3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to
`Runtime, build, connections and security settings > Runtime environment variables` in GCP website when creating a new function or pass those keys when you are deploying from the CLI.

`Runtime, build, connections and security settings > Runtime environment variables`
in GCP website when creating a new function or pass those keys when you are
deploying from the CLI.

## Work locally

Simply run `npm run start`
Simply run `npm run start`
7 changes: 4 additions & 3 deletions examples/google-cloud-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ npm install

2. Create a free Database on [upstash.com](https://console.upstash.com/redis)
3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to
`Runtime, build, connections and security settings > Runtime environment variables` in GCP when creating a new function or pass those keys when you are deploying from the CLI.

`Runtime, build, connections and security settings > Runtime environment variables`
in GCP when creating a new function or pass those keys when you are deploying
from the CLI.

## Work locally

Simply run `npm run start`
Simply run `npm run start`
21 changes: 21 additions & 0 deletions pkg/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ Deno.test("when no commands were added", async (t) => {
});
});

Deno.test("when length called", async (t) => {
await t.step("before exec()", () => {
const key = newKey();
const p = new Pipeline({ client });
for (let i = 0; i < 10; i++) {
p.set(key, randomID());
}
assertEquals(p.length(), 10);
});

await t.step("after exec()", async () => {
const key = newKey();
const p = new Pipeline({ client });
for (let i = 0; i < 10; i++) {
p.set(key, randomID());
}
await p.exec();
assertEquals(p.length(), 10);
});
});

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");
Expand Down
9 changes: 8 additions & 1 deletion pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
}) {
this.client = opts.client;

this.commands = ([] as unknown) as TCommands; // the TCommands generic in the class definition is only used for carrying through chained command types and should never be explicitly set when instantiating the class
this.commands = [] as unknown as TCommands; // the TCommands generic in the class definition is only used for carrying through chained command types and should never be explicitly set when instantiating the class
this.commandOptions = opts.commandOptions;
this.multiExec = opts.multiExec ?? false;
}
Expand Down Expand Up @@ -250,6 +250,13 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
}) as TCommandResults;
};

/**
* Returns the length of pipeline before the execution
*/
length(): number {
return this.commands.length;
}

/**
* Pushes a command into the pipeline and returns a chainable instance of the
* pipeline
Expand Down

0 comments on commit 6a110e3

Please sign in to comment.