Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,041 changes: 3,732 additions & 3,309 deletions apps/sanity/extract.json

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions apps/sanity/functions/sync-nightly-dataset/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { scheduledEventHandler } from "@sanity/functions";

export const handler = scheduledEventHandler(async ({ context }) => {
console.log("[Sync Nightly Dataset] Starting scheduled dataset sync...");

const projectId =
context.clientOptions?.projectId ||
process.env.SANITY_STUDIO_PROJECT_ID ||
"hfh83o0w";
const token =
context.clientOptions?.token || process.env.SANITY_AUTH_TOKEN;

if (!token) {
throw new Error(
"[Sync Nightly Dataset] Missing Sanity API token (neither context.clientOptions.token nor SANITY_AUTH_TOKEN is present).",
);
}

const apiHost =
context.clientOptions?.apiHost || "https://api.sanity.io";
const apiVersion = "v2025-09-30";

const targetDataset = "dev";
const sourceDataset = "production";

console.log(
`[Sync Nightly Dataset] Resetting dataset "${targetDataset}" from source "${sourceDataset}" for project ${projectId}...`,
);

// 1. Delete existing target dataset (dev) if it exists
const deleteUrl = `${apiHost}/${apiVersion}/projects/${projectId}/datasets/${targetDataset}`;
console.log(`[Sync Nightly Dataset] Deleting existing dataset "${targetDataset}": ${deleteUrl}`);

const deleteResp = await fetch(deleteUrl, {
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
});

if (deleteResp.ok) {
console.log(
`[Sync Nightly Dataset] Target dataset "${targetDataset}" deleted successfully (HTTP ${deleteResp.status}).`,
);
} else if (deleteResp.status === 404) {
console.log(
`[Sync Nightly Dataset] Target dataset "${targetDataset}" does not exist yet (HTTP 404). Proceeding to clone.`,
);
} else {
const deleteError = await deleteResp.text();
console.warn(
`[Sync Nightly Dataset] Notice when deleting dataset "${targetDataset}": HTTP ${deleteResp.status} — ${deleteError}`,
);
}

// 2. Clone source dataset (production) to target (dev) via Sanity Enterprise Copy API
const copyUrl = `${apiHost}/${apiVersion}/projects/${projectId}/datasets/${sourceDataset}/copy`;
console.log(
`[Sync Nightly Dataset] Copying "${sourceDataset}" -> "${targetDataset}": ${copyUrl}`,
);

const copyResp = await fetch(copyUrl, {
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
targetDataset,
skipHistory: true,
}),
});

if (!copyResp.ok) {
const errorText = await copyResp.text();
throw new Error(
`[Sync Nightly Dataset] Failed to initiate dataset copy from "${sourceDataset}" to "${targetDataset}": HTTP ${copyResp.status} — ${errorText}`,
);
}

const copyResult = await copyResp.json().catch(() => ({}));
console.log(
`[Sync Nightly Dataset] Successfully initiated dataset copy from "${sourceDataset}" to "${targetDataset}":`,
copyResult,
);
});
Loading
Loading