Skip to content

Commit

Permalink
Add new method
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 17, 2025
1 parent 1b62316 commit a854ef6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
43 changes: 40 additions & 3 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 @@ -304,16 +305,52 @@ export class ChromaClient {
* ```
*/
async listCollections({ limit, offset }: ListCollectionsParams = {}): Promise<
Collection[]
string[]
> {
await this.init();
return (await this.api.listCollections(
const collections = (await this.api.listCollections(
this.tenant,
this.database,
limit,
offset,
this.api.options,
)) as Collection[];
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
14 changes: 12 additions & 2 deletions clients/js/test/collection.client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,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 @@ -33,7 +43,7 @@ describe("collection operations", () => {

const [returnedCollection] = collections;

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

expect([{ name: "test2", metadata: null }]).not.toEqual(
expect.arrayContaining(collections),
Expand All @@ -54,7 +64,7 @@ describe("collection operations", () => {
const collections2 = await client.listCollections();
expect(collections2).toHaveLength(1);
const [returnedCollection2] = collections2;
expect(returnedCollection2.name).toEqual("test2");
expect(returnedCollection2).toEqual("test2");
});

test("it should get a collection", async () => {
Expand Down

0 comments on commit a854ef6

Please sign in to comment.