Files
klz-cables.com/scripts/fix-schema.ts
2026-02-01 02:02:03 +01:00

100 lines
3.6 KiB
TypeScript

import client, { ensureAuthenticated } from '../lib/directus';
import {
createCollection,
createField,
createItem,
readCollections,
deleteCollection
} from '@directus/sdk';
async function fixSchema() {
console.log('🚑 EXTERNAL RESCUE: Fixing Schema & Data...');
await ensureAuthenticated();
// 1. Reset Products Collection to be 100% Standard
console.log('🗑️ Clearing broken collections...');
try { await client.request(deleteCollection('products')); } catch (e) { }
try { await client.request(deleteCollection('products_translations')); } catch (e) { }
// 2. Create Products (Simple, Standard ID)
console.log('🏗️ Rebuilding Products Schema...');
await client.request(createCollection({
collection: 'products',
schema: {}, // Let Directus decide defaults
meta: {
display_template: '{{sku}}',
archive_field: 'status',
archive_value: 'archived',
unarchive_value: 'published'
},
fields: [
{
field: 'id',
type: 'integer',
schema: { is_primary_key: true, has_auto_increment: true },
meta: { hidden: true }
},
{
field: 'status',
type: 'string',
schema: { default_value: 'published' },
meta: { width: 'full', options: { choices: [{ text: 'Published', value: 'published' }] } }
},
{
field: 'sku',
type: 'string',
meta: { interface: 'input', width: 'half' }
}
]
} as any));
// 3. Create Translation Relation Safely
console.log('🌍 Rebuilding Translations...');
await client.request(createCollection({
collection: 'products_translations',
schema: {},
fields: [
{
field: 'id',
type: 'integer',
schema: { is_primary_key: true, has_auto_increment: true },
meta: { hidden: true }
},
{ field: 'products_id', type: 'integer' },
{ field: 'languages_code', type: 'string' },
{ field: 'name', type: 'string', meta: { interface: 'input', width: 'full' } },
{ field: 'description', type: 'text', meta: { interface: 'input-multiline' } },
{ field: 'technical_items', type: 'json', meta: { interface: 'input-code-json' } }
]
} as any));
// 4. Manually Insert ONE Product to Verify
console.log('📦 Injecting Test Product...');
try {
// We do this in two steps to be absolutely sure permissions don't block us
// Step A: Create User-Facing Product
const product = await client.request(createItem('products', {
sku: 'H1Z2Z2-K-TEST',
status: 'published'
}));
// Step B: Add Translation
await client.request(createItem('products_translations', {
products_id: product.id,
languages_code: 'de-DE',
name: 'H1Z2Z2-K Test Cable',
description: 'This is a verified imported product.',
technical_items: [{ label: 'Test', value: '100%' }]
}));
console.log(`✅ SUCCESS! Product Created with ID: ${product.id}`);
console.log(`verify at: ${process.env.DIRECTUS_URL}/admin/content/products/${product.id}`);
} catch (e: any) {
console.error('❌ Failed to create product:', e);
if (e.errors) console.error(JSON.stringify(e.errors, null, 2));
}
}
fixSchema().catch(console.error);