UNPKG

@zilliz/milvus2-sdk-node

Version:

Official Node.js SDK for [Milvus](https://github.com/milvus-io/milvus) vector database. Provides gRPC and HTTP clients for vector similarity search, metadata filtering, and full collection/index/user management.

151 lines • 7.41 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VolumeManager = void 0; const __1 = require("../"); class VolumeManager { constructor(configOrEndpoint, apiKey, fetchImpl) { this.create_volume = this.createVolume; this.list_volumes = this.listVolumes; this.delete_volume = this.deleteVolume; this.describe_volume = this.describeVolume; this.apply_volume = this.applyVolume; if (typeof configOrEndpoint === 'string') { this.cloudEndpoint = configOrEndpoint; this.apiKey = apiKey || ''; this.fetchImpl = fetchImpl || fetch; this.timeout = __1.DEFAULT_HTTP_TIMEOUT; } else { this.cloudEndpoint = configOrEndpoint.cloudEndpoint; this.apiKey = configOrEndpoint.apiKey; this.fetchImpl = configOrEndpoint.fetch || fetch; this.timeout = configOrEndpoint.timeout || __1.DEFAULT_HTTP_TIMEOUT; } } createVolume(data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { timeout } = data, params = __rest(data, ["timeout"]); return this.POST('/v2/volumes/create', params, Object.assign(Object.assign({}, options), { timeout: (_b = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : timeout) !== null && _b !== void 0 ? _b : this.timeout })); }); } listVolumes(data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { timeout } = data, params = __rest(data, ["timeout"]); return this.GET('/v2/volumes', params, Object.assign(Object.assign({}, options), { timeout: (_b = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : timeout) !== null && _b !== void 0 ? _b : this.timeout })); }); } deleteVolume(data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { timeout, volumeName } = data; return this.DELETE(`/v2/volumes/${encodeURIComponent(volumeName)}`, Object.assign(Object.assign({}, options), { timeout: (_b = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : timeout) !== null && _b !== void 0 ? _b : this.timeout })); }); } describeVolume(data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { timeout, volumeName } = data; return this.GET(`/v2/volumes/${encodeURIComponent(volumeName)}`, {}, Object.assign(Object.assign({}, options), { timeout: (_b = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : timeout) !== null && _b !== void 0 ? _b : this.timeout })); }); } applyVolume(data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { timeout } = data, params = __rest(data, ["timeout"]); return this.POST('/v2/volumes/apply', params, Object.assign(Object.assign({}, options), { timeout: (_b = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : timeout) !== null && _b !== void 0 ? _b : this.timeout })); }); } get headers() { return { Authorization: `Bearer ${this.apiKey}`, Accept: 'application/json', ContentType: 'application/json', }; } get baseURL() { return this.cloudEndpoint.replace(/\/+$/, ''); } handleResponse(response, url) { return __awaiter(this, void 0, void 0, function* () { if (!response.ok) { const errorText = yield response.text().catch(() => ''); throw new Error(`HTTP ${response.status} ${response.statusText}: ${url}${errorText ? ` - ${errorText}` : ''}`); } return response.json(); }); } request(method, path, data, options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { let id; const timeout = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : this.timeout; const abortController = (_b = options === null || options === void 0 ? void 0 : options.abortController) !== null && _b !== void 0 ? _b : new AbortController(); try { id = setTimeout(() => abortController.abort(), timeout); const url = this.buildUrl(path, method === 'GET' ? data : undefined); const init = { method, headers: this.headers, signal: abortController.signal, }; if (method === 'POST') { init.body = JSON.stringify(data || {}); } const response = yield this.fetchImpl(url, init); return this.handleResponse(response, url); } finally { if (id !== undefined) { clearTimeout(id); } } }); } buildUrl(path, params) { const url = `${this.baseURL}${path}`; if (!params || Object.keys(params).length === 0) { return url; } const searchParams = new URLSearchParams(); Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { searchParams.append(key, String(value)); } }); const query = searchParams.toString(); return query ? `${url}?${query}` : url; } GET(path, params, options) { return this.request('GET', path, params, options); } POST(path, data, options) { return this.request('POST', path, data, options); } DELETE(path, options) { return this.request('DELETE', path, undefined, options); } } exports.VolumeManager = VolumeManager; //# sourceMappingURL=VolumeManager.js.map