Skip to content

Commit

Permalink
feat: add incremental sync for google drive folders
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Dec 16, 2024
1 parent 48c42e4 commit c177a4d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/api/src/filestorage/folder/services/googledrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ export class GoogleDriveFolderService implements IFolderService {

async recursiveGetGoogleDriveFolders(
auth: OAuth2Client,
connectionId: string,
): Promise<GoogleDriveFolderOutput[]> {
const drive = google.drive({ version: 'v3', auth });

const lastSyncTime = await this.getLastSyncTime(connectionId);
if (lastSyncTime) {
console.log(`Last sync time is ${lastSyncTime.toISOString()}`);
}

const rootDriveId = await drive.files
.get({
fileId: 'root',
Expand Down Expand Up @@ -143,8 +149,10 @@ export class GoogleDriveFolderService implements IFolderService {
let pageToken: string | null = null;

const buildQuery = (parentId: string | null, driveId: string): string => {
const baseQuery =
"mimeType='application/vnd.google-apps.folder' and trashed=false";
let baseQuery = `mimeType='application/vnd.google-apps.folder' and trashed=false`;
if (lastSyncTime) {
baseQuery += ` and modifiedTime >= '${lastSyncTime.toISOString()}'`;
}
return parentId
? `${baseQuery} and '${parentId}' in parents`
: `${baseQuery} and '${driveId}' in parents`;
Expand Down Expand Up @@ -371,7 +379,10 @@ export class GoogleDriveFolderService implements IFolderService {
access_token: this.cryptoService.decrypt(connection.access_token),
});

const folders = await this.recursiveGetGoogleDriveFolders(auth);
const folders = await this.recursiveGetGoogleDriveFolders(
auth,
connection.id_connection,
);
await this.ingestPermissionsForFolders(folders, connection.id_connection);

this.logger.log(`Synced ${folders.length} Google Drive folders!`);
Expand All @@ -387,6 +398,14 @@ export class GoogleDriveFolderService implements IFolderService {
throw error;
}
}

private async getLastSyncTime(connectionId: string): Promise<Date | null> {
const lastSync = await this.prisma.fs_folders.findFirst({
where: { id_connection: connectionId },
orderBy: { remote_modified_at: 'desc' },
});
return lastSync ? lastSync.remote_modified_at : null;
}
}

// Type guard for Google API errors
Expand Down

0 comments on commit c177a4d

Please sign in to comment.