Skip to content

Commit cbe2c4f

Browse files
authored
[ENH] Add getCollectionByCrn api support in js client (#5894)
## Description of changes _Summarize the changes made by this PR._ - Improvements & Bug fixes - This PR adds support for getCollectionByCrn to the js client - It also had to add support for tenant and db fields on the collection object to allow for building the correct path for both private and public collections. this way, instead of using the path w/ tenant and db from the client, it directly uses data from the server about the collection's origin - New functionality - ... ## Test plan _How are these changes tested?_ - [ ] Tests pass locally with `pytest` for python, `yarn test` for js, `cargo test` for rust ## Migration plan _Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?_ ## Observability plan _What is the plan to instrument and monitor this change?_ ## Documentation Changes _Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the [docs section](https://github.com/chroma-core/chroma/tree/main/docs/docs.trychroma.com)?_
1 parent 156e21b commit cbe2c4f

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

clients/new-js/packages/chromadb/src/chroma-client.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ export class ChromaClient {
263263
return new CollectionImpl({
264264
chromaClient: this,
265265
apiClient: this.apiClient,
266+
tenant: collection.tenant,
267+
database: collection.database,
266268
name: collection.name,
267269
id: collection.id,
268270
embeddingFunction: resolvedEmbeddingFunction,
@@ -349,6 +351,8 @@ export class ChromaClient {
349351
chromaClient: this,
350352
apiClient: this.apiClient,
351353
name,
354+
tenant: data.tenant,
355+
database: data.database,
352356
configuration: data.configuration_json,
353357
metadata: deserializeMetadata(data.metadata ?? undefined) ?? undefined,
354358
embeddingFunction: resolvedEmbeddingFunction,
@@ -392,6 +396,40 @@ export class ChromaClient {
392396
chromaClient: this,
393397
apiClient: this.apiClient,
394398
name,
399+
tenant: data.tenant,
400+
database: data.database,
401+
configuration: data.configuration_json,
402+
metadata: deserializeMetadata(data.metadata ?? undefined) ?? undefined,
403+
embeddingFunction: resolvedEmbeddingFunction,
404+
id: data.id,
405+
schema,
406+
});
407+
}
408+
409+
/**
410+
* Retrieves an existing collection by its Chroma Resource Name (CRN).
411+
* @param crn - The Chroma Resource Name of the collection to retrieve
412+
* @returns Promise resolving to the Collection instance
413+
* @throws Error if the collection does not exist
414+
*/
415+
public async getCollectionByCrn(crn: string): Promise<Collection> {
416+
const { data } = await Api.getCollectionByCrn({
417+
client: this.apiClient,
418+
path: { crn },
419+
});
420+
const schema = await Schema.deserializeFromJSON(data.schema ?? undefined);
421+
const schemaEmbeddingFunction = resolveSchemaEmbeddingFunction(schema);
422+
const resolvedEmbeddingFunction =
423+
(await getEmbeddingFunction(
424+
data.name,
425+
data.configuration_json.embedding_function ?? undefined,
426+
)) ?? schemaEmbeddingFunction;
427+
return new CollectionImpl({
428+
chromaClient: this,
429+
apiClient: this.apiClient,
430+
name: data.name,
431+
tenant: data.tenant,
432+
database: data.database,
395433
configuration: data.configuration_json,
396434
metadata: deserializeMetadata(data.metadata ?? undefined) ?? undefined,
397435
embeddingFunction: resolvedEmbeddingFunction,
@@ -489,6 +527,8 @@ export class ChromaClient {
489527
chromaClient: this,
490528
apiClient: this.apiClient,
491529
name,
530+
tenant: data.tenant,
531+
database: data.database,
492532
configuration: data.configuration_json,
493533
metadata: deserializeMetadata(data.metadata ?? undefined) ?? undefined,
494534
embeddingFunction: resolvedEmbeddingFunction,

clients/new-js/packages/chromadb/src/collection.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ import type { SparseVectorIndexConfig } from "./schema";
5252
* Provides methods for adding, querying, updating, and deleting records.
5353
*/
5454
export interface Collection {
55+
/** Tenant name */
56+
tenant: string;
57+
/** Database name */
58+
database: string;
5559
/** Unique identifier for the collection */
5660
id: string;
5761
/** Name of the collection */
@@ -214,6 +218,10 @@ export interface CollectionArgs {
214218
name: string;
215219
/** Collection ID */
216220
id: string;
221+
/** Tenant name */
222+
tenant: string;
223+
/** Database name */
224+
database: string;
217225
/** Embedding function for the collection */
218226
embeddingFunction?: EmbeddingFunction;
219227
/** Collection configuration */
@@ -232,6 +240,8 @@ export class CollectionImpl implements Collection {
232240
protected readonly chromaClient: ChromaClient;
233241
protected readonly apiClient: ReturnType<typeof createClient>;
234242
public readonly id: string;
243+
public readonly tenant: string;
244+
public readonly database: string;
235245
private _name: string;
236246
private _metadata: CollectionMetadata | undefined;
237247
private _configuration: CollectionConfiguration;
@@ -246,6 +256,8 @@ export class CollectionImpl implements Collection {
246256
chromaClient,
247257
apiClient,
248258
id,
259+
tenant,
260+
database,
249261
name,
250262
metadata,
251263
configuration,
@@ -255,6 +267,8 @@ export class CollectionImpl implements Collection {
255267
this.chromaClient = chromaClient;
256268
this.apiClient = apiClient;
257269
this.id = id;
270+
this.tenant = tenant;
271+
this.database = database;
258272
this._name = name;
259273
this._metadata = metadata;
260274
this._configuration = configuration;
@@ -309,9 +323,9 @@ export class CollectionImpl implements Collection {
309323
database: string;
310324
collection_id: string;
311325
}> {
312-
const clientPath = await this.chromaClient._path();
313326
return {
314-
...clientPath,
327+
tenant: this.tenant,
328+
database: this.database,
315329
collection_id: this.id,
316330
};
317331
}
@@ -958,6 +972,8 @@ export class CollectionImpl implements Collection {
958972
chromaClient: this.chromaClient,
959973
apiClient: this.apiClient,
960974
name: data.name,
975+
tenant: this.tenant,
976+
database: this.database,
961977
id: data.id,
962978
embeddingFunction: this._embeddingFunction,
963979
metadata: deserializeMetadata(data.metadata ?? undefined) ?? undefined,

clients/new-js/packages/chromadb/test/schema.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,8 @@ describe("Schema", () => {
13491349
apiClient: {} as any,
13501350
id: "test-id",
13511351
name: "test",
1352+
tenant: "default_tenant",
1353+
database: "default_database",
13521354
configuration: {} as CollectionConfiguration,
13531355
metadata: undefined as CollectionMetadata | undefined,
13541356
embeddingFunction: undefined,
@@ -1399,6 +1401,8 @@ describe("Schema", () => {
13991401
apiClient: mockApiClient as any,
14001402
id: "test-id",
14011403
name: "test",
1404+
tenant: "default_tenant",
1405+
database: "default_database",
14021406
configuration: {} as CollectionConfiguration,
14031407
metadata: undefined as CollectionMetadata | undefined,
14041408
embeddingFunction: undefined,
@@ -1470,6 +1474,8 @@ describe("Schema", () => {
14701474
apiClient: mockApiClient as any,
14711475
id: "test-id",
14721476
name: "test",
1477+
tenant: "default_tenant",
1478+
database: "default_database",
14731479
configuration: {} as CollectionConfiguration,
14741480
metadata: undefined as CollectionMetadata | undefined,
14751481
embeddingFunction: undefined,
@@ -1545,6 +1551,8 @@ describe("Schema", () => {
15451551
apiClient: mockApiClient as any,
15461552
id: "test-id",
15471553
name: "test",
1554+
tenant: "default_tenant",
1555+
database: "default_database",
15481556
configuration: {} as CollectionConfiguration,
15491557
metadata: undefined as CollectionMetadata | undefined,
15501558
embeddingFunction: undefined,
@@ -1621,6 +1629,8 @@ describe("Schema", () => {
16211629
apiClient: mockApiClient as any,
16221630
id: "test-id",
16231631
name: "test",
1632+
tenant: "default_tenant",
1633+
database: "default_database",
16241634
configuration: {} as CollectionConfiguration,
16251635
metadata: undefined as CollectionMetadata | undefined,
16261636
embeddingFunction: undefined,
@@ -1691,6 +1701,8 @@ describe("Schema", () => {
16911701
apiClient: mockApiClient as any,
16921702
id: "test-id",
16931703
name: "test",
1704+
tenant: "default_tenant",
1705+
database: "default_database",
16941706
configuration: {} as CollectionConfiguration,
16951707
metadata: undefined as CollectionMetadata | undefined,
16961708
embeddingFunction: undefined,

clients/new-js/packages/chromadb/test/search.expression.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ describe("search expression DSL", () => {
380380
apiClient: mockApiClient as any,
381381
id: "col-id",
382382
name: "test",
383+
tenant: "default_tenant",
384+
database: "default_database",
383385
configuration: {} as CollectionConfiguration,
384386
metadata: undefined,
385387
embeddingFunction,
@@ -469,6 +471,8 @@ describe("search expression DSL", () => {
469471
apiClient: mockApiClient as any,
470472
id: "col-id",
471473
name: "test",
474+
tenant: "default_tenant",
475+
database: "default_database",
472476
configuration: {} as CollectionConfiguration,
473477
metadata: undefined,
474478
embeddingFunction: undefined,

0 commit comments

Comments
 (0)