Source index

scripts/seed-data.mjs

import {readFile} from 'node:fs/promises';
const project = process.env.SANITY_PROJECT_ID || '2mflxxa8', dataset = process.env.SANITY_DATASET || 'accessatlas';
if (project !== '2mflxxa8' || dataset !== 'accessatlas') throw new Error('This seed command is restricted to the new public AccessAtlas dataset.');
const docs = (await readFile(new URL('../data/public-seed.ndjson', import.meta.url), 'utf8')).split(/\r?\n/).filter(Boolean).map(line => JSON.parse(line));
if (docs.length !== 21 || docs.some(doc => !['guide', 'source', 'check'].includes(doc._type))) throw new Error('Unexpected public seed shape.');
const token = process.env.SANITY_API_WRITE_TOKEN;
if (!token) throw new Error('Missing local server-side project Editor token.');
const readUrl = new URL(`https://${project}.api.sanity.io/v2026-10-02/data/query/${dataset}`);
readUrl.searchParams.set('query', 'count(*)');
const publicResponse = await fetch(readUrl);
if (!publicResponse.ok || (await publicResponse.json()).result !== 0) throw new Error('New dataset is not empty and publicly readable; seed refused.');
const response = await fetch(`https://${project}.api.sanity.io/v2026-10-02/data/mutate/${dataset}?visibility=sync&autoGenerateArrayKeys=true`, {method: 'POST', headers: {'Content-Type': 'application/json', Authorization: `Bearer ${token}`}, body: JSON.stringify({mutations: docs.map(doc => ({createIfNotExists: doc}))})});
if (!response.ok) throw new Error(`Seed HTTP ${response.status}; body omitted.`);
const result = await response.json();
if (!result.transactionId) throw new Error('Seed not confirmed.');
console.log(`Confirmed ${docs.length} public documents in ${project}/${dataset}. Existing production dataset was not targeted.`);