Files
portfolio/scripts/ultra-setup-book-reviews.js
denshooter 931843a5c6
Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Has been cancelled
fix: add missing readBooks translations
2026-02-16 00:37:34 +01:00

67 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
require('dotenv').config();
const DIRECTUS_URL = process.env.DIRECTUS_URL || 'https://cms.dk0.dev';
const DIRECTUS_TOKEN = process.env.DIRECTUS_STATIC_TOKEN;
async function api(endpoint, method = 'GET', body = null) {
const url = `${DIRECTUS_URL}/${endpoint}`;
const options = {
method,
headers: { 'Authorization': `Bearer ${DIRECTUS_TOKEN}`, 'Content-Type': 'application/json' }
};
if (body) options.body = JSON.stringify(body);
const response = await fetch(url, options);
const data = await response.json();
return { ok: response.ok, data };
}
async function ultraSetup() {
console.log('🚀 Starte Ultra-Setup für "Book Reviews"...');
// 1. Collection anlegen (mit Primärschlüssel-Definition im Schema!)
await api('collections', 'POST', {
collection: 'book_reviews',
schema: {},
meta: { icon: 'import_contacts', display_template: '{{book_title}}' }
});
// 2. Felder mit explizitem Schema (Datenbank-Spalten)
const fields = [
{ field: 'id', type: 'integer', schema: { is_primary_key: true, has_auto_increment: true }, meta: { hidden: true } },
{ field: 'status', type: 'string', schema: { default_value: 'draft' }, meta: { interface: 'select-dropdown', width: 'half' } },
{ field: 'book_title', type: 'string', schema: {}, meta: { interface: 'input', width: 'full' } },
{ field: 'book_author', type: 'string', schema: {}, meta: { interface: 'input', width: 'half' } },
{ field: 'rating', type: 'integer', schema: {}, meta: { interface: 'rating', width: 'half' } },
{ field: 'book_image', type: 'string', schema: {}, meta: { interface: 'input', width: 'full' } },
{ field: 'hardcover_id', type: 'string', schema: { is_unique: true }, meta: { interface: 'input', width: 'half' } },
{ field: 'finished_at', type: 'date', schema: {}, meta: { interface: 'datetime', width: 'half' } }
];
for (const f of fields) {
await api('fields/book_reviews', 'POST', f);
}
// 3. Übersetzungen
await api('collections', 'POST', { collection: 'book_reviews_translations', schema: {}, meta: { hidden: true } });
const transFields = [
{ field: 'id', type: 'integer', schema: { is_primary_key: true, has_auto_increment: true }, meta: { hidden: true } },
{ field: 'book_reviews_id', type: 'integer', schema: {}, meta: { hidden: true } },
{ field: 'languages_code', type: 'string', schema: {}, meta: { interface: 'select-dropdown' } },
{ field: 'review', type: 'text', schema: {}, meta: { interface: 'input-rich-text-html' } }
];
for (const f of transFields) {
await api('fields/book_reviews_translations', 'POST', f);
}
// 4. Relationen (Der Kleber)
await api('relations', 'POST', { collection: 'book_reviews_translations', field: 'book_reviews_id', related_collection: 'book_reviews', meta: { one_field: 'translations' }, schema: { on_delete: 'CASCADE' } });
await api('relations', 'POST', { collection: 'book_reviews_translations', field: 'languages_code', related_collection: 'languages', schema: { on_delete: 'SET NULL' } });
await api('fields/book_reviews', 'POST', { field: 'translations', type: 'alias', meta: { interface: 'translations', special: ['translations'] } });
console.log('✅ Ultra-Setup abgeschlossen! Bitte lade Directus neu.');
}
ultraSetup().catch(console.error);