Skip to main content
Use views.sync() when you need fresh results right now. A sync re-executes the view query and updates the materialized files in the virtual filesystem.
views.sync() is idempotent and safe to call repeatedly. It is a good fit for pre-read checks, cron jobs, and retryable worker tasks.

Single-view sync

import Airstore from '@airstore/sdk'

const airstore = new Airstore()

const workspaceId = 'ws_abc123'
const viewId = 'view_abc123'

const result = await airstore.views.sync(workspaceId, viewId)
console.log(`View: ${result.external_id}`)
console.log(`Last synced: ${result.last_synced_at}`)
console.log(`Results: ${result.results_count} total`)
console.log(`New results: ${result.new_results}`)

Sync strategy

import Airstore from '@airstore/sdk'

const airstore = new Airstore()

await airstore.views.sync('ws_abc123', 'view_abc123')

Before-read refresh pattern

If your workflow requires freshness, sync first and then read from the view path.
import Airstore from '@airstore/sdk'

const airstore = new Airstore()

async function readFreshestFirstFile(workspaceId: string, viewId: string, viewPath: string) {
  await airstore.views.sync(workspaceId, viewId)

  const entries = await airstore.fs.list(workspaceId, { path: viewPath })
  const firstFile = entries.find((entry) => entry.type !== 'directory')
  if (!firstFile?.path) return null

  return airstore.fs.read(workspaceId, { path: firstFile.path })
}

Scheduled sync job

This pattern works for periodic refresh workers. Keep sync tasks retry-friendly and continue if one view fails.
import Airstore from '@airstore/sdk'

const airstore = new Airstore()

async function syncAllViewsWithRetries(workspaceId: string, attempts = 3) {
  const views = await airstore.views.list(workspaceId)

  for (const view of views) {
    let success = false

    for (let i = 1; i <= attempts; i += 1) {
      try {
        await airstore.views.sync(workspaceId, view.external_id)
        success = true
        break
      } catch (error) {
        if (i === attempts) {
          console.error(`Failed syncing ${view.name} after ${attempts} attempts`, error)
          continue
        }

        const backoffMs = i * 1000
        await new Promise((resolve) => setTimeout(resolve, backoffMs))
      }
    }

    if (success) {
      console.log(`Synced ${view.name}`)
    }
  }
}

// Example: invoke this from your scheduler every 15 minutes.
await syncAllViewsWithRetries('ws_abc123')