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,