SyncEngine

The SyncEngine orchestrates the full data synchronization pipeline — fetching from GSC, storing in PostgreSQL, and writing snapshots to Sanity.

Constructor

import { SyncEngine } from '@pagebridge/core'

const engine = new SyncEngine({
  gsc: gscClient,    // GSCClient instance
  db: drizzleDb,     // Drizzle database client
  sanity: sanityClient // Sanity client with write access
})
typescript

sync(options)

Runs the main sync — fetches search analytics from GSC and upserts them into the database. Returns the list of synced pages and row count.

const result = await engine.sync({
  siteUrl: 'sc-domain:example.com',
  startDate: new Date(Date.now() - 90 * 86400000),  // 90 days ago
  endDate: new Date(Date.now() - 3 * 86400000),     // 3 days ago
  dimensions: ['page', 'query', 'date']
})

// result: SyncResult
// {
//   pages: string[],        // unique page URLs
//   rowsProcessed: number,  // total rows upserted
//   syncLogId: string       // ID of the sync log entry
// }
typescript

SyncOptions

interface SyncOptions {
  siteUrl: string
  startDate?: Date   // Default: 90 days ago
  endDate?: Date     // Default: 3 days ago
  dimensions?: ('page' | 'query' | 'date')[]
}
typescript

syncIndexStatus(siteUrl, pages)

Checks index status for a list of pages via the URL Inspection API. Rate-limited to 100ms between requests. Results are cached for 24 hours.

const indexResult = await engine.syncIndexStatus(
  'sc-domain:example.com',
  result.pages
)

// indexResult: IndexStatusSyncResult
// {
//   checked: number,    // total URLs checked
//   indexed: number,    // URLs passing index check
//   notIndexed: number, // URLs failing index check
//   skipped: number     // URLs skipped (cached)
// }
typescript

writeSnapshots(siteId, matches, siteUrl?)

Aggregates metrics and writes performance snapshots to Sanity. Creates snapshots for 7-day, 28-day, and 90-day periods with weekly breakdowns and top queries.

await engine.writeSnapshots(siteId, matchedResults, 'sc-domain:example.com')
typescript

getIndexStatus(siteUrl, page)

Retrieves cached index status for a specific page from the database.

const status = await engine.getIndexStatus(
  'sc-domain:example.com',
  'https://example.com/blog/my-post'
)
// Returns IndexStatusResult | null
typescript