Skip to content

Commit 29b1aea

Browse files
Copilothotlong
andcommitted
Add Excel Driver demo example with working implementation
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f19a93d commit 29b1aea

7 files changed

Lines changed: 312 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Generated Excel files
2+
data/*.xlsx
3+
data/*.xls
4+
5+
# Build output
6+
dist/
7+
8+
# Dependencies
9+
node_modules/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Excel Driver Demo
2+
3+
This example demonstrates the Excel Driver for ObjectQL.
4+
5+
## Installation
6+
7+
From the repository root:
8+
9+
```bash
10+
pnpm install
11+
```
12+
13+
## Running the Demo
14+
15+
```bash
16+
cd examples/drivers/excel-demo
17+
pnpm start
18+
```
19+
20+
## What This Demo Shows
21+
22+
1. **Creating Records** - Add users and products to Excel
23+
2. **Querying Data** - Filter, sort, search, and paginate
24+
3. **Updating Records** - Modify individual and bulk records
25+
4. **Deleting Records** - Remove records from Excel
26+
5. **Multiple Worksheets** - Separate sheets for different object types
27+
6. **Bulk Operations** - Create multiple records at once
28+
29+
## Output
30+
31+
The demo will:
32+
- Create an Excel file at `data/demo.xlsx`
33+
- Populate it with sample users and products
34+
- Demonstrate various query operations
35+
- Show the final state of the data
36+
37+
## Excel File Structure
38+
39+
After running, `data/demo.xlsx` will contain:
40+
41+
**Sheet: users**
42+
| id | name | email | role | age | department | created_at | updated_at |
43+
|----|------|-------|------|-----|------------|------------|------------|
44+
| ... | Alice Johnson | alice.johnson@... | admin | 31 | Tech | ... | ... |
45+
46+
**Sheet: products**
47+
| id | name | price | category | stock | created_at | updated_at |
48+
|----|------|-------|----------|-------|------------|------------|
49+
| ... | Laptop Pro | 1299.99 | Electronics | 50 | ... | ... |
50+
51+
## Next Steps
52+
53+
- Modify `src/index.ts` to experiment with different queries
54+
- Try adding your own object types
55+
- Explore filter operators and sorting options
56+
- Check the [Excel Driver README](../../../packages/drivers/excel/README.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@objectql/example-excel-demo",
3+
"version": "0.1.0",
4+
"private": true,
5+
"description": "Example demonstrating the Excel Driver for ObjectQL",
6+
"scripts": {
7+
"start": "ts-node src/index.ts",
8+
"build": "tsc"
9+
},
10+
"dependencies": {
11+
"@objectql/driver-excel": "workspace:*",
12+
"@objectql/types": "workspace:*"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^20.10.0",
16+
"ts-node": "^10.9.0",
17+
"typescript": "^5.0.0"
18+
}
19+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"rootDir": "./src"
6+
},
7+
"include": ["src/**/*"]
8+
}

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ packages:
66
- examples/quickstart/*
77
- examples/integrations/*
88
- examples/showcase/*
9+
- examples/drivers/*
910

1011
onlyBuiltDependencies:
1112
- esbuild

0 commit comments

Comments
 (0)