mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(node): make tags.list() values plain serializable objects (#4915)
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.
This commit is contained in:
@@ -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", () => {
|
||||
|
||||
@@ -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<T extends object>(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<Record<string, TagContents>> {
|
||||
const tags = await nativeTagsList.call(this);
|
||||
for (const contents of Object.values(tags)) {
|
||||
exposeOwnAccessors(contents, ["version", "manifestSize"]);
|
||||
}
|
||||
return tags;
|
||||
};
|
||||
import {
|
||||
AutoQuery,
|
||||
FullTextQuery,
|
||||
|
||||
Reference in New Issue
Block a user