d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
114 lines
4.6 KiB
TypeScript
114 lines
4.6 KiB
TypeScript
import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit'
|
|
import { createLogger } from '@sim/logger'
|
|
import { generateId } from '@sim/utils/id'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { exportTableAsyncContract } from '@/lib/api/contracts/tables'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { isTriggerDevEnabled } from '@/lib/core/config/env-flags'
|
|
import { runDetached } from '@/lib/core/utils/background'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { captureServerEvent } from '@/lib/posthog/server'
|
|
import { runTableExport, type TableExportPayload } from '@/lib/table/export-runner'
|
|
import { markTableJobRunning, releaseJobClaim } from '@/lib/table/jobs/service'
|
|
import type { TableExportJobPayload } from '@/lib/table/types'
|
|
import { accessError, checkAccess } from '@/app/api/table/utils'
|
|
|
|
const logger = createLogger('TableExportAsync')
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
interface RouteParams {
|
|
params: Promise<{ tableId: string }>
|
|
}
|
|
|
|
/**
|
|
* POST /api/table/[tableId]/export-async
|
|
*
|
|
* Kicks off a background export for large tables (small ones stream synchronously via `/export`).
|
|
* Export jobs are read-only, so they bypass the one-running-job-per-table gate (the partial-unique
|
|
* index excludes `type = 'export'`) — an export can run alongside an import or delete, and the
|
|
* delete-mask keeps a mid-delete export consistent with the delete's outcome.
|
|
*/
|
|
export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
|
|
const requestId = generateRequestId()
|
|
|
|
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
|
}
|
|
|
|
const parsed = await parseRequest(exportTableAsyncContract, request, { params })
|
|
if (!parsed.success) return parsed.response
|
|
const { tableId } = parsed.data.params
|
|
const { workspaceId, format } = parsed.data.body
|
|
|
|
const access = await checkAccess(tableId, authResult.userId, 'read')
|
|
if (!access.ok) return accessError(access, requestId, tableId)
|
|
if (access.table.workspaceId !== workspaceId) {
|
|
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
|
|
}
|
|
|
|
const jobId = generateId()
|
|
const jobPayload: TableExportJobPayload = { format }
|
|
const claimed = await markTableJobRunning(tableId, jobId, 'export', jobPayload)
|
|
if (!claimed) {
|
|
// Only possible against another running *export*-typed insert race losing on the pkey, or a
|
|
// missing table — the active-job index excludes exports.
|
|
return NextResponse.json({ error: 'Failed to start export' }, { status: 409 })
|
|
}
|
|
|
|
const payload: TableExportPayload = {
|
|
jobId,
|
|
tableId,
|
|
workspaceId,
|
|
format,
|
|
}
|
|
if (isTriggerDevEnabled) {
|
|
try {
|
|
const [{ tableExportTask }, { tasks }, { resolveTriggerRegion }] = await Promise.all([
|
|
import('@/background/table-export'),
|
|
import('@trigger.dev/sdk'),
|
|
import('@/lib/core/async-jobs/region'),
|
|
])
|
|
await tasks.trigger<typeof tableExportTask>('table-export', payload, {
|
|
tags: [`tableId:${tableId}`, `jobId:${jobId}`],
|
|
region: await resolveTriggerRegion(),
|
|
})
|
|
} catch (error) {
|
|
// A failed dispatch must not leave a ghost `running` job holding the
|
|
// table's one-write-job slot until the stale-job janitor fires.
|
|
await releaseJobClaim(tableId, jobId).catch(() => {})
|
|
throw error
|
|
}
|
|
} else {
|
|
runDetached('table-export', () => runTableExport(payload))
|
|
}
|
|
|
|
// Audit at authorization (like the sync route) so a failed/abandoned job still records the export.
|
|
recordAudit({
|
|
workspaceId,
|
|
actorId: authResult.userId,
|
|
action: AuditAction.TABLE_EXPORTED,
|
|
resourceType: AuditResourceType.TABLE,
|
|
resourceId: tableId,
|
|
resourceName: access.table.name,
|
|
description: `Exported table "${access.table.name}" as ${format.toUpperCase()}`,
|
|
metadata: { format, rowCount: access.table.rowCount, async: true },
|
|
request,
|
|
})
|
|
if (access.table.workspaceId) {
|
|
captureServerEvent(
|
|
authResult.userId,
|
|
'table_exported',
|
|
{ table_id: tableId, workspace_id: workspaceId },
|
|
{ groups: { workspace: workspaceId } }
|
|
)
|
|
}
|
|
|
|
logger.info(`[${requestId}] Async export started`, { tableId, jobId, format })
|
|
return NextResponse.json({ success: true, data: { tableId, jobId } })
|
|
})
|