-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-data.js
More file actions
executable file
·588 lines (553 loc) · 14.4 KB
/
seed-data.js
File metadata and controls
executable file
·588 lines (553 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env node
/**
* Seed Script for Directus CMS Test Data
*
* Creates:
* - Collections: posts, pages, categories, authors
* - Sample data for each collection
* - Activity logs through various API operations
*/
const API_URL = 'http://localhost:8055';
const EMAIL = 'admin@example.com';
const PASSWORD = 'admin123';
let authToken = null;
/**
* Authenticate with Directus and get access token
*/
async function authenticate() {
const response = await fetch(`${API_URL}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: EMAIL, password: PASSWORD }),
});
if (!response.ok) {
throw new Error(`Authentication failed: ${response.statusText}`);
}
const data = await response.json();
authToken = data.data.access_token;
console.log('✅ Authenticated successfully');
return authToken;
}
/**
* Make authenticated API request
*/
async function apiRequest(endpoint, options = {}) {
const response = await fetch(`${API_URL}${endpoint}`, {
...options,
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
const error = await response.text();
throw new Error(`API request failed: ${response.statusText}\n${error}`);
}
return response.json();
}
/**
* Create a collection with fields
*/
async function createCollection(collectionName, fields) {
console.log(`\n📦 Creating collection: ${collectionName}`);
// Create collection
await apiRequest('/collections', {
method: 'POST',
body: JSON.stringify({
collection: collectionName,
meta: {
collection: collectionName,
icon: getIconForCollection(collectionName),
note: `${collectionName.charAt(0).toUpperCase() + collectionName.slice(1)} collection`,
display_template: null,
hidden: false,
singleton: false,
translations: null,
archive_field: null,
archive_app_filter: true,
archive_value: null,
unarchive_value: null,
sort_field: null,
accountability: 'all',
color: null,
item_duplication_fields: null,
sort: null,
group: null,
collapse: 'open',
},
schema: {
name: collectionName,
},
fields: [
{
field: 'id',
type: 'integer',
meta: {
hidden: true,
interface: 'input',
readonly: true,
},
schema: {
is_primary_key: true,
has_auto_increment: true,
},
},
],
}),
});
// Add custom fields
for (const field of fields) {
await apiRequest(`/fields/${collectionName}`, {
method: 'POST',
body: JSON.stringify(field),
});
}
console.log(`✅ Collection ${collectionName} created with ${fields.length} fields`);
}
/**
* Get appropriate icon for collection
*/
function getIconForCollection(name) {
const icons = {
posts: 'article',
pages: 'description',
categories: 'folder',
authors: 'person',
};
return icons[name] || 'box';
}
/**
* Insert items into a collection
*/
async function insertItems(collection, items) {
console.log(`\n📝 Inserting ${items.length} items into ${collection}`);
for (const item of items) {
await apiRequest(`/items/${collection}`, {
method: 'POST',
body: JSON.stringify(item),
});
}
console.log(`✅ Inserted ${items.length} items into ${collection}`);
}
/**
* Generate activity by reading collections
*/
async function generateActivity() {
console.log('\n🔄 Generating activity logs...');
const collections = ['posts', 'pages', 'categories', 'authors'];
for (let i = 0; i < 10; i++) {
for (const collection of collections) {
// Read items (generates read activity)
await apiRequest(`/items/${collection}?limit=10`);
// Add a small delay to make timestamps different
await new Promise(resolve => setTimeout(resolve, 100));
}
}
console.log('✅ Generated activity logs');
}
/**
* Main execution
*/
async function main() {
try {
console.log('🚀 Starting Directus CMS data seeding...\n');
// Authenticate
await authenticate();
// Create Categories collection
await createCollection('categories', [
{
field: 'name',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'Category name' },
display: 'raw',
readonly: false,
hidden: false,
required: true,
},
schema: {
name: 'name',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'slug',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'category-slug' },
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'slug',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'description',
type: 'text',
meta: {
interface: 'input-multiline',
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'description',
data_type: 'text',
},
},
]);
// Create Authors collection
await createCollection('authors', [
{
field: 'name',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'Author name' },
display: 'raw',
readonly: false,
hidden: false,
required: true,
},
schema: {
name: 'name',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'email',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'author@example.com' },
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'email',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'bio',
type: 'text',
meta: {
interface: 'input-multiline',
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'bio',
data_type: 'text',
},
},
]);
// Create Posts collection
await createCollection('posts', [
{
field: 'title',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'Post title' },
display: 'raw',
readonly: false,
hidden: false,
required: true,
},
schema: {
name: 'title',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'slug',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'post-slug' },
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'slug',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'content',
type: 'text',
meta: {
interface: 'input-rich-text-html',
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'content',
data_type: 'text',
},
},
{
field: 'status',
type: 'string',
meta: {
interface: 'select-dropdown',
options: {
choices: [
{ text: 'Draft', value: 'draft' },
{ text: 'Published', value: 'published' },
{ text: 'Archived', value: 'archived' },
],
},
display: 'labels',
readonly: false,
hidden: false,
},
schema: {
name: 'status',
data_type: 'varchar',
max_length: 20,
default_value: 'draft',
},
},
{
field: 'category',
type: 'integer',
meta: {
interface: 'select-dropdown-m2o',
display: 'related-values',
readonly: false,
hidden: false,
},
schema: {
name: 'category',
data_type: 'integer',
foreign_key_table: 'categories',
foreign_key_column: 'id',
},
},
{
field: 'author',
type: 'integer',
meta: {
interface: 'select-dropdown-m2o',
display: 'related-values',
readonly: false,
hidden: false,
},
schema: {
name: 'author',
data_type: 'integer',
foreign_key_table: 'authors',
foreign_key_column: 'id',
},
},
{
field: 'published_date',
type: 'timestamp',
meta: {
interface: 'datetime',
display: 'datetime',
readonly: false,
hidden: false,
},
schema: {
name: 'published_date',
data_type: 'timestamp',
},
},
]);
// Create Pages collection
await createCollection('pages', [
{
field: 'title',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'Page title' },
display: 'raw',
readonly: false,
hidden: false,
required: true,
},
schema: {
name: 'title',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'slug',
type: 'string',
meta: {
interface: 'input',
options: { placeholder: 'page-slug' },
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'slug',
data_type: 'varchar',
max_length: 255,
},
},
{
field: 'content',
type: 'text',
meta: {
interface: 'input-rich-text-html',
display: 'raw',
readonly: false,
hidden: false,
},
schema: {
name: 'content',
data_type: 'text',
},
},
{
field: 'status',
type: 'string',
meta: {
interface: 'select-dropdown',
options: {
choices: [
{ text: 'Draft', value: 'draft' },
{ text: 'Published', value: 'published' },
],
},
display: 'labels',
readonly: false,
hidden: false,
},
schema: {
name: 'status',
data_type: 'varchar',
max_length: 20,
default_value: 'draft',
},
},
]);
// Insert sample data
await insertItems('categories', [
{ name: 'Technology', slug: 'technology', description: 'Tech news and tutorials' },
{ name: 'Business', slug: 'business', description: 'Business insights' },
{ name: 'Lifestyle', slug: 'lifestyle', description: 'Lifestyle articles' },
{ name: 'Travel', slug: 'travel', description: 'Travel guides and tips' },
]);
await insertItems('authors', [
{ name: 'John Doe', email: 'john@example.com', bio: 'Senior tech writer' },
{ name: 'Jane Smith', email: 'jane@example.com', bio: 'Business analyst' },
{ name: 'Bob Wilson', email: 'bob@example.com', bio: 'Lifestyle blogger' },
]);
await insertItems('posts', [
{
title: 'Getting Started with Directus',
slug: 'getting-started-directus',
content: '<p>Learn how to use Directus for your projects...</p>',
status: 'published',
category: 1,
author: 1,
published_date: new Date('2025-01-15').toISOString(),
},
{
title: 'Building Modern APIs',
slug: 'building-modern-apis',
content: '<p>Best practices for API development...</p>',
status: 'published',
category: 1,
author: 1,
published_date: new Date('2025-01-16').toISOString(),
},
{
title: 'Business Growth Strategies',
slug: 'business-growth',
content: '<p>Strategies for scaling your business...</p>',
status: 'published',
category: 2,
author: 2,
published_date: new Date('2025-01-17').toISOString(),
},
{
title: 'Remote Work Tips',
slug: 'remote-work-tips',
content: '<p>Tips for effective remote work...</p>',
status: 'published',
category: 3,
author: 3,
published_date: new Date('2025-01-18').toISOString(),
},
{
title: 'Best Travel Destinations 2025',
slug: 'travel-destinations-2025',
content: '<p>Top places to visit in 2025...</p>',
status: 'published',
category: 4,
author: 3,
published_date: new Date('2025-01-19').toISOString(),
},
{
title: 'Draft Post Example',
slug: 'draft-post',
content: '<p>This is a draft post...</p>',
status: 'draft',
category: 1,
author: 1,
published_date: null,
},
]);
await insertItems('pages', [
{
title: 'About Us',
slug: 'about',
content: '<p>Learn more about our company...</p>',
status: 'published',
},
{
title: 'Contact',
slug: 'contact',
content: '<p>Get in touch with us...</p>',
status: 'published',
},
{
title: 'Privacy Policy',
slug: 'privacy',
content: '<p>Our privacy policy...</p>',
status: 'published',
},
]);
// Generate activity logs
await generateActivity();
console.log('\n✅ Data seeding completed successfully!');
console.log('\n📊 Summary:');
console.log(' - Collections: categories, authors, posts, pages');
console.log(' - Categories: 4 items');
console.log(' - Authors: 3 items');
console.log(' - Posts: 6 items (5 published, 1 draft)');
console.log(' - Pages: 3 items');
console.log(' - Activity logs: ~40 entries');
console.log('\n🎯 You can now test the Usage Analytics extension!');
} catch (error) {
console.error('\n❌ Error:', error.message);
process.exit(1);
}
}
// Run the script
main();