|
| 1 | +/** |
| 2 | + * Excel Driver Demo |
| 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. |
| 6 | + */ |
| 7 | + |
| 8 | +import { ExcelDriver } from '@objectql/driver-excel'; |
| 9 | +import * as path from 'path'; |
| 10 | + |
| 11 | +async function main() { |
| 12 | + console.log('🚀 Excel Driver Demo\n'); |
| 13 | + |
| 14 | + // Initialize the Excel driver |
| 15 | + const dataPath = path.join(__dirname, '../data/demo.xlsx'); |
| 16 | + console.log(`📁 Using Excel file: ${dataPath}\n`); |
| 17 | + |
| 18 | + const driver = new ExcelDriver({ |
| 19 | + filePath: dataPath, |
| 20 | + createIfMissing: true, |
| 21 | + autoSave: true |
| 22 | + }); |
| 23 | + |
| 24 | + // ========== CRUD Operations ========== |
| 25 | + console.log('📝 Creating users...'); |
| 26 | + |
| 27 | + const user1 = await driver.create('users', { |
| 28 | + name: 'Alice Johnson', |
| 29 | + email: 'alice@example.com', |
| 30 | + role: 'admin', |
| 31 | + age: 30, |
| 32 | + department: 'Engineering' |
| 33 | + }); |
| 34 | + console.log('✓ Created:', user1.name, `(ID: ${user1.id})`); |
| 35 | + |
| 36 | + const user2 = await driver.create('users', { |
| 37 | + name: 'Bob Smith', |
| 38 | + email: 'bob@example.com', |
| 39 | + role: 'user', |
| 40 | + age: 25, |
| 41 | + department: 'Marketing' |
| 42 | + }); |
| 43 | + console.log('✓ Created:', user2.name, `(ID: ${user2.id})`); |
| 44 | + |
| 45 | + const user3 = await driver.create('users', { |
| 46 | + name: 'Charlie Davis', |
| 47 | + email: 'charlie@example.com', |
| 48 | + role: 'user', |
| 49 | + age: 35, |
| 50 | + department: 'Engineering' |
| 51 | + }); |
| 52 | + console.log('✓ Created:', user3.name, `(ID: ${user3.id})`); |
| 53 | + |
| 54 | + // Create products |
| 55 | + console.log('\n📦 Creating products...'); |
| 56 | + |
| 57 | + await driver.create('products', { |
| 58 | + name: 'Laptop Pro', |
| 59 | + price: 1299.99, |
| 60 | + category: 'Electronics', |
| 61 | + stock: 50 |
| 62 | + }); |
| 63 | + console.log('✓ Created: Laptop Pro'); |
| 64 | + |
| 65 | + await driver.create('products', { |
| 66 | + name: 'Wireless Mouse', |
| 67 | + price: 29.99, |
| 68 | + category: 'Accessories', |
| 69 | + stock: 200 |
| 70 | + }); |
| 71 | + console.log('✓ Created: Wireless Mouse'); |
| 72 | + |
| 73 | + // ========== Query Operations ========== |
| 74 | + console.log('\n🔍 Querying data...'); |
| 75 | + |
| 76 | + // Find all users |
| 77 | + const allUsers = await driver.find('users'); |
| 78 | + console.log(`\n✓ Found ${allUsers.length} users total`); |
| 79 | + |
| 80 | + // Filter by role |
| 81 | + const admins = await driver.find('users', { |
| 82 | + filters: [['role', '=', 'admin']] |
| 83 | + }); |
| 84 | + console.log(`✓ Found ${admins.length} admin(s):`, admins.map(u => u.name).join(', ')); |
| 85 | + |
| 86 | + // Filter by age |
| 87 | + const youngUsers = await driver.find('users', { |
| 88 | + filters: [['age', '<', 30]] |
| 89 | + }); |
| 90 | + console.log(`✓ Found ${youngUsers.length} user(s) under 30:`, youngUsers.map(u => u.name).join(', ')); |
| 91 | + |
| 92 | + // Filter by department |
| 93 | + const engineers = await driver.find('users', { |
| 94 | + filters: [['department', '=', 'Engineering']] |
| 95 | + }); |
| 96 | + console.log(`✓ Found ${engineers.length} engineer(s):`, engineers.map(u => u.name).join(', ')); |
| 97 | + |
| 98 | + // Search by name |
| 99 | + const searchResults = await driver.find('users', { |
| 100 | + filters: [['name', 'contains', 'li']] |
| 101 | + }); |
| 102 | + console.log(`✓ Search "li" found ${searchResults.length} user(s):`, searchResults.map(u => u.name).join(', ')); |
| 103 | + |
| 104 | + // Sort users by age |
| 105 | + const sortedByAge = await driver.find('users', { |
| 106 | + sort: [['age', 'desc']] |
| 107 | + }); |
| 108 | + console.log('✓ Users sorted by age (desc):', sortedByAge.map(u => `${u.name} (${u.age})`).join(', ')); |
| 109 | + |
| 110 | + // Pagination |
| 111 | + const pagedUsers = await driver.find('users', { |
| 112 | + limit: 2, |
| 113 | + skip: 1 |
| 114 | + }); |
| 115 | + console.log(`✓ Page 2 (limit 2, skip 1):`, pagedUsers.map(u => u.name).join(', ')); |
| 116 | + |
| 117 | + // Count |
| 118 | + const userCount = await driver.count('users', {}); |
| 119 | + console.log(`✓ Total user count: ${userCount}`); |
| 120 | + |
| 121 | + // Distinct values |
| 122 | + const departments = await driver.distinct('users', 'department'); |
| 123 | + console.log('✓ Distinct departments:', departments.join(', ')); |
| 124 | + |
| 125 | + // ========== Update Operations ========== |
| 126 | + console.log('\n✏️ Updating records...'); |
| 127 | + |
| 128 | + await driver.update('users', user1.id, { |
| 129 | + email: 'alice.johnson@example.com', |
| 130 | + age: 31 |
| 131 | + }); |
| 132 | + console.log(`✓ Updated ${user1.name}'s email and age`); |
| 133 | + |
| 134 | + // Update many |
| 135 | + const updateResult = await driver.updateMany( |
| 136 | + 'users', |
| 137 | + [['department', '=', 'Engineering']], |
| 138 | + { department: 'Tech' } |
| 139 | + ); |
| 140 | + console.log(`✓ Updated ${updateResult.modifiedCount} user(s) department to Tech`); |
| 141 | + |
| 142 | + // ========== Query Updated Data ========== |
| 143 | + console.log('\n🔄 After updates...'); |
| 144 | + const updatedUser1 = await driver.findOne('users', user1.id); |
| 145 | + console.log(`✓ ${updatedUser1.name}: age=${updatedUser1.age}, email=${updatedUser1.email}`); |
| 146 | + |
| 147 | + const techUsers = await driver.find('users', { |
| 148 | + filters: [['department', '=', 'Tech']] |
| 149 | + }); |
| 150 | + console.log(`✓ Users in Tech: ${techUsers.map(u => u.name).join(', ')}`); |
| 151 | + |
| 152 | + // ========== Multiple Worksheets ========== |
| 153 | + console.log('\n📊 Multiple worksheets (object types)...'); |
| 154 | + const products = await driver.find('products'); |
| 155 | + console.log(`✓ Found ${products.length} products in separate worksheet`); |
| 156 | + products.forEach(p => { |
| 157 | + console.log(` - ${p.name}: $${p.price} (${p.stock} in stock)`); |
| 158 | + }); |
| 159 | + |
| 160 | + // ========== Delete Operations ========== |
| 161 | + console.log('\n🗑️ Deleting records...'); |
| 162 | + |
| 163 | + await driver.delete('users', user2.id); |
| 164 | + console.log(`✓ Deleted ${user2.name}`); |
| 165 | + |
| 166 | + const finalCount = await driver.count('users', {}); |
| 167 | + console.log(`✓ Remaining users: ${finalCount}`); |
| 168 | + |
| 169 | + // ========== Bulk Operations ========== |
| 170 | + console.log('\n📦 Bulk operations...'); |
| 171 | + |
| 172 | + const newUsers = await driver.createMany('users', [ |
| 173 | + { name: 'Diana Prince', email: 'diana@example.com', role: 'user', age: 28 }, |
| 174 | + { name: 'Ethan Hunt', email: 'ethan@example.com', role: 'admin', age: 40 } |
| 175 | + ]); |
| 176 | + console.log(`✓ Created ${newUsers.length} users in bulk`); |
| 177 | + |
| 178 | + // ========== Final Summary ========== |
| 179 | + console.log('\n📈 Final Summary:'); |
| 180 | + const finalUsers = await driver.find('users'); |
| 181 | + console.log(`✓ Total users in Excel: ${finalUsers.length}`); |
| 182 | + finalUsers.forEach((user, index) => { |
| 183 | + console.log(` ${index + 1}. ${user.name} (${user.role}, ${user.age} years old)`); |
| 184 | + }); |
| 185 | + |
| 186 | + const finalProducts = await driver.find('products'); |
| 187 | + console.log(`✓ Total products in Excel: ${finalProducts.length}`); |
| 188 | + |
| 189 | + // Clean up |
| 190 | + await driver.disconnect(); |
| 191 | + |
| 192 | + console.log('\n✅ Demo completed successfully!'); |
| 193 | + console.log(`📁 Check the Excel file at: ${dataPath}\n`); |
| 194 | +} |
| 195 | + |
| 196 | +// Run the demo |
| 197 | +main().catch(error => { |
| 198 | + console.error('❌ Error:', error.message); |
| 199 | + process.exit(1); |
| 200 | +}); |
0 commit comments