diff --git a/src/commands/client/upsert/index.test.ts b/src/commands/client/upsert/index.test.ts index 1b9862c..b77bc19 100644 --- a/src/commands/client/upsert/index.test.ts +++ b/src/commands/client/upsert/index.test.ts @@ -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(); @@ -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"); + }); }); diff --git a/src/commands/client/upsert/index.ts b/src/commands/client/upsert/index.ts index 9e99577..c3b7f30 100644 --- a/src/commands/client/upsert/index.ts +++ b/src/commands/client/upsert/index.ts @@ -25,10 +25,24 @@ export class UpsertCommand extends Command { 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; + } + } } } else { if ("data" in payload) { endpoint = "upsert-data"; + + if (!("metadata" in payload)) { + payload.metadata = { + data: payload.data, + } as NoInfer; + } } }