Skip to content

Commit

Permalink
switch to lazily calling init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlabasterAxe committed Jul 18, 2024
1 parent 4d34fc0 commit 60d0ade
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
30 changes: 18 additions & 12 deletions clients/js/src/ChromaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ChromaClient {
private database: string;
private _adminClient: AdminClient;
private authProvider: ClientAuthProvider | undefined;
private initPromise: Promise<void>;
private _initPromise: Promise<void> | undefined;

/**
* Creates a new ChromaClient instance.
Expand Down Expand Up @@ -77,13 +77,19 @@ export class ChromaClient {
tenant,
database,
});

this.initPromise = this.init();
}

/** @ignore */
private async init() {
await validateTenantDatabase(this._adminClient, this.tenant, this.database);
private init(): Promise<void> {
if (!this._initPromise) {
this._initPromise = validateTenantDatabase(
this._adminClient,
this.tenant,
this.database,
);
}

return this._initPromise;
}

/**
Expand All @@ -99,7 +105,7 @@ export class ChromaClient {
* ```
*/
async reset(): Promise<boolean> {
await this.initPromise;
await this.init();
return await this.api.reset(this.api.options);
}

Expand Down Expand Up @@ -161,7 +167,7 @@ export class ChromaClient {
metadata,
embeddingFunction = new DefaultEmbeddingFunction(),
}: CreateCollectionParams): Promise<Collection> {
await this.initPromise;
await this.init();
const newCollection = await this.api
.createCollection(
this.tenant,
Expand Down Expand Up @@ -216,7 +222,7 @@ export class ChromaClient {
metadata,
embeddingFunction = new DefaultEmbeddingFunction(),
}: GetOrCreateCollectionParams): Promise<Collection> {
await this.initPromise;
await this.init();
const newCollection = await this.api
.createCollection(
this.tenant,
Expand Down Expand Up @@ -259,7 +265,7 @@ export class ChromaClient {
async listCollections({ limit, offset }: ListCollectionsParams = {}): Promise<
CollectionType[]
> {
await this.initPromise;
await this.init();
const response = await this.api.listCollections(
limit,
offset,
Expand All @@ -282,7 +288,7 @@ export class ChromaClient {
* ```
*/
async countCollections(): Promise<number> {
await this.initPromise;
await this.init();

const response = await this.api.countCollections(
this.tenant,
Expand Down Expand Up @@ -311,7 +317,7 @@ export class ChromaClient {
name,
embeddingFunction,
}: GetCollectionParams): Promise<Collection> {
await this.initPromise;
await this.init();

const response = await this.api
.getCollection(name, this.tenant, this.database, this.api.options)
Expand Down Expand Up @@ -341,7 +347,7 @@ export class ChromaClient {
* ```
*/
async deleteCollection({ name }: DeleteCollectionParams): Promise<void> {
await this.initPromise;
await this.init();

return await this.api
.deleteCollection(name, this.tenant, this.database, this.api.options)
Expand Down
12 changes: 12 additions & 0 deletions clients/js/test/auth.basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { expect, test } from "@jest/globals";
import { chromaBasic } from "./initClientWithAuth";
import chromaNoAuth from "./initClient";

test("it should get the version without auth needed", async () => {
const version = await chromaNoAuth.version();
expect(version).toBeDefined();
expect(version).toMatch(/^[0-9]+\.[0-9]+\.[0-9]+$/);
});

test("it should get the heartbeat without auth needed", async () => {
const heartbeat = await chromaNoAuth.heartbeat();
expect(heartbeat).toBeDefined();
expect(heartbeat).toBeGreaterThan(0);
});

test("it should throw error when non authenticated", async () => {
try {
await chromaNoAuth.listCollections();
Expand Down

0 comments on commit 60d0ade

Please sign in to comment.