diff --git a/src/collections/collection/index.ts b/src/collections/collection/index.ts index 243a5533..c4f7ee31 100644 --- a/src/collections/collection/index.ts +++ b/src/collections/collection/index.ts @@ -63,6 +63,12 @@ export interface Collection { * are returned. Use `wvc.QueryReference` to specify which references to return. */ iterator: (opts?: IteratorOptions) => Iterator; + /** + * 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; /** * Use this method to return a collection object specific to a single consistency level. * @@ -111,9 +117,16 @@ const collection = ( throw new WeaviateInvalidInputError(`The collection name must be a string, got: ${typeof name}`); } const capitalizedName = capitalizeCollectionName(name); + const aggregateCollection = aggregate( + connection, + capitalizedName, + dbVersionSupport, + consistencyLevel, + tenant + ); const queryCollection = query(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant); return { - aggregate: aggregate(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant), + aggregate: aggregateCollection, backup: backupCollection(connection, capitalizedName), config: config(connection, capitalizedName, dbVersionSupport, tenant), data: data(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant), @@ -139,6 +152,7 @@ const collection = ( }) .then((res) => res.objects) ), + length: () => aggregateCollection.overAll().then(({ totalCount }) => totalCount), withConsistency: (consistencyLevel: ConsistencyLevel) => collection(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant), withTenant: (tenant: string | TT) =>