Skip to content

Commit

Permalink
Merge pull request #20 from upstash/dx-821-vector-add-data-as-a-metad…
Browse files Browse the repository at this point in the history
…ata-field-on-sdks-if-there-is-no

DX-821: Add data as metadata
  • Loading branch information
buggyhunter committed Apr 15, 2024
2 parents 0f9f881 + cd6ac92 commit 184aff1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/commands/client/upsert/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { UpsertCommand } from "@commands/index";
import { FetchCommand, UpsertCommand } from "@commands/index";
import { newHttpClient, resetIndexes } from "@utils/test-utils";

const client = newHttpClient();
Expand Down Expand Up @@ -87,4 +87,26 @@ describe("UPSERT", () => {

expect(throwable).toThrow();
});

test("should add data as metadata, when no metadata is provided", async () => {
const embeddingClient = newHttpClient(undefined, {
token: process.env.EMBEDDING_UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.EMBEDDING_UPSTASH_VECTOR_REST_URL!,
});
const resUpsert = await new UpsertCommand({
id: "hello-world",
data: "testing data",
}).exec(embeddingClient);

const resFetch = await new FetchCommand([
["hello-world"],
{
includeMetadata: true,
},
]).exec(embeddingClient);

expect(resFetch[0]?.metadata).toEqual({ data: "testing data" });

expect(resUpsert).toEqual("Success");
});
});
14 changes: 14 additions & 0 deletions src/commands/client/upsert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ export class UpsertCommand<TMetadata> extends Command<string> {
const hasData = payload.some((p) => "data" in p && p.data);
if (hasData) {
endpoint = "upsert-data";

for (const p of payload) {
if (!("metadata" in p) && "data" in p) {
p.metadata = {
data: p.data,
} as NoInfer<TMetadata & { data: string }>;
}
}
}
} else {
if ("data" in payload) {
endpoint = "upsert-data";

if (!("metadata" in payload)) {
payload.metadata = {
data: payload.data,
} as NoInfer<TMetadata & { data: string }>;
}
}
}

Expand Down

0 comments on commit 184aff1

Please sign in to comment.