diff --git a/docs/src/js/classes/Connection.md b/docs/src/js/classes/Connection.md index f79eb5232..bb990ec62 100644 --- a/docs/src/js/classes/Connection.md +++ b/docs/src/js/classes/Connection.md @@ -437,6 +437,29 @@ Open a table in the database. *** +### renameTable() + +```ts +abstract renameTable( + oldName, + newName, + namespacePath?): Promise +``` + +#### Parameters + +* **oldName**: `string` + +* **newName**: `string` + +* **namespacePath?**: `string`[] + +#### Returns + +`Promise`<`void`> + +*** + ### tableNames() #### tableNames(options) diff --git a/nodejs/__test__/connection.test.ts b/nodejs/__test__/connection.test.ts index b7bafad7c..21cc1c4fd 100644 --- a/nodejs/__test__/connection.test.ts +++ b/nodejs/__test__/connection.test.ts @@ -81,6 +81,16 @@ describe("given a connection", () => { await db.createTable("test4", [{ id: 1 }, { id: 2 }]); }); + it("should expose renameTable and reject on OSS listing DB", async () => { + await db.createTable("old_name", [{ id: 1 }]); + + await expect(db.renameTable("old_name", "new_name")).rejects.toThrow( + "rename_table is not supported in LanceDB OSS", + ); + + await expect(db.tableNames()).resolves.toEqual(["old_name"]); + }); + it("should fail if creating table twice, unless overwrite is true", async () => { let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]); await expect(tbl.countRows()).resolves.toBe(2); diff --git a/nodejs/lancedb/connection.ts b/nodejs/lancedb/connection.ts index 45513f77c..a3a84c268 100644 --- a/nodejs/lancedb/connection.ts +++ b/nodejs/lancedb/connection.ts @@ -296,6 +296,12 @@ export abstract class Connection { */ abstract dropTable(name: string, namespacePath?: string[]): Promise; + abstract renameTable( + oldName: string, + newName: string, + namespacePath?: string[], + ): Promise; + /** * Drop all tables in the database. * @param {string[]} namespacePath The namespace path to drop tables from (defaults to root namespace). @@ -609,6 +615,14 @@ export class LocalConnection extends Connection { return this.inner.dropTable(name, namespacePath ?? []); } + async renameTable( + oldName: string, + newName: string, + namespacePath?: string[], + ): Promise { + return this.inner.renameTable(oldName, newName, namespacePath ?? []); + } + async dropAllTables(namespacePath?: string[]): Promise { return this.inner.dropAllTables(namespacePath ?? []); } diff --git a/nodejs/src/connection.rs b/nodejs/src/connection.rs index eb8bab22e..faf42699a 100644 --- a/nodejs/src/connection.rs +++ b/nodejs/src/connection.rs @@ -328,6 +328,20 @@ impl Connection { .default_error() } + #[napi(catch_unwind)] + pub async fn rename_table( + &self, + old_name: String, + new_name: String, + namespace_path: Option>, + ) -> napi::Result<()> { + let ns = namespace_path.unwrap_or_default(); + self.get_inner()? + .rename_table(&old_name, &new_name, &ns, &ns) + .await + .default_error() + } + #[napi(catch_unwind)] pub async fn drop_all_tables(&self, namespace_path: Option>) -> napi::Result<()> { let ns = namespace_path.unwrap_or_default();