feat(nodejs): feature parity [3/N] - createTable({name, data, ...options}) (#1386)

adds support for the `vectordb` syntax of `createTable({name, data,
...options})`.


depends on https://github.com/lancedb/lancedb/pull/1380
see actual diff here
https://github.com/universalmind303/lancedb/compare/table-name...universalmind303:create-table-args
This commit is contained in:
Cory Grinstead
2024-06-21 12:17:39 -05:00
committed by GitHub
parent b3e5ac6d2a
commit 33cc9b682f
5 changed files with 53 additions and 36 deletions

View File

@@ -151,6 +151,19 @@ export abstract class Connection {
options?: Partial<OpenTableOptions>,
): Promise<Table>;
/**
* Creates a new Table and initialize it with new data.
* @param {object} options - The options object.
* @param {string} options.name - The name of the table.
* @param {Data} options.data - Non-empty Array of Records to be inserted into the table
*
*/
abstract createTable(
options: {
name: string;
data: Data;
} & Partial<CreateTableOptions>,
): Promise<Table>;
/**
* Creates a new Table and initialize it with new data.
* @param {string} name - The name of the table.
@@ -219,13 +232,22 @@ export class LocalConnection extends Connection {
}
async createTable(
name: string,
data: Record<string, unknown>[] | ArrowTable,
nameOrOptions:
| string
| ({ name: string; data: Data } & Partial<CreateTableOptions>),
data?: Record<string, unknown>[] | ArrowTable,
options?: Partial<CreateTableOptions>,
): Promise<Table> {
if (typeof nameOrOptions !== "string" && "name" in nameOrOptions) {
const { name, data, ...options } = nameOrOptions;
return this.createTable(name, data, options);
}
if (data === undefined) {
throw new Error("data is required");
}
const { buf, mode } = await Table.parseTableData(data, options);
const innerTable = await this.inner.createTable(
name,
nameOrOptions,
buf,
mode,
cleanseStorageOptions(options?.storageOptions),

View File

@@ -106,10 +106,19 @@ export class RemoteConnection extends Connection {
}
async createTable(
tableName: string,
data: Data,
nameOrOptions:
| string
| ({ name: string; data: Data } & Partial<CreateTableOptions>),
data?: Data,
options?: Partial<CreateTableOptions> | undefined,
): Promise<Table> {
if (typeof nameOrOptions !== "string" && "name" in nameOrOptions) {
const { name, data, ...options } = nameOrOptions;
return this.createTable(name, data, options);
}
if (data === undefined) {
throw new Error("data is required");
}
if (options?.mode) {
console.warn(
"option 'mode' is not supported in LanceDB Cloud",
@@ -132,7 +141,7 @@ export class RemoteConnection extends Connection {
);
await this.#client.post(
`/v1/table/${encodeURIComponent(tableName)}/create/`,
`/v1/table/${encodeURIComponent(nameOrOptions)}/create/`,
buf,
{
config: {
@@ -141,8 +150,8 @@ export class RemoteConnection extends Connection {
headers: { "Content-Type": "application/vnd.apache.arrow.stream" },
},
);
this.#tableCache.set(tableName, true);
return new RemoteTable(this.#client, tableName, this.#dbName);
this.#tableCache.set(nameOrOptions, true);
return new RemoteTable(this.#client, nameOrOptions, this.#dbName);
}
async createEmptyTable(