Skip to content

Commit

Permalink
- using ?? instead of ||
Browse files Browse the repository at this point in the history
- add fetch options for GET/SET
- update reamde

Signed-off-by: ruiyi.jiang <[email protected]>
  • Loading branch information
shanghaikid committed Oct 31, 2023
1 parent 21b0fee commit 561c460
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The following table shows the recommended `@zilliz/milvus2-sdk-node` versions fo

- [Milvus](https://milvus.io/)
- [Zilliz Cloud](https://cloud.zilliz.com/signup)
- Node: v12+
- Node: v18+

## Installation

Expand Down
43 changes: 27 additions & 16 deletions milvus/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClientConfig } from './types';
import { HttpClientConfig, FetchOptions } from './types';
import { Collection, Vector } from './http';
import {
DEFAULT_DB,
Expand Down Expand Up @@ -67,12 +67,12 @@ export class HttpBaseClient {

// database
get database() {
return this.config.database || DEFAULT_DB;
return this.config.database ?? DEFAULT_DB;
}

// timeout
get timeout() {
return this.config.timeout || DEFAULT_HTTP_TIMEOUT;
return this.config.timeout ?? DEFAULT_HTTP_TIMEOUT;
}

// headers
Expand All @@ -86,47 +86,58 @@ export class HttpBaseClient {

// fetch
get fetch() {
return this.config.fetch || fetch;
return this.config.fetch ?? fetch;
}

// POST API
async POST<T>(url: string, data: Record<string, any> = {}): Promise<T> {
async POST<T>(
url: string,
data: Record<string, any> = {},
options?: FetchOptions
): Promise<T> {
try {
// timeout controller
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), this.timeout);
const timeout = options?.timeout ?? this.timeout;
const abortController = options?.abortController ?? new AbortController();
const id = setTimeout(() => abortController.abort(), timeout);

// assign database
if (data) {
data.dbName = data.dbName || this.database;
data.dbName = data.dbName ?? this.database;
}

const response = await this.fetch(`${this.baseURL}${url}`, {
method: 'post',
headers: this.headers,
body: JSON.stringify(data),
signal: controller.signal,
signal: abortController.signal,
});

clearTimeout(id);
return response.json() as T;
} catch (error) {
if (error.name === 'AbortError') {
console.warn('milvus http client: request was timeout');
console.warn(`post ${url} request was timeout`);
}
return Promise.reject(error);
}
}

// GET API
async GET<T>(url: string, params: Record<string, any> = {}): Promise<T> {
async GET<T>(
url: string,
params: Record<string, any> = {},
options?: FetchOptions
): Promise<T> {
try {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), this.timeout);
// timeout controller
const timeout = options?.timeout ?? this.timeout;
const abortController = options?.abortController ?? new AbortController();
const id = setTimeout(() => abortController.abort(), timeout);

// assign database
if (params) {
params.dbName = params.dbName || this.database;
params.dbName = params.dbName ?? this.database;
}

const queryParams = new URLSearchParams(params);
Expand All @@ -136,7 +147,7 @@ export class HttpBaseClient {
{
method: 'get',
headers: this.headers,
signal: controller.signal,
signal: abortController.signal,
}
);

Expand All @@ -145,7 +156,7 @@ export class HttpBaseClient {
return response.json() as T;
} catch (error) {
if (error.name === 'AbortError') {
console.warn('milvus http client: request was timeout');
console.warn(`milvus http client: request was timeout`);
}
return Promise.reject(error);
}
Expand Down
31 changes: 21 additions & 10 deletions milvus/http/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HttpCollectionDescribeResponse,
HttpBaseResponse,
HttpBaseReq,
FetchOptions,
} from '../types';
import {
DEFAULT_PRIMARY_KEY_FIELD,
Expand All @@ -17,10 +18,10 @@ import {
/**
* Collection is a mixin function that extends the functionality of a base class.
* It provides methods to interact with collections in a Milvus cluster.
*
*
* @param {Constructor<HttpBaseClient>} Base - The base class to be extended.
* @returns {class} - The extended class with additional methods for collection management.
*
*
* @method createCollection - Creates a new collection in Milvus.
* @method describeCollection - Retrieves the description of a specific collection.
* @method dropCollection - Deletes a specific collection from Milvus.
Expand All @@ -30,7 +31,8 @@ export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
// POST create collection
async createCollection(
data: HttpCollectionCreateReq
data: HttpCollectionCreateReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/collections/create`;

Expand All @@ -39,31 +41,40 @@ export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
data.primaryField = data.primaryField || DEFAULT_PRIMARY_KEY_FIELD;
data.vectorField = data.vectorField || DEFAULT_VECTOR_FIELD;

return await this.POST<HttpBaseResponse>(url, data);
return await this.POST<HttpBaseResponse>(url, data, options);
}

// GET describe collection
async describeCollection(
params: HttpBaseReq
params: HttpBaseReq,
options?: FetchOptions
): Promise<HttpCollectionDescribeResponse> {
const url = `/vector/collections/describe`;
return await this.GET<HttpCollectionDescribeResponse>(url, params);
return await this.GET<HttpCollectionDescribeResponse>(
url,
params,
options
);
}

// POST drop collection
async dropCollection(data: HttpBaseReq): Promise<HttpBaseResponse> {
async dropCollection(
data: HttpBaseReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/collections/drop`;

return await this.POST<HttpBaseResponse>(url, data);
return await this.POST<HttpBaseResponse>(url, data, options);
}

// GET list collections
async listCollections(
params: HttpCollectionListReq = {}
params: HttpCollectionListReq = {},
options?: FetchOptions
): Promise<HttpCollectionListResponse> {
const url = `/vector/collections`;

return await this.GET<HttpCollectionListResponse>(url, params);
return await this.GET<HttpCollectionListResponse>(url, params, options);
}
};
}
43 changes: 31 additions & 12 deletions milvus/http/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HttpVectorDeleteReq,
HttpVectorSearchResponse,
HttpBaseResponse,
FetchOptions,
} from '../types';

/**
Expand All @@ -29,39 +30,57 @@ import {
export function Vector<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
// GET get data
async get(params: HttpVectorGetReq): Promise<HttpBaseResponse> {
async get(
params: HttpVectorGetReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/get`;
return await this.GET<HttpBaseResponse>(url, params);
return await this.GET<HttpBaseResponse>(url, params, options);
}

// POST insert data
async insert(data: HttpVectorInsertReq): Promise<HttpVectorInsertResponse> {
async insert(
data: HttpVectorInsertReq,
options?: FetchOptions
): Promise<HttpVectorInsertResponse> {
const url = `/vector/insert`;
return await this.POST<HttpVectorInsertResponse>(url, data);
return await this.POST<HttpVectorInsertResponse>(url, data, options);
}

// POST insert data
async upsert(data: HttpVectorInsertReq): Promise<HttpVectorInsertResponse> {
async upsert(
data: HttpVectorInsertReq,
options?: FetchOptions
): Promise<HttpVectorInsertResponse> {
const url = `/vector/insert`;
return await this.POST<HttpVectorInsertResponse>(url, data);
return await this.POST<HttpVectorInsertResponse>(url, data, options);
}

// POST query data
async query(data: HttpVectorQueryReq): Promise<HttpVectorQueryResponse> {
async query(
data: HttpVectorQueryReq,
options?: FetchOptions
): Promise<HttpVectorQueryResponse> {
const url = `/vector/query`;
return await this.POST<HttpVectorQueryResponse>(url, data);
return await this.POST<HttpVectorQueryResponse>(url, data, options);
}

// POST search data
async search(data: HttpVectorSearchReq): Promise<HttpVectorSearchResponse> {
async search(
data: HttpVectorSearchReq,
options?: FetchOptions
): Promise<HttpVectorSearchResponse> {
const url = `/vector/search`;
return await this.POST<HttpVectorSearchResponse>(url, data);
return await this.POST<HttpVectorSearchResponse>(url, data, options);
}

// POST delete collection
async delete(data: HttpVectorDeleteReq): Promise<HttpBaseResponse> {
async delete(
data: HttpVectorDeleteReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/delete`;
return await this.POST<HttpBaseResponse>(url, data);
return await this.POST<HttpBaseResponse>(url, data, options);
}
};
}
4 changes: 4 additions & 0 deletions milvus/types/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ type Fetch = (input: any, init?: any) => Promise<any>;

// Class types
export type Constructor<T = {}> = new (...args: any[]) => T;
export type FetchOptions = {
abortController: AbortController;
timeout: number;
}

type HttpClientConfigBase = {
// database name
Expand Down

0 comments on commit 561c460

Please sign in to comment.