From 855bec2a5741e8d9b284d51d9a36eb858d604e59 Mon Sep 17 00:00:00 2001 From: "Tomwang.eth" Date: Mon, 30 Nov 2026 11:14:23 +0000 Subject: [PATCH] fix(node): make tags.list() values plain serializable objects (#4915) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #4849 (same defect class on `branches.list()`, fixed in #4914). **Problem** `Tags.list()` returns napi class instances whose fields live on the prototype, so `console.log`, spread and `JSON.stringify` show `{}` — the documented example (`{ "v1": { version: 1, manifestSize: ... } }`) doesn't match what users actually see. Property access (`tags["v1"].version`) works, which is why the existing tests didn't catch it. **Change** - `TagContents` stays a runtime napi class export: existing ESM named imports of `TagContents` and `instanceof` checks keep working, and existing TypeScript values remain assignable to it. - `nodejs/lancedb/table.ts` post-processes `Tags.prototype.list()` results, promoting `version` and `manifestSize` to own enumerable properties on each returned instance by reusing the class getters and setters. Values are now enumerable, spreadable and JSON-serializable with no fields added, removed or renamed. - TS-only: the Python SDK already returns proper dicts with both fields. **Tests** - New regression test in `nodejs/__test__/table.test.ts` covers the named import and `instanceof` alongside own-enumerable keys, spread and a JSON round-trip through `tags.list()`. - The tag tests from #5145 are kept and pass against this change. - Verified locally on Linux x86_64: `tsc -b`, Biome checks, and the focused tags suite (4 passed), including `tags.create(name)` / `tags.create(name, null)` behavior. --- nodejs/__test__/table.test.ts | 29 +++++++++++++++++++++++ nodejs/lancedb/table.ts | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index f790bb6cc..00d20521b 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -19,6 +19,7 @@ import { Table, TableDelta, TableDeltaReader, + TagContents, VectorQuery, blob, connect, @@ -4144,6 +4145,34 @@ describe("when dealing with tags", () => { await tags.create("tag-checkout"); expect(await tags.getVersion("tag-checkout")).toBe(2); }); + + it("returns serializable TagContents instances from list()", async () => { + const conn = await connect(tmpDir.name, { + readConsistencyInterval: 0, + }); + const table = await conn.createTable("my_table", [ + { id: 1n, vector: [0.1, 0.2] }, + ]); + const tagsManager = await table.tags(); + await tagsManager.create("tag1", 1); + + const list = await tagsManager.list(); + const tag1 = list["tag1"]; + + // The runtime class export and instance identity are preserved. + expect(tag1).toBeInstanceOf(TagContents); + + // version and manifestSize are own enumerable properties, so spread + // and JSON serialization see them. + expect(Object.keys(tag1)).toEqual( + expect.arrayContaining(["version", "manifestSize"]), + ); + expect({ ...tag1 }.version).toBe(1); + + const serialized = JSON.parse(JSON.stringify(list)); + expect(serialized["tag1"].version).toBe(1); + expect(serialized["tag1"].manifestSize).toBeGreaterThan(0); + }); }); describe("when dealing with branches", () => { diff --git a/nodejs/lancedb/table.ts b/nodejs/lancedb/table.ts index 1bbcb4ac5..973cdde40 100644 --- a/nodejs/lancedb/table.ts +++ b/nodejs/lancedb/table.ts @@ -82,11 +82,54 @@ import { RefreshColumnResult, RefreshMaterializedViewResult, TableStatistics, + TagContents, Tags, UpdateFieldMetadataResult, UpdateResult, Table as _NativeTable, } from "./native"; + +/** + * Copies accessor properties from a napi-rs class prototype onto an instance + * as own enumerable properties, reusing the same getters and setters. napi-rs + * exposes class fields as prototype accessors, which are invisible to + * `Object.keys`, object spread and `JSON.stringify`; promoting them to own + * properties keeps the class (and `instanceof`) intact while making returned + * values serializable. + */ +function exposeOwnAccessors(instance: T, keys: string[]): T { + const proto = Object.getPrototypeOf(instance); + for (const key of keys) { + if (Object.prototype.hasOwnProperty.call(instance, key)) continue; + const desc = Object.getOwnPropertyDescriptor(proto, key); + if ( + desc !== undefined && + (desc.get !== undefined || desc.set !== undefined) + ) { + Object.defineProperty(instance, key, { + get: desc.get, + set: desc.set, + enumerable: true, + configurable: true, + }); + } + } + return instance; +} + +// `Tags` is a native napi-rs class returned directly from `Table.tags()`. +// Post-process `list()` results so each `TagContents` instance carries +// `version` and `manifestSize` as own enumerable properties. +const nativeTagsList = Tags.prototype.list; +Tags.prototype.list = async function ( + this: Tags, +): Promise> { + const tags = await nativeTagsList.call(this); + for (const contents of Object.values(tags)) { + exposeOwnAccessors(contents, ["version", "manifestSize"]); + } + return tags; +}; import { AutoQuery, FullTextQuery,