|
1 | 1 | /** |
2 | 2 | * Excel Driver Demo |
3 | 3 | * |
4 | | - * This example demonstrates the basic usage of the Excel Driver for ObjectQL. |
5 | | - * It creates, queries, updates, and deletes records in an Excel file. |
| 4 | + * This example demonstrates both storage modes of the Excel Driver for ObjectQL: |
| 5 | + * 1. Single-file mode (default): All data in one Excel file |
| 6 | + * 2. File-per-object mode: Each object type in its own file |
6 | 7 | */ |
7 | 8 |
|
8 | 9 | import { ExcelDriver } from '@objectql/driver-excel'; |
9 | 10 | import * as path from 'path'; |
10 | 11 |
|
11 | | -async function main() { |
12 | | - console.log('🚀 Excel Driver Demo\n'); |
| 12 | +async function demoFilePerObjectMode() { |
| 13 | + console.log('=' .repeat(60)); |
| 14 | + console.log('📂 FILE-PER-OBJECT MODE DEMO'); |
| 15 | + console.log('=' .repeat(60) + '\n'); |
| 16 | + |
| 17 | + const dataDir = path.join(__dirname, '../data/file-per-object'); |
| 18 | + console.log(`📁 Using directory: ${dataDir}\n`); |
| 19 | + |
| 20 | + const driver = await ExcelDriver.create({ |
| 21 | + filePath: dataDir, |
| 22 | + fileStorageMode: 'file-per-object', |
| 23 | + createIfMissing: true, |
| 24 | + autoSave: true |
| 25 | + }); |
| 26 | + |
| 27 | + console.log('📝 Creating data in separate files...'); |
| 28 | + |
| 29 | + // Each object type will be stored in its own file |
| 30 | + await driver.create('customers', { |
| 31 | + name: 'ACME Corp', |
| 32 | + email: 'contact@acme.com', |
| 33 | + industry: 'Technology' |
| 34 | + }); |
| 35 | + console.log('✓ Created customer (saved to customers.xlsx)'); |
| 36 | + |
| 37 | + await driver.create('invoices', { |
| 38 | + number: 'INV-001', |
| 39 | + amount: 5000.00, |
| 40 | + status: 'paid' |
| 41 | + }); |
| 42 | + console.log('✓ Created invoice (saved to invoices.xlsx)'); |
| 43 | + |
| 44 | + await driver.create('tasks', { |
| 45 | + title: 'Review proposal', |
| 46 | + priority: 'high', |
| 47 | + status: 'in-progress' |
| 48 | + }); |
| 49 | + console.log('✓ Created task (saved to tasks.xlsx)'); |
| 50 | + |
| 51 | + console.log('\n📊 Summary:'); |
| 52 | + console.log(' - Each object type has its own Excel file'); |
| 53 | + console.log(' - Better for large datasets or independent objects'); |
| 54 | + console.log(' - Files: customers.xlsx, invoices.xlsx, tasks.xlsx\n'); |
| 55 | + |
| 56 | + await driver.disconnect(); |
| 57 | +} |
| 58 | + |
| 59 | +async function demoSingleFileMode() { |
| 60 | + console.log('=' .repeat(60)); |
| 61 | + console.log('📄 SINGLE-FILE MODE DEMO'); |
| 62 | + console.log('=' .repeat(60) + '\n'); |
13 | 63 |
|
14 | 64 | // Initialize the Excel driver |
15 | 65 | const dataPath = path.join(__dirname, '../data/demo.xlsx'); |
16 | 66 | console.log(`📁 Using Excel file: ${dataPath}\n`); |
17 | 67 |
|
18 | 68 | const driver = await ExcelDriver.create({ |
19 | 69 | filePath: dataPath, |
| 70 | + fileStorageMode: 'single-file', // Default mode (can be omitted) |
20 | 71 | createIfMissing: true, |
21 | 72 | autoSave: true |
22 | 73 | }); |
@@ -186,15 +237,41 @@ async function main() { |
186 | 237 | const finalProducts = await driver.find('products'); |
187 | 238 | console.log(`✓ Total products in Excel: ${finalProducts.length}`); |
188 | 239 |
|
| 240 | + console.log('\n📊 Summary:'); |
| 241 | + console.log(' - All object types in one Excel file'); |
| 242 | + console.log(' - Easy to manage (single file)'); |
| 243 | + console.log(' - Best for related data sets'); |
| 244 | + |
189 | 245 | // Clean up |
190 | 246 | await driver.disconnect(); |
191 | 247 |
|
192 | | - console.log('\n✅ Demo completed successfully!'); |
| 248 | + console.log('\n✅ Single-file mode demo completed!'); |
193 | 249 | console.log(`📁 Check the Excel file at: ${dataPath}\n`); |
194 | 250 | } |
195 | 251 |
|
| 252 | +async function main() { |
| 253 | + console.log('\n🚀 Excel Driver Demo - Storage Modes Comparison\n'); |
| 254 | + |
| 255 | + try { |
| 256 | + // Demo both storage modes |
| 257 | + await demoFilePerObjectMode(); |
| 258 | + await demoSingleFileMode(); |
| 259 | + |
| 260 | + console.log('=' .repeat(60)); |
| 261 | + console.log('✅ ALL DEMOS COMPLETED SUCCESSFULLY!'); |
| 262 | + console.log('=' .repeat(60)); |
| 263 | + console.log('\nYou can now:'); |
| 264 | + console.log(' 1. Open data/demo.xlsx to see single-file mode results'); |
| 265 | + console.log(' 2. Open data/file-per-object/*.xlsx to see file-per-object results'); |
| 266 | + console.log('\n'); |
| 267 | + } catch (error) { |
| 268 | + console.error('\n❌ Error:', error); |
| 269 | + process.exit(1); |
| 270 | + } |
| 271 | +} |
| 272 | + |
196 | 273 | // Run the demo |
197 | 274 | main().catch(error => { |
198 | | - console.error('❌ Error:', error.message); |
| 275 | + console.error('❌ Fatal error:', error.message); |
199 | 276 | process.exit(1); |
200 | 277 | }); |
0 commit comments