Skip to content

Commit

Permalink
Merge pull request #5 from upstash/vec-114-index-level-generic-support
Browse files Browse the repository at this point in the history
Implement index-level generic support
  • Loading branch information
joschan21 authored Feb 5, 2024
2 parents fba3e4c + 406527f commit bf0b424
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
7 changes: 6 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export type IndexConfig = {
/**
* Serverless vector client for upstash.
*/
export class Index extends core.Index {
export class Index<
TIndexMetadata extends Record<
string,
unknown
> = Record<string, unknown>
> extends core.Index<TIndexMetadata> {
/**
* Create a new vector client by providing the url and token
*
Expand Down
2 changes: 1 addition & 1 deletion src/commands/client/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type FetchCommandOptions = {
export type FetchResult<TMetadata = Record<string, unknown>> = Vector<TMetadata> | null;

export class FetchCommand<TMetadata> extends Command<FetchResult<TMetadata>[]> {
constructor([ids, opts]: [ids: number[] | string[], opts: FetchCommandOptions]) {
constructor([ids, opts]: [ids: number[] | string[], opts?: FetchCommandOptions]) {
super({ ids, ...opts }, "fetch");
}
}
8 changes: 4 additions & 4 deletions src/commands/client/upsert/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Command } from "@commands/command";

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

export class UpsertCommand extends Command<string> {
constructor(payload: UpsertCommandPayload | UpsertCommandPayload[]) {
export class UpsertCommand<TMetadata> extends Command<string> {
constructor(payload: UpsertCommandPayload<TMetadata> | UpsertCommandPayload<TMetadata>[]) {
super(payload, "upsert");
}
}
21 changes: 13 additions & 8 deletions src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type CommandArgs<TCommand extends new (_args: any) => any> =
/**
* Serverless vector client for upstash vector db.
*/
export class Index {
export class Index<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> {
protected client: Requester;

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

/**
* Upserts (Updates and Inserts) specific items into the index.
Expand All @@ -89,7 +90,9 @@ export class Index {
*
* @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
*/
upsert = (args: CommandArgs<typeof UpsertCommand>) => new UpsertCommand(args).exec(this.client);
upsert = <TMetadata extends TIndexMetadata = 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 @@ -111,8 +114,9 @@ export class Index {
*
* @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>(...args: CommandArgs<typeof FetchCommand>) =>
new FetchCommand<TMetadata>(args).exec(this.client);
fetch = <TMetadata extends TIndexMetadata = TIndexMetadata>(
...args: CommandArgs<typeof FetchCommand>
) => new FetchCommand<TMetadata>(args).exec(this.client);

/**
* It's used for wiping an entire index.
Expand Down Expand Up @@ -150,8 +154,9 @@ export class Index {
*
* @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>(args: CommandArgs<typeof RangeCommand>) =>
new RangeCommand<TMetadata>(args).exec(this.client);
range = <TMetadata extends TIndexMetadata = TIndexMetadata>(
args: CommandArgs<typeof RangeCommand>
) => new RangeCommand<TMetadata>(args).exec(this.client);

/**
* Retrieves info from the index.
Expand Down

0 comments on commit bf0b424

Please sign in to comment.