Skip to content

Commit

Permalink
purely initial validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlabasterAxe committed Jul 18, 2024
1 parent 25ae8e4 commit 100d8d3
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions clients/js/src/ChromaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ChromaClient {
* ```
*/
constructor({
path,
path = "http://localhost:8000",
fetchOptions,
auth,
tenant = DEFAULT_TENANT,
Expand Down Expand Up @@ -78,10 +78,12 @@ export class ChromaClient {
database: database,
});

// TODO: Validate tenant and database on client creation
// this got tricky because:
// - the constructor is sync but the generated api is async
// - we need to inject auth information so a simple rewrite/fetch does not work
this.initPromise = this.init();
}

/** @ignore */
private async init() {
await validateTenantDatabase(this._adminClient, this.tenant, this.database);
}

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

Expand All @@ -110,8 +113,8 @@ export class ChromaClient {
* const version = await client.version();
* ```
*/
public async version(): Promise<string> {
const response = await this.api.version(this.api.options);
async version(): Promise<string> {
await this.initPromise;
return await handleSuccess(response);
}

Expand All @@ -125,7 +128,8 @@ export class ChromaClient {
* const heartbeat = await client.heartbeat();
* ```
*/
public async heartbeat(): Promise<number> {
async heartbeat(): Promise<number> {
await this.initPromise;
const response = await this.api.heartbeat(this.api.options);
let ret = await handleSuccess(response);
return ret["nanosecond heartbeat"];
Expand Down Expand Up @@ -153,17 +157,12 @@ export class ChromaClient {
* });
* ```
*/
public async createCollection({
async createCollection({
name,
metadata,
embeddingFunction,
embeddingFunction = new DefaultEmbeddingFunction(),
}: CreateCollectionParams): Promise<Collection> {
if (embeddingFunction === undefined) {
embeddingFunction = new DefaultEmbeddingFunction();
}

const newCollection = await this.api
.createCollection(
await this.initPromise;
this.tenant,
this.database,
{
Expand Down Expand Up @@ -211,17 +210,12 @@ export class ChromaClient {
* });
* ```
*/
public async getOrCreateCollection({
async getOrCreateCollection({
name,
metadata,
embeddingFunction,
embeddingFunction = new DefaultEmbeddingFunction(),
}: GetOrCreateCollectionParams): Promise<Collection> {
if (embeddingFunction === undefined) {
embeddingFunction = new DefaultEmbeddingFunction();
}

const newCollection = await this.api
.createCollection(
await this.initPromise;
this.tenant,
this.database,
{
Expand Down Expand Up @@ -259,10 +253,10 @@ export class ChromaClient {
* });
* ```
*/
public async listCollections({
async listCollections({ limit, offset }: ListCollectionsParams = {}): Promise<
limit,
offset,
}: ListCollectionsParams = {}): Promise<CollectionType[]> {
await this.initPromise;
const response = await this.api.listCollections(
limit,
offset,
Expand All @@ -284,8 +278,8 @@ export class ChromaClient {
* const collections = await client.countCollections();
* ```
*/
public async countCollections(): Promise<number> {
const response = await this.api.countCollections(
async countCollections(): Promise<number> {
await this.initPromise;
this.tenant,
this.database,
this.api.options,
Expand All @@ -308,13 +302,11 @@ export class ChromaClient {
* });
* ```
*/
public async getCollection({
async getCollection({
name,
embeddingFunction,
}: GetCollectionParams): Promise<Collection> {
const response = await this.api
.getCollection(name, this.tenant, this.database, this.api.options)
.then(handleSuccess);
await this.initPromise;

return new Collection(
response.name,
Expand All @@ -339,7 +331,8 @@ export class ChromaClient {
* });
* ```
*/
public async deleteCollection({
async deleteCollection({ name }: DeleteCollectionParams): Promise<void> {
await this.initPromise;
name,
}: DeleteCollectionParams): Promise<void> {
return await this.api
Expand Down

0 comments on commit 100d8d3

Please sign in to comment.