Skip to content

Commit 0d444cd

Browse files
authored
Merge pull request #229 from weaviate/add-missing-collection-length-method
Adds `await collection.length()` shorthand method
2 parents 977523a + 45036dc commit 0d444cd

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/collections/collection/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export interface Collection<T = undefined, N = string> {
6363
* are returned. Use `wvc.QueryReference` to specify which references to return.
6464
*/
6565
iterator: (opts?: IteratorOptions<T>) => Iterator<T>;
66+
/**
67+
* Use this method to return the total number of objects in the collection.
68+
*
69+
* This is a short-hand for calling `collection.aggregate.overAll().then(({ totalCount }) => totalCount)`.
70+
*/
71+
length: () => Promise<number>;
6672
/**
6773
* Use this method to return a collection object specific to a single consistency level.
6874
*
@@ -111,9 +117,16 @@ const collection = <T, N>(
111117
throw new WeaviateInvalidInputError(`The collection name must be a string, got: ${typeof name}`);
112118
}
113119
const capitalizedName = capitalizeCollectionName(name);
120+
const aggregateCollection = aggregate<T>(
121+
connection,
122+
capitalizedName,
123+
dbVersionSupport,
124+
consistencyLevel,
125+
tenant
126+
);
114127
const queryCollection = query<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant);
115128
return {
116-
aggregate: aggregate<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
129+
aggregate: aggregateCollection,
117130
backup: backupCollection(connection, capitalizedName),
118131
config: config<T>(connection, capitalizedName, dbVersionSupport, tenant),
119132
data: data<T>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
@@ -139,6 +152,7 @@ const collection = <T, N>(
139152
})
140153
.then((res) => res.objects)
141154
),
155+
length: () => aggregateCollection.overAll().then(({ totalCount }) => totalCount),
142156
withConsistency: (consistencyLevel: ConsistencyLevel) =>
143157
collection<T, N>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
144158
withTenant: <TT extends TenantBase>(tenant: string | TT) =>

0 commit comments

Comments
 (0)