Skip to content

[ENH]: fix typings on JS client #3501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions clients/js/src/AdminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ interface Tenant {
name: string;
}

// interface for tenant
interface Database {
id: string;
tenant: string;
name: string;
}

Expand Down Expand Up @@ -203,7 +204,7 @@ export class AdminClient {
}: {
name: string;
tenantName: string;
}): Promise<Database> {
}): Promise<{ name: string }> {
await this.api.createDatabase(tenantName, { name }, this.api.options);

return { name };
Expand Down Expand Up @@ -234,13 +235,13 @@ export class AdminClient {
name: string;
tenantName: string;
}): Promise<Database> {
const getDatabase = (await this.api.getDatabase(
const result = (await this.api.getDatabase(
name,
tenantName,
this.api.options,
)) as Database;

return { name: getDatabase.name } as Database;
return result;
}

/**
Expand Down Expand Up @@ -282,13 +283,11 @@ export class AdminClient {
offset?: number;
tenantName: string;
}): Promise<Database[]> {
const listDatabases = (await this.api.listDatabases(
return (await this.api.listDatabases(
tenantName,
limit,
offset,
this.api.options,
)) as Database[];

return listDatabases.map((db) => ({ name: db.name }));
}
}
40 changes: 38 additions & 2 deletions clients/js/src/ChromaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DefaultEmbeddingFunction } from "./embeddings/DefaultEmbeddingFunction"
import { Configuration, ApiApi as DefaultApi } from "./generated";
import type {
ChromaClientParams,
CollectionMetadata,
CollectionParams,
ConfigOptions,
CreateCollectionParams,
Expand Down Expand Up @@ -288,7 +289,7 @@ export class ChromaClient {
}

/**
* Lists all collections.
* Get all collection names.
*
* @returns {Promise<string[]>} A promise that resolves to a list of collection names.
* @param {PositiveInteger} [params.limit] - Optional limit on the number of items to get.
Expand All @@ -314,7 +315,42 @@ export class ChromaClient {
offset,
this.api.options,
)) as Collection[];
return collections.map((collection: Collection) => collection.name);
return collections.map((collection) => collection.name);
}

/**
* List collection names, IDs, and metadata.
*
* @param {PositiveInteger} [params.limit] - Optional limit on the number of items to get.
* @param {PositiveInteger} [params.offset] - Optional offset on the items to get.
* @throws {Error} If there is an issue listing the collections.
* @returns {Promise<{ name: string, id: string, metadata?: CollectionMetadata }[]>} A promise that resolves to a list of collection names, IDs, and metadata.
*
* @example
* ```typescript
* const collections = await client.listCollectionsAndMetadata({
* limit: 10,
* offset: 0,
* });
*/
async listCollectionsAndMetadata({
limit,
offset,
}: ListCollectionsParams = {}): Promise<
{
name: string;
id: string;
metadata?: CollectionMetadata;
}[]
> {
await this.init();
return (await this.api.listCollections(
this.tenant,
this.database,
limit,
offset,
this.api.options,
)) as CollectionParams[];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ export type {
DeleteParams,
CollectionParams,
} from "./types";

export * from "./Errors";
19 changes: 12 additions & 7 deletions clients/js/test/collection.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
expect,
test,
beforeEach,
describe,
} from "@jest/globals";
import { expect, test, beforeEach, describe } from "@jest/globals";
import { DefaultEmbeddingFunction } from "../src";
import { ChromaClient } from "../src";

Expand All @@ -27,6 +22,16 @@ describe("collection operations", () => {
expect(collections).toHaveLength(1);
});

test("it should list collections with metadata", async () => {
await client.createCollection({ name: "test", metadata: { test: "test" } });
const collections = await client.listCollectionsAndMetadata();
expect(collections).toHaveLength(1);
const [collection] = collections;
expect(collection).toHaveProperty("metadata");
expect(collection.metadata).toHaveProperty("test");
expect(collection.metadata).toEqual({ test: "test" });
});

test("it should create a collection", async () => {
const collection = await client.createCollection({ name: "test" });
expect(collection).toBeDefined();
Expand All @@ -38,7 +43,7 @@ describe("collection operations", () => {

const [returnedCollection] = collections;

expect(returnedCollection).toEqual("test")
expect(returnedCollection).toEqual("test");

expect([{ name: "test2", metadata: null }]).not.toEqual(
expect.arrayContaining(collections),
Expand Down
Loading