Google Calendar の増分同期実装パターン
バックエンド
Google Calendar API
sync token を使用した効率的な変更取得の実装方法。
-
初回同期
const response = await calendar.events.list({ calendarId: 'primary', maxResults: 2500, showDeleted: true, singleEvents: true }); // sync token を保存 await this.calendarSyncTokenRepository.upsert({ tenantId, userId, calendarId: 'primary', syncToken: response.data.nextSyncToken, lastSyncedAt: new Date() });
-
増分同期
try { const response = await calendar.events.list({ calendarId: 'primary', syncToken: savedSyncToken, // 保存済みトークン maxResults: 2500 }); // 変更されたイベントを処理 for (const event of response.data.items || []) { await this.processEventChange(event); } } catch (error) { if (error.code === 410) { // sync token 無効時は初回同期を再実行 return this.performInitialSync(); } }
-
ページネーション対応
let pageToken: string | undefined; do { const response = await calendar.events.list({ calendarId: 'primary', syncToken, pageToken, maxResults: 2500 }); pageToken = response.data.nextPageToken; // イベント処理... } while (pageToken);