From b3e5ac6d2af63e53d742293f845b05928f06f8ff Mon Sep 17 00:00:00 2001 From: Cory Grinstead Date: Fri, 21 Jun 2024 11:38:26 -0500 Subject: [PATCH] feat(nodejs): feature parity [2/N] - add `table.name` and `lancedb.connect({args})` (#1380) depends on https://github.com/lancedb/lancedb/pull/1378 see proper diff here https://github.com/universalmind303/lancedb/compare/remote-table-node...universalmind303:lancedb:table-name --- nodejs/lancedb/connection.ts | 2 +- nodejs/lancedb/index.ts | 55 +++++++++++++++++++++++++++++----- nodejs/lancedb/remote/table.ts | 4 +++ nodejs/lancedb/table.ts | 6 +++- nodejs/src/connection.rs | 6 ---- nodejs/src/lib.rs | 2 -- nodejs/src/table.rs | 2 +- 7 files changed, 59 insertions(+), 18 deletions(-) diff --git a/nodejs/lancedb/connection.ts b/nodejs/lancedb/connection.ts index f38b8ce3..4269282d 100644 --- a/nodejs/lancedb/connection.ts +++ b/nodejs/lancedb/connection.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Table as ArrowTable, Schema } from "./arrow"; +import { Table as ArrowTable, Data, Schema } from "./arrow"; import { fromTableToBuffer, makeEmptyTable } from "./arrow"; import { EmbeddingFunctionConfig, getRegistry } from "./embedding/registry"; import { Connection as LanceDbConnection } from "./native"; diff --git a/nodejs/lancedb/index.ts b/nodejs/lancedb/index.ts index 3be59718..3c037b72 100644 --- a/nodejs/lancedb/index.ts +++ b/nodejs/lancedb/index.ts @@ -56,12 +56,7 @@ export { export { Index, IndexOptions, IvfPqOptions } from "./indices"; -export { - Table, - AddDataOptions, - IndexConfig, - UpdateOptions, -} from "./table"; +export { Table, AddDataOptions, IndexConfig, UpdateOptions } from "./table"; export * as embedding from "./embedding"; @@ -76,15 +71,61 @@ export * as embedding from "./embedding"; * @param {string} uri - The uri of the database. If the database uri starts * with `db://` then it connects to a remote database. * @see {@link ConnectionOptions} for more details on the URI format. + * @example + * ```ts + * const conn = await connect("/path/to/database"); + * ``` + * @example + * ```ts + * const conn = await connect( + * "s3://bucket/path/to/database", + * {storageOptions: {timeout: "60s"} + * }); + * ``` */ export async function connect( uri: string, opts?: Partial, +): Promise; +/** + * Connect to a LanceDB instance at the given URI. + * + * Accepted formats: + * + * - `/path/to/database` - local database + * - `s3://bucket/path/to/database` or `gs://bucket/path/to/database` - database on cloud storage + * - `db://host:port` - remote database (LanceDB cloud) + * @param options - The options to use when connecting to the database + * @see {@link ConnectionOptions} for more details on the URI format. + * @example + * ```ts + * const conn = await connect({ + * uri: "/path/to/database", + * storageOptions: {timeout: "60s"} + * }); + * ``` + */ +export async function connect( + opts: Partial & { uri: string }, +): Promise; +export async function connect( + uriOrOptions: + | string + | (Partial & { uri: string }), + opts: Partial = {}, ): Promise { + let uri: string | undefined; + if (typeof uriOrOptions !== "string") { + const { uri: uri_, ...options } = uriOrOptions; + uri = uri_; + opts = options; + } else { + uri = uriOrOptions; + } + if (!uri) { throw new Error("uri is required"); } - opts = opts ?? {}; if (uri?.startsWith("db://")) { return new RemoteConnection(uri, opts as RemoteConnectionOptions); diff --git a/nodejs/lancedb/remote/table.ts b/nodejs/lancedb/remote/table.ts index a9760adf..224bffcc 100644 --- a/nodejs/lancedb/remote/table.ts +++ b/nodejs/lancedb/remote/table.ts @@ -34,6 +34,10 @@ export class RemoteTable extends Table { return `/v1/table/${encodeURIComponent(this.#name)}/`; } + get name(): string { + return this.#name; + } + public constructor( client: RestfulLanceDBClient, tableName: string, diff --git a/nodejs/lancedb/table.ts b/nodejs/lancedb/table.ts index ceb805b3..b85e719e 100644 --- a/nodejs/lancedb/table.ts +++ b/nodejs/lancedb/table.ts @@ -98,6 +98,8 @@ export abstract class Table { [Symbol.for("nodejs.util.inspect.custom")](): string { return this.display(); } + /** Returns the name of the table */ + abstract get name(): string; /** Return true if the table has not been closed */ abstract isOpen(): boolean; @@ -412,7 +414,9 @@ export class LocalTable extends Table { super(); this.inner = inner; } - + get name(): string { + return this.inner.name; + } isOpen(): boolean { return this.inner.isOpen(); } diff --git a/nodejs/src/connection.rs b/nodejs/src/connection.rs index 88300184..56de5783 100644 --- a/nodejs/src/connection.rs +++ b/nodejs/src/connection.rs @@ -56,12 +56,6 @@ impl Connection { #[napi(factory)] pub async fn new(uri: String, options: ConnectionOptions) -> napi::Result { let mut builder = ConnectBuilder::new(&uri); - if let Some(api_key) = options.api_key { - builder = builder.api_key(&api_key); - } - if let Some(host_override) = options.host_override { - builder = builder.host_override(&host_override); - } if let Some(interval) = options.read_consistency_interval { builder = builder.read_consistency_interval(std::time::Duration::from_secs_f64(interval)); diff --git a/nodejs/src/lib.rs b/nodejs/src/lib.rs index fda89660..1c15ff91 100644 --- a/nodejs/src/lib.rs +++ b/nodejs/src/lib.rs @@ -28,8 +28,6 @@ mod util; #[napi(object)] #[derive(Debug)] pub struct ConnectionOptions { - pub api_key: Option, - pub host_override: Option, /// (For LanceDB OSS only): The interval, in seconds, at which to check for /// updates to the table from other processes. If None, then consistency is not /// checked. For performance reasons, this is the default. For strong diff --git a/nodejs/src/table.rs b/nodejs/src/table.rs index 084cb576..b2b0039e 100644 --- a/nodejs/src/table.rs +++ b/nodejs/src/table.rs @@ -30,7 +30,7 @@ use crate::query::{Query, VectorQuery}; pub struct Table { // We keep a duplicate of the table name so we can use it for error // messages even if the table has been closed - name: String, + pub name: String, pub(crate) inner: Option, }