From 33cc9b682fd071afed385f9ad6cfb7bca810c810 Mon Sep 17 00:00:00 2001 From: Cory Grinstead Date: Fri, 21 Jun 2024 12:17:39 -0500 Subject: [PATCH] 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 --- nodejs/__test__/connection.test.ts | 12 ++++++++++++ nodejs/lancedb/connection.ts | 28 +++++++++++++++++++++++++--- nodejs/lancedb/remote/connection.ts | 19 ++++++++++++++----- nodejs/package-lock.json | 26 -------------------------- nodejs/package.json | 4 ++-- 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/nodejs/__test__/connection.test.ts b/nodejs/__test__/connection.test.ts index 5e05f103..616c48b3 100644 --- a/nodejs/__test__/connection.test.ts +++ b/nodejs/__test__/connection.test.ts @@ -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 }]); diff --git a/nodejs/lancedb/connection.ts b/nodejs/lancedb/connection.ts index 4269282d..4e3aa699 100644 --- a/nodejs/lancedb/connection.ts +++ b/nodejs/lancedb/connection.ts @@ -151,6 +151,19 @@ export abstract class Connection { options?: Partial, ): Promise; + /** + * 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, + ): Promise
; /** * 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[] | ArrowTable, + nameOrOptions: + | string + | ({ name: string; data: Data } & Partial), + data?: Record[] | ArrowTable, options?: Partial, ): Promise
{ + 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), diff --git a/nodejs/lancedb/remote/connection.ts b/nodejs/lancedb/remote/connection.ts index ef6e5808..59d8d61a 100644 --- a/nodejs/lancedb/remote/connection.ts +++ b/nodejs/lancedb/remote/connection.ts @@ -106,10 +106,19 @@ export class RemoteConnection extends Connection { } async createTable( - tableName: string, - data: Data, + nameOrOptions: + | string + | ({ name: string; data: Data } & Partial), + data?: Data, options?: Partial | undefined, ): Promise
{ + 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( diff --git a/nodejs/package-lock.json b/nodejs/package-lock.json index d621d4cc..b33a1b3c 100644 --- a/nodejs/package-lock.json +++ b/nodejs/package-lock.json @@ -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", diff --git a/nodejs/package.json b/nodejs/package.json index 4e2255a9..4256daee 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -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",