Skip to content

Commit

Permalink
test(database): fix returning ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ronal2do committed Dec 22, 2024
1 parent 5e4c3de commit 71e534d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
47 changes: 46 additions & 1 deletion src/database.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { afterAll, describe, expect, test } from "bun:test";
import { Database } from "./database";
import { Index } from "@upstash/vector";
import { awaitUntilIndexed } from "./test-utils";

const nanoidLength = 21;

describe("Database", () => {
const vector = new Index();

Expand Down Expand Up @@ -34,4 +35,48 @@ describe("Database", () => {
});
expect(result.map(({ data }) => data).join(" ")).toContain("330");
});

test("should save and retrieve info using id field", async () => {
const database = new Database(vector);

await database.save({
type: "text",
id: "eiffel-tower",
data: "Paris, the capital of France, is renowned for its iconic landmark, the Eiffel Tower, which was completed in 1889 and stands at 330 meters tall.",
});

await awaitUntilIndexed(vector);

const result = await database.retrieve({
question:
"What year was the construction of the Eiffel Tower completed, and what is its height?",
});
// sadly we can't test the id here
// because the questions are not unique
// every time we run the test we will add the same question
// and the id will be different because the database find the similarity
// we could create a unique namespace for each test
expect(result[0].data).toContain("1889");
expect(result[0].data).toContain("330 meters");
});

test("should return generated id when saving without providing an id", async () => {
const database = new Database(vector);

const saveResult = await database.save({
type: "text",
data: "Paris, the capital of France, is renowned for its iconic landmark, the Eiffel Tower.",
});

expect(saveResult.success).toBe(true);
if (!saveResult.success) {
throw new Error(`Failed to save entry: ${saveResult.error}`);
}

expect(saveResult.success).toBe(true);
expect(saveResult.ids).toHaveLength(1);
expect(typeof saveResult.ids[0]).toBe("string");
expect(saveResult.ids[0]).toBeTruthy();
expect(saveResult.ids[0]).toHaveLength(nanoidLength);
});
});
14 changes: 8 additions & 6 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,34 @@ export class Database {
const { namespace } = input.options ?? {};
if (input.type === "text") {
try {
const vectorId = await this.index.upsert(
const returnId = input.id ?? nanoid();
await this.index.upsert(
{
data: input.data,
id: input.id ?? nanoid(),
id: returnId,
metadata: input.options?.metadata,
},
{ namespace }
);

return { success: true, ids: [vectorId.toString()] };
return { success: true, ids: [returnId.toString()] };
} catch (error) {
return { success: false, error: JSON.stringify(error, Object.getOwnPropertyNames(error)) };
}
} else if (input.type === "embedding") {
try {
const vectorId = await this.index.upsert(
const returnId = input.id ?? nanoid();
await this.index.upsert(
{
vector: input.data,
data: input.text,
id: input.id ?? nanoid(),
id: returnId,
metadata: input.options?.metadata,
},
{ namespace }
);

return { success: true, ids: [vectorId.toString()] };
return { success: true, ids: [returnId.toString()] };
} catch (error) {
return { success: false, error: JSON.stringify(error, Object.getOwnPropertyNames(error)) };
}
Expand Down

0 comments on commit 71e534d

Please sign in to comment.