mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix: expose branch create_at and metadata in branches.list() for node and python (#4914)
Fixes #4849. **Problem** - TypeScript SDK: `Branches.list()` returned napi class instances whose fields live on the prototype, so `console.log`, spread and `JSON.stringify` showed `{}` (the exact symptom in the docs example). `create_at` and `metadata`, which Lance already stores for every branch, were not exposed at all. - Python SDK: the `Branches.list()` dicts also dropped `create_at` and `metadata`. **Change** - `nodejs/src/table.rs`: `BranchContents` stays a runtime napi class export and gains `create_at` (Unix seconds) and `metadata`, populated from the stored Lance branch contents. The `metadata` getter defines every key as an own enumerable data property (`napi_define_properties`), so keys like `__proto__` that ordinary map conversion would route to the prototype setter are preserved. - `nodejs/lancedb/table.ts`: the `Branches.list()` wrapper promotes all fields (`parentBranch`, `parentVersion`, `createAt`, `manifestSize`, `metadata`) to own enumerable properties on each returned instance by reusing the class getters and setters, so spread and `JSON.stringify` see them while `instanceof` keeps working. Its return type is `ListedBranchContents`, which guarantees `createAt` and `metadata`. - Public typing: the exported `BranchContents` type keeps its released shape (`parentBranch`/`parentVersion`/`manifestSize` only), so values and interfaces written against it stay assignable and extendable, including a view whose `createAt` is a `Date` and an interface that extends `BranchContents` with its own `metadata`. `ListedBranchContents extends BranchContents` adds `createAt` and `metadata` as required fields, and the exported `BranchContents` constructor value has its `prototype` typed as `BranchContents`, so the class value and `instanceof` checks are unchanged. - `python/src/table.rs`: `Branches.list()` dicts now include `create_at` and `metadata`. - Docs: regenerated TypeScript API reference for the new shape, and the branching guide now documents `createAt`/`create_at` (Unix seconds) and `metadata`. **Tests** - `nodejs/__test__/table.test.ts`: a branch whose stored ref file carries non-empty metadata (including a `__proto__` key) and a known creation time in Unix seconds is listed with both, as a `BranchContents` instance with own enumerable fields, surviving spread and a JSON round-trip with every metadata key intact. A second test covers the consumer type contracts with `tsc`: a released-shape value with an extra `createAt: Date` stays assignable to `BranchContents`, an interface extending `BranchContents` can declare its own `metadata` shape, the exported constructor's `prototype` is typed as `BranchContents`, and `branches.list()` results guarantee `createAt` and `metadata`. - `python/python/tests/test_table.py::test_branches`: same non-empty-metadata (with `__proto__`) and known-`create_at` coverage. - Verified locally on Linux x86_64: `tsc -b`, Biome checks, regenerated docs diff limited to the `BranchContents` pages, node branches suite (34 passed), python `test_branches` (4 passed, 1 skipped). --------- Co-authored-by: tomwangcn <tomwangcn@users.noreply.github.com>
This commit is contained in:
@@ -2746,7 +2746,87 @@ pub struct TagContents {
|
||||
pub struct BranchContents {
|
||||
pub parent_branch: Option<String>,
|
||||
pub parent_version: i64,
|
||||
/// Branch creation time in Unix seconds.
|
||||
pub create_at: i64,
|
||||
pub manifest_size: i64,
|
||||
metadata: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
impl BranchContents {
|
||||
/// Application-defined string metadata stored with the branch.
|
||||
///
|
||||
/// Every key is preserved, including keys like `__proto__` that ordinary
|
||||
/// object assignment would route to the prototype setter.
|
||||
#[napi(getter, ts_return_type = "Record<string, string>")]
|
||||
pub fn metadata(&self, env: Env) -> napi::Result<Object<'static>> {
|
||||
let raw_env = env.raw();
|
||||
let mut raw_obj = std::ptr::null_mut();
|
||||
unsafe {
|
||||
napi::check_status!(
|
||||
napi::sys::napi_create_object(raw_env, &mut raw_obj),
|
||||
"Failed to create metadata object"
|
||||
)?;
|
||||
}
|
||||
for (key, value) in &self.metadata {
|
||||
define_own_data_property(raw_env, raw_obj, key, value)?;
|
||||
}
|
||||
Ok(Object::from_raw(raw_env, raw_obj))
|
||||
}
|
||||
|
||||
#[napi(setter)]
|
||||
pub fn set_metadata(&mut self, metadata: HashMap<String, String>) {
|
||||
self.metadata = metadata;
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines `key` as an own enumerable data property holding `value`.
|
||||
///
|
||||
/// napi-rs converts maps with `napi_set_property`, whose `[[Set]]` semantics
|
||||
/// send a `__proto__` key to the prototype setter and drop it from the
|
||||
/// object's own keys. `napi_define_properties` uses `[[DefineOwnProperty]]`,
|
||||
/// which keeps every key.
|
||||
fn define_own_data_property(
|
||||
raw_env: napi::sys::napi_env,
|
||||
raw_obj: napi::sys::napi_value,
|
||||
key: &str,
|
||||
value: &str,
|
||||
) -> napi::Result<()> {
|
||||
use napi::sys;
|
||||
use std::ffi::CString;
|
||||
use std::ptr;
|
||||
|
||||
let key_c =
|
||||
CString::new(key).map_err(|e| napi::Error::new(napi::Status::InvalidArg, e.to_string()))?;
|
||||
unsafe {
|
||||
let mut raw_value = ptr::null_mut();
|
||||
napi::check_status!(
|
||||
sys::napi_create_string_utf8(
|
||||
raw_env,
|
||||
value.as_ptr().cast(),
|
||||
value.len() as isize,
|
||||
&mut raw_value
|
||||
),
|
||||
"Failed to create metadata value"
|
||||
)?;
|
||||
let descriptor = sys::napi_property_descriptor {
|
||||
utf8name: key_c.as_ptr(),
|
||||
name: ptr::null_mut(),
|
||||
method: None,
|
||||
getter: None,
|
||||
setter: None,
|
||||
value: raw_value,
|
||||
attributes: sys::PropertyAttributes::enumerable
|
||||
| sys::PropertyAttributes::writable
|
||||
| sys::PropertyAttributes::configurable,
|
||||
data: ptr::null_mut(),
|
||||
};
|
||||
napi::check_status!(
|
||||
sys::napi_define_properties(raw_env, raw_obj, 1, &descriptor),
|
||||
"Failed to define metadata property"
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn from_tag_hint(name: &str, tag: &str) -> String {
|
||||
@@ -2922,7 +3002,9 @@ impl Branches {
|
||||
BranchContents {
|
||||
parent_branch: v.parent_branch,
|
||||
parent_version: v.parent_version as i64,
|
||||
create_at: v.create_at as i64,
|
||||
manifest_size: v.manifest_size as i64,
|
||||
metadata: v.metadata,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user