Fix: Remove SDK dependency from scripts - use fetch only
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Spark Platform - Directus Schema Import Script
|
* Spark Platform - Directus Schema Import Script
|
||||||
*
|
* Uses only fetch API - no external dependencies
|
||||||
* This script imports the collections, fields, and relations from the template
|
|
||||||
* into a fresh Directus instance.
|
|
||||||
*
|
|
||||||
* Usage: node scripts/import_template.js
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createDirectus, rest, staticToken } from '@directus/sdk';
|
const DIRECTUS_URL = process.env.PUBLIC_URL || process.env.DIRECTUS_URL || 'http://localhost:8055';
|
||||||
|
const DIRECTUS_TOKEN = process.env.ADMIN_TOKEN || process.env.DIRECTUS_ADMIN_TOKEN;
|
||||||
|
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { dirname, join } from 'path';
|
import { dirname, join } from 'path';
|
||||||
@@ -15,12 +13,10 @@ import { dirname, join } from 'path';
|
|||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
const DIRECTUS_URL = process.env.PUBLIC_URL || process.env.DIRECTUS_URL || 'http://localhost:8055';
|
|
||||||
const DIRECTUS_TOKEN = process.env.ADMIN_TOKEN || process.env.DIRECTUS_ADMIN_TOKEN;
|
|
||||||
|
|
||||||
if (!DIRECTUS_TOKEN) {
|
if (!DIRECTUS_TOKEN) {
|
||||||
console.error('❌ ADMIN_TOKEN or DIRECTUS_ADMIN_TOKEN is required');
|
console.error('❌ ADMIN_TOKEN is required');
|
||||||
console.log('Set it in your environment or run: export ADMIN_TOKEN=your-token');
|
console.log('Run: export ADMIN_TOKEN="your-token-here"');
|
||||||
|
console.log('Get token from: Directus Admin → Settings → Access Tokens');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,65 +36,34 @@ try {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const directus = createDirectus(DIRECTUS_URL).with(rest()).with(staticToken(DIRECTUS_TOKEN));
|
async function apiRequest(method, endpoint, body = null) {
|
||||||
|
const options = {
|
||||||
async function createCollection(collection) {
|
method,
|
||||||
const response = await fetch(`${DIRECTUS_URL}/collections`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
|
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
}
|
||||||
body: JSON.stringify(collection)
|
};
|
||||||
});
|
if (body) options.body = JSON.stringify(body);
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json();
|
|
||||||
throw new Error(error.errors?.[0]?.message || response.statusText);
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createField(collectionName, field) {
|
const response = await fetch(`${DIRECTUS_URL}${endpoint}`, options);
|
||||||
const response = await fetch(`${DIRECTUS_URL}/fields/${collectionName}`, {
|
const data = await response.json();
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(field)
|
|
||||||
});
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json();
|
|
||||||
throw new Error(error.errors?.[0]?.message || response.statusText);
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createRelation(relation) {
|
|
||||||
const response = await fetch(`${DIRECTUS_URL}/relations`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(relation)
|
|
||||||
});
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
throw new Error(data.errors?.[0]?.message || response.statusText);
|
||||||
throw new Error(error.errors?.[0]?.message || response.statusText);
|
|
||||||
}
|
}
|
||||||
return response.json();
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importSchema() {
|
async function importSchema() {
|
||||||
console.log('🚀 Starting Spark Platform schema import...');
|
console.log('🚀 Starting Spark Platform schema import...');
|
||||||
console.log(` Directus URL: ${DIRECTUS_URL}\n`);
|
console.log(` URL: ${DIRECTUS_URL}\n`);
|
||||||
|
|
||||||
// Create collections
|
// Create collections
|
||||||
console.log('📦 Creating collections...');
|
console.log('📦 Creating collections...');
|
||||||
for (const collection of collections) {
|
for (const collection of collections) {
|
||||||
try {
|
try {
|
||||||
await createCollection(collection);
|
await apiRequest('POST', '/collections', collection);
|
||||||
console.log(` ✅ ${collection.collection}`);
|
console.log(` ✅ ${collection.collection}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message?.includes('already exists')) {
|
if (err.message?.includes('already exists')) {
|
||||||
@@ -114,7 +79,7 @@ async function importSchema() {
|
|||||||
for (const [collectionName, collectionFields] of Object.entries(fields)) {
|
for (const [collectionName, collectionFields] of Object.entries(fields)) {
|
||||||
for (const field of collectionFields) {
|
for (const field of collectionFields) {
|
||||||
try {
|
try {
|
||||||
await createField(collectionName, field);
|
await apiRequest('POST', `/fields/${collectionName}`, field);
|
||||||
console.log(` ✅ ${collectionName}.${field.field}`);
|
console.log(` ✅ ${collectionName}.${field.field}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message?.includes('already exists')) {
|
if (err.message?.includes('already exists')) {
|
||||||
@@ -130,7 +95,7 @@ async function importSchema() {
|
|||||||
console.log('\n🔗 Creating relations...');
|
console.log('\n🔗 Creating relations...');
|
||||||
for (const relation of relations) {
|
for (const relation of relations) {
|
||||||
try {
|
try {
|
||||||
await createRelation(relation);
|
await apiRequest('POST', '/relations', relation);
|
||||||
console.log(` ✅ ${relation.collection}.${relation.field} → ${relation.related_collection}`);
|
console.log(` ✅ ${relation.collection}.${relation.field} → ${relation.related_collection}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message?.includes('already exists')) {
|
if (err.message?.includes('already exists')) {
|
||||||
@@ -144,7 +109,7 @@ async function importSchema() {
|
|||||||
console.log('\n✨ Schema import complete!');
|
console.log('\n✨ Schema import complete!');
|
||||||
}
|
}
|
||||||
|
|
||||||
importSchema().catch((err) => {
|
importSchema().catch(err => {
|
||||||
console.error('❌ Import failed:', err);
|
console.error('❌ Import failed:', err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user