Skip to content

Commit

Permalink
Update JS client
Browse files Browse the repository at this point in the history
  • Loading branch information
drewkim committed Oct 15, 2024
1 parent f2dec14 commit a41e4e6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
50 changes: 43 additions & 7 deletions clients/js/src/ChromaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
GetCollectionParams,
GetOrCreateCollectionParams,
ListCollectionsParams,
TenantAndDatabases,
} from "./types";
import { validateTenantDatabase, wrapCollection } from "./utils";

Expand All @@ -27,19 +28,19 @@ export class ChromaClient {
/**
* @ignore
*/
private tenant: string;
public tenant: string;
/**
* @ignore
*/
private database: string;
public database: string;
/**
* @ignore
*/
private _adminClient: AdminClient;
/**
* @ignore
*/
private authProvider: ClientAuthProvider | undefined;
public authProvider: ClientAuthProvider | undefined;
/**
* @ignore
*/
Expand Down Expand Up @@ -94,7 +95,9 @@ export class ChromaClient {
}

/** @ignore */
init(): Promise<void> {
async init(): Promise<void> {
await this.resolveTenantAndDatabases();

if (!this._initPromise) {
this._initPromise = validateTenantDatabase(
this._adminClient,
Expand All @@ -106,6 +109,39 @@ export class ChromaClient {
return this._initPromise;
}

/**
* Tries to set the tenant and database for the client.
*
* @returns {Promise<void>} A promise that resolves when the tenant/database is resolved.
* @throws {Error} If there is an issue resolving the tenant and database.
*
*/
async resolveTenantAndDatabases(): Promise<void> {
const response = (await this.api.resolveTenantAndDatabases(
this.api.options,
)) as TenantAndDatabases;
const user_identity = response as TenantAndDatabases;
const user_tenant = user_identity.tenant;
const user_databases = user_identity.databases;

if (
user_tenant !== null &&
user_tenant !== undefined &&
user_tenant !== "*"
) {
this.tenant = user_tenant;
}

if (
user_databases !== null &&
user_databases !== undefined &&
user_databases.length == 1 &&
user_databases[0] !== "*"
) {
this.database = user_databases[0];
}
}

/**
* Resets the state of the object by making an API call to the reset endpoint.
*
Expand Down Expand Up @@ -269,10 +305,10 @@ export class ChromaClient {
> {
await this.init();
return (await this.api.listCollections(
limit,
offset,
this.tenant,
this.database,
limit,
offset,
this.api.options,
)) as CollectionParams[];
}
Expand Down Expand Up @@ -320,9 +356,9 @@ export class ChromaClient {
await this.init();

const response = (await this.api.getCollection(
name,
this.tenant,
this.database,
name,
this.api.options,
)) as CollectionParams;

Expand Down
18 changes: 18 additions & 0 deletions clients/js/src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export class Collection {
await this.client.init();

await this.client.api.add(
this.client.tenant,
this.client.database,
this.id,
// TODO: For some reason the auto generated code requires metadata to be defined here.
(await prepareRecordRequest(
Expand Down Expand Up @@ -104,6 +106,8 @@ export class Collection {
await this.client.init();

await this.client.api.upsert(
this.client.tenant,
this.client.database,
this.id,
// TODO: For some reason the auto generated code requires metadata to be defined here.
(await prepareRecordRequest(
Expand All @@ -126,6 +130,8 @@ export class Collection {
async count(): Promise<number> {
await this.client.init();
return (await this.client.api.count(
this.client.tenant,
this.client.database,
this.id,
this.client.api.options,
)) as number;
Expand Down Expand Up @@ -168,6 +174,8 @@ export class Collection {

const resp = (await this.client.api.aGet(
this.id,
this.client.tenant,
this.client.database,
{
ids: idsArray,
where,
Expand Down Expand Up @@ -205,6 +213,8 @@ export class Collection {
await this.client.init();

await this.client.api.update(
this.client.tenant,
this.client.database,
this.id,
await prepareRecordRequest(params, this.embeddingFunction, true),
this.client.api.options,
Expand Down Expand Up @@ -266,6 +276,8 @@ export class Collection {
: toArrayOfArrays<number>(queryEmbeddings);

return (await this.client.api.getNearestNeighbors(
this.client.tenant,
this.client.database,
this.id,
{
query_embeddings: arrayQueryEmbeddings,
Expand Down Expand Up @@ -303,6 +315,8 @@ export class Collection {
await this.client.init();
return this.client.api
.updateCollection(
this.client.tenant,
this.client.database,
this.id,
{
new_name: name,
Expand Down Expand Up @@ -342,6 +356,8 @@ export class Collection {
await this.client.init();
return (await this.client.api.aGet(
this.id,
this.client.tenant,
this.client.database,
{
limit,
},
Expand Down Expand Up @@ -377,6 +393,8 @@ export class Collection {
if (ids !== undefined) idsArray = toArray(ids);
await this.client.api.aDelete(
this.id,
this.client.tenant,
this.client.database,
{ ids: idsArray, where: where, where_document: whereDocument },
this.client.api.options,
);
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/generated/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ export const ApiApiFetchParamCreator = function (configuration?: Configuration)
* ApiApi - functional programming interface
* @export
*/
export const ApiApiFp = function(configuration?: Configuration) {
export const ApiApiFp = function (configuration?: Configuration) {
return {
/**
* @summary Add
Expand Down
1 change: 1 addition & 0 deletions clients/js/src/generated/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'isomorphic-fetch';
import 'isomorphic-fetch';
/* eslint-disable */
// tslint:disable
/**
Expand Down
5 changes: 5 additions & 0 deletions clients/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,8 @@ export type DeleteParams = {
where?: Where;
whereDocument?: WhereDocument;
};

export type TenantAndDatabases = {
tenant: string;
databases: string[];
};

0 comments on commit a41e4e6

Please sign in to comment.