Skip to content

Commit c177a4d

Browse files
committed
feat: add incremental sync for google drive folders
1 parent 48c42e4 commit c177a4d

File tree

1 file changed

+22
-3
lines changed
  • packages/api/src/filestorage/folder/services/googledrive

1 file changed

+22
-3
lines changed

packages/api/src/filestorage/folder/services/googledrive/index.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ export class GoogleDriveFolderService implements IFolderService {
102102

103103
async recursiveGetGoogleDriveFolders(
104104
auth: OAuth2Client,
105+
connectionId: string,
105106
): Promise<GoogleDriveFolderOutput[]> {
106107
const drive = google.drive({ version: 'v3', auth });
107108

109+
const lastSyncTime = await this.getLastSyncTime(connectionId);
110+
if (lastSyncTime) {
111+
console.log(`Last sync time is ${lastSyncTime.toISOString()}`);
112+
}
113+
108114
const rootDriveId = await drive.files
109115
.get({
110116
fileId: 'root',
@@ -143,8 +149,10 @@ export class GoogleDriveFolderService implements IFolderService {
143149
let pageToken: string | null = null;
144150

145151
const buildQuery = (parentId: string | null, driveId: string): string => {
146-
const baseQuery =
147-
"mimeType='application/vnd.google-apps.folder' and trashed=false";
152+
let baseQuery = `mimeType='application/vnd.google-apps.folder' and trashed=false`;
153+
if (lastSyncTime) {
154+
baseQuery += ` and modifiedTime >= '${lastSyncTime.toISOString()}'`;
155+
}
148156
return parentId
149157
? `${baseQuery} and '${parentId}' in parents`
150158
: `${baseQuery} and '${driveId}' in parents`;
@@ -371,7 +379,10 @@ export class GoogleDriveFolderService implements IFolderService {
371379
access_token: this.cryptoService.decrypt(connection.access_token),
372380
});
373381

374-
const folders = await this.recursiveGetGoogleDriveFolders(auth);
382+
const folders = await this.recursiveGetGoogleDriveFolders(
383+
auth,
384+
connection.id_connection,
385+
);
375386
await this.ingestPermissionsForFolders(folders, connection.id_connection);
376387

377388
this.logger.log(`Synced ${folders.length} Google Drive folders!`);
@@ -387,6 +398,14 @@ export class GoogleDriveFolderService implements IFolderService {
387398
throw error;
388399
}
389400
}
401+
402+
private async getLastSyncTime(connectionId: string): Promise<Date | null> {
403+
const lastSync = await this.prisma.fs_folders.findFirst({
404+
where: { id_connection: connectionId },
405+
orderBy: { remote_modified_at: 'desc' },
406+
});
407+
return lastSync ? lastSync.remote_modified_at : null;
408+
}
390409
}
391410

392411
// Type guard for Google API errors

0 commit comments

Comments
 (0)