ユーザーアイコン

mizuko

2か月前

0
0

Google Calendar の増分同期実装パターン

バックエンド
Google Calendar API

sync token を使用した効率的な変更取得の実装方法。

  1. 初回同期

    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() });
  2. 増分同期

    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(); } }
  3. ページネーション対応

    let pageToken: string | undefined; do { const response = await calendar.events.list({ calendarId: 'primary', syncToken, pageToken, maxResults: 2500 }); pageToken = response.data.nextPageToken; // イベント処理... } while (pageToken);