-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-bundle.ts
More file actions
98 lines (83 loc) · 2.75 KB
/
query-bundle.ts
File metadata and controls
98 lines (83 loc) · 2.75 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
import { LyraBundle, type LyraQuery, type LyraResult } from '../../dist/index.js';
import { readFileSync } from 'fs';
import { join } from 'path';
type Ticket = {
id: string;
customer: string;
priority: string;
status: string;
productArea: string;
createdAt: string;
amount: number;
};
async function main() {
// Load bundle from JSON file
const bundlePath = join(__dirname, 'bundle.json');
const rawBundle = readFileSync(bundlePath, 'utf-8');
const bundleData = JSON.parse(rawBundle);
console.log('Loading bundle...');
const bundle = LyraBundle.load<Ticket>(bundleData);
console.log(`Loaded bundle with ${bundle.describe().fields.length} fields\n`);
// Example 1: Simple equality query
console.log('=== Example 1: Equality Query ===');
const equalityQuery: LyraQuery = {
equal: {
status: 'open',
priority: 'high',
},
limit: 10,
};
const equalityResult: LyraResult<Ticket> = bundle.query(equalityQuery);
console.log(`Found ${equalityResult.total} tickets matching status=open AND priority=high`);
console.log('Items:', equalityResult.items.map((t) => t.id));
console.log();
// Example 2: Range query
console.log('=== Example 2: Range Query ===');
const now = Date.now();
const oneWeekAgo = now - 7 * 24 * 60 * 60 * 1000;
const rangeQuery: LyraQuery = {
ranges: {
createdAt: { min: oneWeekAgo, max: now },
amount: { min: 1000 },
},
};
const rangeResult = bundle.query(rangeQuery);
console.log(`Found ${rangeResult.total} tickets created in last week with amount >= 1000`);
console.log('Items:', rangeResult.items.map((t) => `${t.id} (${t.amount})`));
console.log();
// Example 3: Query with facet counts
console.log('=== Example 3: Query with Facet Counts ===');
const countsQuery: LyraQuery = {
equal: {
customer: 'Acme Corp',
},
includeFacetCounts: true,
};
const countsResult = bundle.query(countsQuery);
console.log(`Found ${countsResult.total} tickets for Acme Corp`);
console.log('Facet counts:');
if (countsResult.facets) {
for (const [field, counts] of Object.entries(countsResult.facets)) {
console.log(` ${field}:`, counts);
}
}
console.log();
// Example 4: Combined query
console.log('=== Example 4: Combined Equality + Range Query ===');
const combinedQuery: LyraQuery = {
equal: {
status: 'open',
},
ranges: {
amount: { min: 1500 },
},
limit: 5,
};
const combinedResult = bundle.query(combinedQuery);
console.log(`Found ${combinedResult.total} open tickets with amount >= 1500`);
console.log('Items:', combinedResult.items.map((t) => `${t.id} - ${t.customer} - $${t.amount}`));
}
main().catch((error) => {
console.error('Error querying bundle:', error);
process.exit(1);
});