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

Add length to pipeline #656

Merged
merged 2 commits into from
Oct 17, 2023
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
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