mirror of
https://github.com/lancedb/lancedb.git
synced 2026-01-09 13:22:58 +00:00
In order to add support for `add` we needed to migrate the rust `Table`
trait to a `Table` struct and `TableInternal` trait (similar to the way
the connection is designed).
While doing this we also cleaned up some inconsistencies between the
SDKs:
* Python and Node are garbage collected languages and it can be
difficult to trigger something to be freed. The convention for these
languages is to have some kind of close method. I added a close method
to both the table and connection which will drop the underlying rust
object.
* We made significant improvements to table creation in
cc5f2136a6
for the `node` SDK. I copied these changes to the `nodejs` SDK.
* The nodejs tables were using fs to create tmp directories and these
were not getting cleaned up. This is mostly harmless but annoying and so
I changed it up a bit to ensure we cleanup tmp directories.
* ~~countRows in the node SDK was returning `bigint`. I changed it to
return `number`~~ (this actually happened in a previous PR)
* Tables and connections now implement `std::fmt::Display` which is
hooked into python's `__repr__`. Node has no concept of a regular "to
string" function and so I added a `display` method.
* Python method signatures are changing so that optional parameters are
always `Optional[foo] = None` instead of something like `foo = False`.
This is because we want those defaults to be in rust whenever possible
(though we still need to mention the default in documentation).
* I changed the python `AsyncConnection/AsyncTable` classes from
abstract classes with a single implementation to just classes because we
no longer have the remote implementation in python.
Note: this does NOT add the `add` function to the remote table. This PR
was already large enough, and the remote implementation is unique
enough, that I am going to do all the remote stuff at a later date (we
should have the structure in place and correct so there shouldn't be any
refactor concerns)
---------
Co-authored-by: Will Jones <willjones127@gmail.com>
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
// 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.
|
|
|
|
import * as tmp from "tmp";
|
|
|
|
import { Connection, connect } from "../dist/index.js";
|
|
|
|
describe("when connecting", () => {
|
|
|
|
let tmpDir: tmp.DirResult;
|
|
beforeEach(() => tmpDir = tmp.dirSync({ unsafeCleanup: true }));
|
|
afterEach(() => tmpDir.removeCallback());
|
|
|
|
it("should connect", async() => {
|
|
const db = await connect(tmpDir.name);
|
|
expect(db.display()).toBe(`NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=None)`);
|
|
})
|
|
|
|
it("should allow read consistency interval to be specified", async() => {
|
|
const db = await connect(tmpDir.name, { readConsistencyInterval: 5});
|
|
expect(db.display()).toBe(`NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=5s)`);
|
|
})
|
|
});
|
|
|
|
describe("given a connection", () => {
|
|
|
|
let tmpDir: tmp.DirResult
|
|
let db: Connection
|
|
beforeEach(async () => {
|
|
tmpDir = tmp.dirSync({ unsafeCleanup: true });
|
|
db = await connect(tmpDir.name)
|
|
});
|
|
afterEach(() => tmpDir.removeCallback());
|
|
|
|
it("should raise an error if opening a non-existent table", async() => {
|
|
await expect(db.openTable("non-existent")).rejects.toThrow("was not found");
|
|
})
|
|
|
|
it("should raise an error if any operation is tried after it is closed", async() => {
|
|
expect(db.isOpen()).toBe(true);
|
|
await db.close();
|
|
expect(db.isOpen()).toBe(false);
|
|
await expect(db.tableNames()).rejects.toThrow("Connection is closed");
|
|
})
|
|
|
|
it("should fail if creating table twice, unless overwrite is true", async() => {
|
|
let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]);
|
|
await expect(tbl.countRows()).resolves.toBe(2);
|
|
await expect(db.createTable("test", [{ id: 1 }, { id: 2 }])).rejects.toThrow();
|
|
tbl = await db.createTable("test", [{ id: 3 }], { mode: "overwrite" });
|
|
await expect(tbl.countRows()).resolves.toBe(1);
|
|
})
|
|
|
|
it("should list tables", async() => {
|
|
await db.createTable("test2", [{ id: 1 }, { id: 2 }]);
|
|
await db.createTable("test1", [{ id: 1 }, { id: 2 }]);
|
|
expect(await db.tableNames()).toEqual(["test1", "test2"]);
|
|
})
|
|
|
|
});
|