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

@@ -57,6 +57,18 @@ describe("given a connection", () => {
expect(db.isOpen()).toBe(false);
await expect(db.tableNames()).rejects.toThrow("Connection is closed");
});
it("should be able to create a table from an object arg `createTable(options)`, or args `createTable(name, data, options)`", async () => {
let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]);
await expect(tbl.countRows()).resolves.toBe(2);
tbl = await db.createTable({
name: "test",
data: [{ id: 3 }],
mode: "overwrite",
});
await expect(tbl.countRows()).resolves.toBe(1);
});
it("should fail if creating table twice, unless overwrite is true", async () => {
let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]);

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(

View File

@@ -21,7 +21,6 @@
"@types/axios": "^0.14.0",
"apache-arrow": "^15.0.0",
"axios": "^1.7.2",
"memoize": "^10.0.0",
"openai": "^4.29.2",
"reflect-metadata": "^0.2.2"
},
@@ -5942,20 +5941,6 @@
"is-buffer": "~1.1.6"
}
},
"node_modules/memoize": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/memoize/-/memoize-10.0.0.tgz",
"integrity": "sha512-H6cBLgsi6vMWOcCpvVCdFFnl3kerEXbrYh9q+lY6VXvQSmM6CkmV08VOwT+WE2tzIEqRPFfAq3fm4v/UIW6mSA==",
"dependencies": {
"mimic-function": "^5.0.0"
},
"engines": {
"node": ">=18"
},
"funding": {
"url": "https://github.com/sindresorhus/memoize?sponsor=1"
}
},
"node_modules/merge-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@@ -6003,17 +5988,6 @@
"node": ">= 0.6"
}
},
"node_modules/mimic-function": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/mimic-function/-/mimic-function-5.0.1.tgz",
"integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==",
"engines": {
"node": ">=18"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimatch": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",

View File

@@ -38,7 +38,8 @@
"typedoc": "^0.25.7",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^5.3.3",
"typescript-eslint": "^7.1.0"
"typescript-eslint": "^7.1.0",
"@types/axios": "^0.14.0"
},
"ava": {
"timeout": "3m"
@@ -65,7 +66,6 @@
"version": "napi version"
},
"dependencies": {
"@types/axios": "^0.14.0",
"apache-arrow": "^15.0.0",
"axios": "^1.7.2",
"openai": "^4.29.2",