Skip to content

Commit

Permalink
feat: allow overriding index-level generics
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Feb 5, 2024
1 parent c9ef649 commit 44b0c75
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/commands/client/upsert/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Command } from "@commands/command";

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

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

export class UpsertCommand<TMetadata> extends Command<string> {
Expand Down
20 changes: 6 additions & 14 deletions src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import {
import { Requester } from "@http";
import { InfoCommand } from "./commands";

type ValidateShape<T, Base> = T extends Base
? Exclude<keyof T, keyof Base> extends never
? T
: ExtraProperties<Exclude<keyof T, keyof Base>>
: never;

type ExtraProperties<Keys extends PropertyKey> = Record<Keys, never>;

export type CommandArgs<TCommand extends new (_args: any) => any> =
ConstructorParameters<TCommand>[0];

Expand Down Expand Up @@ -72,7 +64,7 @@ export class Index<TIndexMetadata extends Record<string, unknown> = Record<strin
*
* @returns A promise that resolves with an array of query result objects when the request to query the index is completed.
*/
query = <TMetadata extends TIndexMetadata = TIndexMetadata>(
query = <TMetadata extends Record<string, unknown> = TIndexMetadata>(
args: CommandArgs<typeof QueryCommand>
) => new QueryCommand<TMetadata>(args).exec(this.client);

Expand All @@ -98,9 +90,9 @@ export class Index<TIndexMetadata extends Record<string, unknown> = Record<strin
*
* @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
*/
upsert = <TMetadata extends TIndexMetadata = TIndexMetadata>(
args: CommandArgs<typeof UpsertCommand<ValidateShape<TMetadata, TIndexMetadata>>>
) => new UpsertCommand<ValidateShape<TMetadata, TIndexMetadata>>(args).exec(this.client);
upsert = <TMetadata extends Record<string, unknown> = TIndexMetadata>(
args: CommandArgs<typeof UpsertCommand<TMetadata>>
) => new UpsertCommand<TMetadata>(args).exec(this.client);

/**
* It's used for retrieving specific items from the index, optionally including
Expand All @@ -122,7 +114,7 @@ export class Index<TIndexMetadata extends Record<string, unknown> = Record<strin
*
* @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
*/
fetch = <TMetadata extends TIndexMetadata = TIndexMetadata>(
fetch = <TMetadata extends Record<string, unknown> = TIndexMetadata>(
...args: CommandArgs<typeof FetchCommand>
) => new FetchCommand<TMetadata>(args).exec(this.client);

Expand Down Expand Up @@ -162,7 +154,7 @@ export class Index<TIndexMetadata extends Record<string, unknown> = Record<strin
*
* @returns {Promise<RangeReturnResponse<TMetadata>>} A promise that resolves with the response containing the next cursor and an array of vectors, after the command is executed.
*/
range = <TMetadata extends TIndexMetadata = TIndexMetadata>(
range = <TMetadata extends Record<string, unknown> = TIndexMetadata>(
args: CommandArgs<typeof RangeCommand>
) => new RangeCommand<TMetadata>(args).exec(this.client);

Expand Down

0 comments on commit 44b0c75

Please sign in to comment.