Skip to content

Commit

Permalink
Add range command
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Jan 11, 2024
1 parent e20f705 commit 64e9e26
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
27 changes: 12 additions & 15 deletions src/commands/client/delete/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ describe("DELETE", () => {
expect(deletionResult).toBeTruthy();
});

test("deleting the same ids should throw", () => {
const throwable = async () => {
const initialVector = [6.6, 7.7];
const idsToUpsert = [randomID(), randomID(), randomID()];

const upsertPromises = idsToUpsert.map((id) =>
new UpsertCommand({ id, vector: initialVector }).exec(client)
);
await Promise.all(upsertPromises);

await new DeleteCommand({ ids: idsToUpsert }).exec(client);
//This should throw
await new DeleteCommand({ ids: idsToUpsert }).exec(client);
};
expect(throwable).toThrow();
test("deleting the same ids should throw", async () => {
const initialVector = [6.6, 7.7];
const idsToUpsert = [randomID(), randomID(), randomID()];

const upsertPromises = idsToUpsert.map((id) =>
new UpsertCommand({ id, vector: initialVector }).exec(client)
);
await Promise.all(upsertPromises);

await new DeleteCommand({ ids: idsToUpsert }).exec(client);
const res1 = await new DeleteCommand({ ids: idsToUpsert }).exec(client);
expect(res1).toBeNull();
});
});
7 changes: 1 addition & 6 deletions src/commands/client/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Command } from "../../index";
import { Vector } from "../types";

type Payload = {
ids: number[] | string[];
includeMetadata?: boolean;
includeVectors?: boolean;
};

type Vector<TMetadata> = {
id: string;
vector: number[];
metadata?: TMetadata;
};

type FetchReturnResponse<TMetadata> = Vector<TMetadata> | null;

export class FetchCommand<TMetadata> extends Command<FetchReturnResponse<TMetadata>[]> {
Expand Down
26 changes: 26 additions & 0 deletions src/commands/client/range/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { afterAll, describe, expect, test } from "bun:test";
import { RangeCommand } from ".";
import { newHttpClient, randomFloat, randomID, resetIndexes } from "../../../utils/test-utils";
import { UpsertCommand } from "../upsert";

const client = newHttpClient();

describe("RANGE", () => {
afterAll(async () => await resetIndexes());

test("should query records successfully", async () => {
const randomizedData = new Array(20)
.fill("")
.map(() => ({ id: randomID(), vector: [randomFloat(), randomFloat()] }));

const payloads = randomizedData.map((data) => new UpsertCommand(data).exec(client));
await Promise.all(payloads);

const res = await new RangeCommand({
cursor: 0,
limit: 5,
includeVectors: true,
}).exec(client);
expect(res.nextCursor).toBe("5");
});
});
20 changes: 20 additions & 0 deletions src/commands/client/range/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Command } from "../../index";
import { Vector } from "../types";

type Payload = {
cursor: number;
limit: number;
includeVectors?: boolean;
includeMetadata?: boolean;
};

type RangeReturnResponse<TMetadata> = {
nextCursor: string;
vectors: Vector<TMetadata>[];
};

export class RangeCommand<TResult> extends Command<RangeReturnResponse<TResult>> {
constructor(payload: Payload) {
super(payload, "range");
}
}
5 changes: 5 additions & 0 deletions src/commands/client/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Vector<TMetadata> = {
id: string;
vector: number[];
metadata?: TMetadata;
};
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UpstashError } from "../error";
import { Requester } from "../http";

const ENDPOINTS = ["upsert", "query", "delete", "fetch", "reset"] as const;
const ENDPOINTS = ["upsert", "query", "delete", "fetch", "reset", "range"] as const;

export type EndpointVariants = (typeof ENDPOINTS)[number];
/**
Expand Down
9 changes: 1 addition & 8 deletions src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ export class HttpClient implements Requester {

return body as UpstashResponse<TResult>;
}
if (isArray<TResult>(body)) {
return { result: body };
}
throw new UpstashError(`${body}, command was: ${JSON.stringify(req.body)}`);
return { result: body };
}
}

Expand All @@ -166,7 +163,3 @@ function isResultErrorTuple<T>(
): response is { result?: string; error?: string } {
return response && typeof response === "object" && ("result" in response || "error" in response);
}

function isArray<T>(response: RawUpstashResponse<T>): response is T {
return Array.isArray(response);
}
2 changes: 2 additions & 0 deletions src/vector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FetchCommand } from "./commands/client";
import { DeleteCommand } from "./commands/client/delete";
import { QueryCommand } from "./commands/client/query";
import { RangeCommand } from "./commands/client/range";
import { ResetCommand } from "./commands/client/reset";
import { UpsertCommand } from "./commands/client/upsert";
import { Requester } from "./http";
Expand Down Expand Up @@ -34,4 +35,5 @@ export class Index {
upsert = (args: CommandArgs<typeof UpsertCommand>) => new UpsertCommand(args).exec(this.client);
fetch = (args: CommandArgs<typeof FetchCommand>) => new FetchCommand(args).exec(this.client);
reset = () => new ResetCommand().exec(this.client);
range = (args: CommandArgs<typeof RangeCommand>) => new RangeCommand(args).exec(this.client);
}

0 comments on commit 64e9e26

Please sign in to comment.