From 1d343edbd4621cf5e6a8f00cbdf456b617043f26 Mon Sep 17 00:00:00 2001 From: Lei Xu Date: Tue, 18 Jul 2023 16:56:47 -0700 Subject: [PATCH] [Node] implement remote db.TableNames (#334) --- node/src/remote/client.ts | 35 +++++++++++++++++++++++++++++++++-- node/src/remote/index.ts | 4 ++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/node/src/remote/client.ts b/node/src/remote/client.ts index a2e74ead..64b22a8d 100644 --- a/node/src/remote/client.ts +++ b/node/src/remote/client.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import axios from 'axios' +import axios, { type AxiosResponse } from 'axios' import { tableFromIPC, type Table as ArrowTable } from 'apache-arrow' @@ -60,10 +60,41 @@ export class HttpLancedbClient { }) if (response.status !== 200) { const errorData = new TextDecoder().decode(response.data) - throw new Error(`Server Error, status: ${response.status as number}, message: ${response.statusText as string}: ${errorData}`) + throw new Error( + `Server Error, status: ${response.status as number}, ` + + `message: ${response.statusText as string}: ${errorData}` + ) } const table = tableFromIPC(response.data) return table } + + /** + * Sent GET request. + */ + public async get (path: string, params?: Record): Promise { + const response = await axios.get( + `${this._url}${path}`, + { + headers: { + 'Content-Type': 'application/json', + 'x-api-key': this._apiKey + }, + params, + timeout: 10000 + } + ).catch((err) => { + console.error('error: ', err) + return err.response + }) + if (response.status !== 200) { + const errorData = new TextDecoder().decode(response.data) + throw new Error( + `Server Error, status: ${response.status as number}, ` + + `message: ${response.statusText as string}: ${errorData}` + ) + } + return response + } } diff --git a/node/src/remote/index.ts b/node/src/remote/index.ts index 92ae066a..4c1a81a4 100644 --- a/node/src/remote/index.ts +++ b/node/src/remote/index.ts @@ -47,7 +47,8 @@ export class RemoteConnection implements Connection { } async tableNames (): Promise { - throw new Error('Not implemented') + const response = await this._client.get('/v1/table/') + return response.data.tables } async openTable (name: string): Promise @@ -83,7 +84,6 @@ export class RemoteQuery extends Query { // TODO: refactor this to a base class + queryImpl pattern async execute>(): Promise { - // TODO: remove as any hack once we refactor const embeddings = this._embeddings const query = (this as any)._query let queryVector: number[]