Skip to content

Commit

Permalink
Merge pull request #26 from upstash/dx-887-vector-sdk-upsert-and-vect…
Browse files Browse the repository at this point in the history
…or-type-issue

DX-887: Fix Update Vector
  • Loading branch information
ogzhanolguncu committed May 7, 2024
2 parents 801c666 + 4331b44 commit 878e648
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/commands/client/upsert/index.test.ts
Expand Up @@ -73,12 +73,13 @@ describe("UPSERT", () => {
await new UpsertCommand([
{
id: "hello-world",
//@ts-expect-error Mixed usage of vector and data in the same upsert command is not allowed.
data: "Test1-2-3-4-5",
metadata: { upstash: "test" },
},
{
id: "hello-world",
//@ts-ignore

vector: [1, 2, 3, 4],
metadata: { upstash: "test" },
},
Expand Down
30 changes: 22 additions & 8 deletions src/commands/client/upsert/index.ts
@@ -1,23 +1,37 @@
import { Command } from "@commands/command";


type NoInfer<T> = T extends infer U ? U : never;

type VectorPayload<TMetadata> = {
type BasePayload = {
id: number | string;
vector: number[];
metadata?: NoInfer<TMetadata>;
};
type DataPayload<TMetadata> = {
id: number | string;

type ExtendedVectorPayload<TMetadata> = BasePayload & ({
metadata: NoInfer<TMetadata>;
vector?: number[];
data?: never;
} | {
metadata?: NoInfer<TMetadata>;
vector: number[];
data?: never;
});

type ExtendedDataPayload<TMetadata> = BasePayload & ({
metadata: NoInfer<TMetadata>;
data: string;
vector?: never;
} | {
metadata?: NoInfer<TMetadata>;
};
data: string;
vector?: never;
});

type PayloadArray<TMetadata> = VectorPayload<TMetadata>[] | DataPayload<TMetadata>[];
type Payload<TMetadata> = ExtendedDataPayload<TMetadata> | ExtendedVectorPayload<TMetadata> | ExtendedDataPayload<TMetadata>[] | ExtendedVectorPayload<TMetadata>[];

export class UpsertCommand<TMetadata> extends Command<string> {
constructor(
payload: VectorPayload<TMetadata> | DataPayload<TMetadata> | PayloadArray<TMetadata>
payload: Payload<TMetadata>,
) {
let endpoint: "upsert" | "upsert-data" = "upsert";

Expand Down

0 comments on commit 878e648

Please sign in to comment.