|
| 1 | +import { MetadataManager } from '@objectstack/metadata'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +// import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +// const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 6 | +const rootDir = path.resolve(__dirname, '../metadata'); |
| 7 | + |
| 8 | +async function main() { |
| 9 | + console.log('🚀 Starting Metadata Demo...'); |
| 10 | + console.log(`📂 Root Directory: ${rootDir}`); |
| 11 | + |
| 12 | + // 1. Initialize Manager |
| 13 | + const manager = new MetadataManager({ |
| 14 | + rootDir, |
| 15 | + formats: ['json', 'yaml', 'typescript'], |
| 16 | + watch: false |
| 17 | + }); |
| 18 | + |
| 19 | + // 2. List initial items |
| 20 | + console.log('\n2. Listing "object" items:'); |
| 21 | + const objects = await manager.list('object'); |
| 22 | + console.log(' Found:', objects); |
| 23 | + |
| 24 | + // 3. Load an item |
| 25 | + console.log('\n3. Loading "demo_object":'); |
| 26 | + const demoObject = await manager.load('object', 'demo_object'); |
| 27 | + console.log(' Loaded:', demoObject?.label); |
| 28 | + |
| 29 | + if (demoObject) { |
| 30 | + // 4. Modify and Save (Update) |
| 31 | + console.log('\n4. Updating "demo_object" (adding description)...'); |
| 32 | + demoObject.description = `Updated at ${new Date().toISOString()}`; |
| 33 | + |
| 34 | + // Explicitly using filesystem loader, using atomic writes |
| 35 | + const saveResult = await manager.save('object', 'demo_object', demoObject, { |
| 36 | + loader: 'filesystem', |
| 37 | + atomic: true, |
| 38 | + backup: true // Create .bak file |
| 39 | + }); |
| 40 | + console.log(' Save Result:', saveResult.success ? 'Success' : 'Failed'); |
| 41 | + console.log(' Path:', saveResult.path); |
| 42 | + } |
| 43 | + |
| 44 | + // 5. Create a NEW item programmatically |
| 45 | + console.log('\n5. Creating new "generated_object"...'); |
| 46 | + const newObject = { |
| 47 | + name: 'generated_object', |
| 48 | + label: 'Generated Object', |
| 49 | + type: 'object', |
| 50 | + fields: { |
| 51 | + auto_field: { type: 'text' } |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const createResult = await manager.save('object', 'generated_object', newObject, { |
| 56 | + format: 'yaml', // Save as YAML |
| 57 | + create: true |
| 58 | + }); |
| 59 | + console.log(' Created Result:', createResult.success ? 'Success' : 'Failed'); |
| 60 | + console.log(' Path:', createResult.path); |
| 61 | + |
| 62 | + // 6. Verify List again |
| 63 | + console.log('\n6. Final List:'); |
| 64 | + const finalObjects = await manager.list('object'); |
| 65 | + console.log(' Found:', finalObjects); |
| 66 | +} |
| 67 | + |
| 68 | +main().catch(console.error); |
0 commit comments