Cookbook · Maintaining
Wrap a bulk create in a verify-and-rollback pattern so a bad batch never lands on disk.
Recipe
import { UPGClient, type CreateNodeArgs } from '@unified-product-graph/sdk'
const upg = new UPGClient({ file: './product.upg' })
async function safeBulkCreate(items: CreateNodeArgs[]) {
const before = await upg.health()
const created = await Promise.all(items.map(i => upg.nodes.create(i)))
const report = await upg.verify()
if (report.errors.length > 0) {
console.error('rollback — validation failed:', report.errors)
for (const { node } of created) {
await upg.nodes.delete(node.id)
}
return null
}
const after = await upg.health()
console.log(`+${created.length} nodes · health ${before.score} → ${after.score}`)
return created
}What it does
verify() runs the schema validator and the anti-pattern catalogue. The pattern: create, verify, rollback on errors. Cheaper than pre-validating each item because the validator catches cross-node anti-patterns the SDK can not detect at create time.
Variations
Soft-warn mode (log + continue)
const report = await upg.verify()
report.warnings.forEach(w => console.warn(w.code, w.message))See also