Skip to content

Commit

Permalink
Merge pull request #229 from weaviate/add-missing-collection-length-m…
Browse files Browse the repository at this point in the history
…ethod

Adds `await collection.length()` shorthand method
  • Loading branch information
tsmith023 authored Nov 13, 2024
2 parents 977523a + 45036dc commit 0d444cd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/collections/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface Collection<T = undefined, N = string> {
* are returned. Use `wvc.QueryReference` to specify which references to return.
*/
iterator: (opts?: IteratorOptions<T>) => Iterator<T>;
/**
* Use this method to return the total number of objects in the collection.
*
* This is a short-hand for calling `collection.aggregate.overAll().then(({ totalCount }) => totalCount)`.
*/
length: () => Promise<number>;
/**
* Use this method to return a collection object specific to a single consistency level.
*
Expand Down Expand Up @@ -111,9 +117,16 @@ const collection = <T, N>(
throw new WeaviateInvalidInputError(`The collection name must be a string, got: ${typeof name}`);
}
const capitalizedName = capitalizeCollectionName(name);
const aggregateCollection = aggregate<T>(
connection,
capitalizedName,
dbVersionSupport,
consistencyLevel,
tenant
);
const queryCollection = query<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant);
return {
aggregate: aggregate<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
aggregate: aggregateCollection,
backup: backupCollection(connection, capitalizedName),
config: config<T>(connection, capitalizedName, dbVersionSupport, tenant),
data: data<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
Expand All @@ -139,6 +152,7 @@ const collection = <T, N>(
})
.then((res) => res.objects)
),
length: () => aggregateCollection.overAll().then(({ totalCount }) => totalCount),
withConsistency: (consistencyLevel: ConsistencyLevel) =>
collection<T, N>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
withTenant: <TT extends TenantBase>(tenant: string | TT) =>
Expand Down

0 comments on commit 0d444cd

Please sign in to comment.