File size: 2,865 Bytes
96c8713 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import { DEFAULT_HEADERS } from '../lib/constants'
import { StorageError } from '../lib/common/errors'
import { Fetch, vectorsApi } from '../lib/common/fetch'
import BaseApiClient from '../lib/common/BaseApiClient'
import {
ApiResponse,
VectorIndex,
ListIndexesOptions,
ListIndexesResponse,
VectorDataType,
DistanceMetric,
MetadataConfiguration,
} from '../lib/types'
/**
* @alpha
*
* Options for creating a vector index
*
* **Public alpha:** This API is part of a public alpha release and may not be available to your account type.
*/
export interface CreateIndexOptions {
vectorBucketName: string
indexName: string
dataType: VectorDataType
dimension: number
distanceMetric: DistanceMetric
metadataConfiguration?: MetadataConfiguration
}
/**
* @hidden
* Base implementation for vector index operations.
* Use {@link VectorBucketScope} via `supabase.storage.vectors.from('bucket')` instead.
*/
export default class VectorIndexApi extends BaseApiClient<StorageError> {
/** Creates a new VectorIndexApi instance */
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
const finalUrl = url.replace(/\/$/, '')
const finalHeaders = { ...DEFAULT_HEADERS, 'Content-Type': 'application/json', ...headers }
super(finalUrl, finalHeaders, fetch, 'vectors')
}
/** Creates a new vector index within a bucket */
async createIndex(options: CreateIndexOptions): Promise<ApiResponse<undefined>> {
return this.handleOperation(async () => {
const data = await vectorsApi.post(this.fetch, `${this.url}/CreateIndex`, options, {
headers: this.headers,
})
return data || {}
})
}
/** Retrieves metadata for a specific vector index */
async getIndex(
vectorBucketName: string,
indexName: string
): Promise<ApiResponse<{ index: VectorIndex }>> {
return this.handleOperation(async () => {
return await vectorsApi.post(
this.fetch,
`${this.url}/GetIndex`,
{ vectorBucketName, indexName },
{ headers: this.headers }
)
})
}
/** Lists vector indexes within a bucket with optional filtering and pagination */
async listIndexes(options: ListIndexesOptions): Promise<ApiResponse<ListIndexesResponse>> {
return this.handleOperation(async () => {
return await vectorsApi.post(this.fetch, `${this.url}/ListIndexes`, options, {
headers: this.headers,
})
})
}
/** Deletes a vector index and all its data */
async deleteIndex(vectorBucketName: string, indexName: string): Promise<ApiResponse<undefined>> {
return this.handleOperation(async () => {
const data = await vectorsApi.post(
this.fetch,
`${this.url}/DeleteIndex`,
{ vectorBucketName, indexName },
{ headers: this.headers }
)
return data || {}
})
}
}
|