Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(backend): paginate storage object listing

Richie McIlroy83bbd5ab9b49
diff --git a/packages/web-backend/src/Storage/StorageRepo.ts b/packages/web-backend/src/Storage/StorageRepo.ts
index 4e78297bf..4e63dbee5 100644
--- a/packages/web-backend/src/Storage/StorageRepo.ts
+++ b/packages/web-backend/src/Storage/StorageRepo.ts
@@ -423,21 +423,32 @@ export class StorageRepo extends Effect.Service<StorageRepo>()("StorageRepo", {
integrationId: Storage.StorageIntegrationId,
prefix: string | undefined,
maxKeys: number | undefined,
+ continuationToken: string | undefined,
) =>
- db.use((db) => {
- const where = prefix
- ? Dz.and(
- Dz.eq(Db.storageObjects.integrationId, integrationId),
- Dz.sql`BINARY ${Db.storageObjects.objectKey} LIKE ${`${escapeLikePattern(prefix)}%`}`,
- )
- : Dz.eq(Db.storageObjects.integrationId, integrationId);
-
- return db
+ db.use(async (db) => {
+ const pageSize = Math.min(Math.max(maxKeys ?? 1_000, 1), 1_000);
+ const objects = await db
.select()
.from(Db.storageObjects)
- .where(where)
- .orderBy(Db.storageObjects.objectKey)
- .limit(maxKeys ?? 1000);
+ .where(
+ Dz.and(
+ Dz.eq(Db.storageObjects.integrationId, integrationId),
+ prefix
+ ? Dz.sql`BINARY ${Db.storageObjects.objectKey} LIKE ${`${escapeLikePattern(prefix)}%`}`
+ : undefined,
+ continuationToken
+ ? Dz.sql`BINARY ${Db.storageObjects.objectKey} > BINARY ${continuationToken}`
+ : undefined,
+ ),
+ )
+ .orderBy(Dz.sql`BINARY ${Db.storageObjects.objectKey}`)
+ .limit(pageSize + 1);
+ const page = objects.slice(0, pageSize);
+ return {
+ objects: page,
+ nextContinuationToken:
+ objects.length > pageSize ? page.at(-1)?.objectKey : undefined,
+ };
}),
);
diff --git a/packages/web-backend/src/Storage/index.ts b/packages/web-backend/src/Storage/index.ts
index b53dfe781..b8e9a3186 100644
--- a/packages/web-backend/src/Storage/index.ts
+++ b/packages/web-backend/src/Storage/index.ts
@@ -489,10 +489,15 @@ const makeGoogleDriveAccess = ({
continuationToken?: string;
}) =>
mapStorageError(
- repo.listObjectsByPrefix(integrationId, input.prefix, input.maxKeys),
+ repo.listObjectsByPrefix(
+ integrationId,
+ input.prefix,
+ input.maxKeys,
+ input.continuationToken,
+ ),
).pipe(
- Effect.map((objects) => ({
- Contents: objects
+ Effect.map(({ objects, nextContinuationToken }) => {
+ const contents = objects
.filter(
(object) =>
object.contentType !== GOOGLE_DRIVE_FOLDER_MIME_TYPE &&
@@ -503,16 +508,14 @@ const makeGoogleDriveAccess = ({
Key: object.objectKey,
Size: object.contentLength ?? undefined,
LastModified: object.updatedAt,
- })),
- KeyCount: objects.filter(
- (object) =>
- object.contentType !== GOOGLE_DRIVE_FOLDER_MIME_TYPE &&
- !object.objectKey.startsWith(".cap-folders/") &&
- !object.objectKey.startsWith(".cap-warnings/"),
- ).length,
- IsTruncated: false,
- NextContinuationToken: undefined,
- })),
+ }));
+ return {
+ Contents: contents,
+ KeyCount: contents.length,
+ IsTruncated: nextContinuationToken !== undefined,
+ NextContinuationToken: nextContinuationToken,
+ };
+ }),
),
headObject: (key: string) =>
getObjectRecord(key).pipe(