mirror of
https://github.com/lancedb/lancedb.git
synced 2026-01-07 12:22:59 +00:00
This is the start of a more comprehensive refactor and stabilization of the Rust API. The `Connection` trait is cleaned up to not require `lance` and to match the `Connection` trait in other APIs. In addition, the concrete implementation `Database` is hidden. BREAKING CHANGE: The struct `crate::connection::Database` is now gone. Several examples opened a connection using `Database::connect` or `Database::connect_with_params`. Users should now use `vectordb::connect`. BREAKING CHANGE: The `connect`, `create_table`, and `open_table` methods now all return a builder object. This means that a call like `conn.open_table(..., opt1, opt2)` will now become `conn.open_table(...).opt1(opt1).opt2(opt2).execute()` In addition, the structure of options has changed slightly. However, no options capability has been removed. --------- Co-authored-by: Will Jones <willjones127@gmail.com>
87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
// Copyright 2024 Lance Developers.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
use napi::bindgen_prelude::*;
|
|
use napi_derive::*;
|
|
|
|
use crate::table::Table;
|
|
use vectordb::connection::Connection as LanceDBConnection;
|
|
use vectordb::ipc::ipc_file_to_batches;
|
|
|
|
#[napi]
|
|
pub struct Connection {
|
|
conn: LanceDBConnection,
|
|
}
|
|
|
|
#[napi]
|
|
impl Connection {
|
|
/// Create a new Connection instance from the given URI.
|
|
#[napi(factory)]
|
|
pub async fn new(uri: String) -> napi::Result<Self> {
|
|
Ok(Self {
|
|
conn: vectordb::connect(&uri).execute().await.map_err(|e| {
|
|
napi::Error::from_reason(format!("Failed to connect to database: {}", e))
|
|
})?,
|
|
})
|
|
}
|
|
|
|
/// List all tables in the dataset.
|
|
#[napi]
|
|
pub async fn table_names(&self) -> napi::Result<Vec<String>> {
|
|
self.conn
|
|
.table_names()
|
|
.await
|
|
.map_err(|e| napi::Error::from_reason(format!("{}", e)))
|
|
}
|
|
|
|
/// Create table from a Apache Arrow IPC (file) buffer.
|
|
///
|
|
/// Parameters:
|
|
/// - name: The name of the table.
|
|
/// - buf: The buffer containing the IPC file.
|
|
///
|
|
#[napi]
|
|
pub async fn create_table(&self, name: String, buf: Buffer) -> napi::Result<Table> {
|
|
let batches = ipc_file_to_batches(buf.to_vec())
|
|
.map_err(|e| napi::Error::from_reason(format!("Failed to read IPC file: {}", e)))?;
|
|
let tbl = self
|
|
.conn
|
|
.create_table(&name, Box::new(batches))
|
|
.execute()
|
|
.await
|
|
.map_err(|e| napi::Error::from_reason(format!("{}", e)))?;
|
|
Ok(Table::new(tbl))
|
|
}
|
|
|
|
#[napi]
|
|
pub async fn open_table(&self, name: String) -> napi::Result<Table> {
|
|
let tbl = self
|
|
.conn
|
|
.open_table(&name)
|
|
.execute()
|
|
.await
|
|
.map_err(|e| napi::Error::from_reason(format!("{}", e)))?;
|
|
Ok(Table::new(tbl))
|
|
}
|
|
|
|
/// Drop table with the name. Or raise an error if the table does not exist.
|
|
#[napi]
|
|
pub async fn drop_table(&self, name: String) -> napi::Result<()> {
|
|
self.conn
|
|
.drop_table(&name)
|
|
.await
|
|
.map_err(|e| napi::Error::from_reason(format!("{}", e)))
|
|
}
|
|
}
|