-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-example.ts
More file actions
184 lines (157 loc) Β· 6.21 KB
/
advanced-example.ts
File metadata and controls
184 lines (157 loc) Β· 6.21 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import createSqlify from '../src';
import { Pool } from 'pg'; // For example purposes
// Mock database connection (in real app, use your DB library)
const mockDb = {
query: async (sql: string, params?: any[]) => {
console.log('π Executing query:');
console.log(sql);
if (params?.length) {
console.log('π With parameters:', params);
}
// Simulate query results based on the query type
if (sql.includes('ORDER_TOTAL')) {
return [
{ user_id: 1, username: 'john_doe', order_count: 5, total_amount: 530.75, last_order_date: new Date('2025-03-15') },
{ user_id: 2, username: 'jane_smith', order_count: 3, total_amount: 290.50, last_order_date: new Date('2025-04-02') }
];
} else if (sql.includes('PRODUCT_REVIEWS')) {
return [
{ id: 101, product_name: 'Wireless Headphones', avg_rating: 4.7, review_count: 120 },
{ id: 102, product_name: 'Smart Watch', avg_rating: 4.2, review_count: 85 }
];
} else {
return [{ result: 'No specific data available for this query' }];
}
}
};
// Initialize our SQL utility
const sql = createSqlify(mockDb);
// ========================================================
// βοΈ Advanced Example: Combining Views, Dynamic Filters, JS Expressions
// ========================================================
console.log('π SQL-TEMPLATE-JS ADVANCED EXAMPLE π\n');
// ---------------------------------------------
// 1. Create a complex view with analytics data
// ---------------------------------------------
// In a real app, this would be loaded from a file
const orderAnalyticsView = `
-- Complex view for order analytics
CREATE VIEW "ORDER_TOTAL" AS
SELECT
u.id AS user_id,
u.username,
COUNT(o.id) AS order_count,
SUM(o.total) AS total_amount,
MAX(o.created_at) AS last_order_date
FROM "users" u
LEFT JOIN "orders" o ON u.id = o.user_id
GROUP BY u.id, u.username
`;
// ---------------------------------------------
// 2. Create product reviews view
// ---------------------------------------------
const productReviewsView = `
-- View for product reviews with average ratings
CREATE VIEW "PRODUCT_REVIEWS" AS
SELECT
p.id,
p.name AS product_name,
AVG(r.rating) AS avg_rating,
COUNT(r.id) AS review_count
FROM "products" p
LEFT JOIN "reviews" r ON p.id = r.product_id
GROUP BY p.id, p.name
`;
// ---------------------------------------------
// 3. Create our model classes from the views
// ---------------------------------------------
const OrderAnalytics = sql.fromFile = (() => {
// This is just for example - in a real app use sql.fromFile
const content = orderAnalyticsView;
return createSqlify(mockDb).createQueryFromSchema(content, "ORDER_TOTAL");
})();
const ProductReviews = sql.fromFile = (() => {
// This is just for example - in a real app use sql.fromFile
const content = productReviewsView;
return createSqlify(mockDb).createQueryFromSchema(content, "PRODUCT_REVIEWS");
})();
// ---------------------------------------------
// 4. Dynamic filter builder with JS expressions
// ---------------------------------------------
// Create a dynamic filter builder function
const createFilter = (field: string, operator: string) => {
return (props: any) => `${field} ${operator} $${props.paramName}`;
};
// ---------------------------------------------
// 5. Create advanced queries with dynamic parts
// ---------------------------------------------
// Dynamic order analytics query with conditional filters
const getOrderAnalytics = sql`
SELECT * FROM "ORDER_TOTAL"
WHERE
${props => props.minTotal ? createFilter('total_amount', '>=')(props) : '1=1'}
AND ${props => props.afterDate ? createFilter('last_order_date', '>=')(props) : '1=1'}
AND ${props => props.userId ? `user_id = $(userId):integer` : '1=1'}
ORDER BY ${props => props.orderBy || 'total_amount'} ${props => props.orderDirection || 'DESC'}
LIMIT $(limit):integer
`;
// Dynamic product query with configurable rating threshold
const getTopProducts = sql`
SELECT * FROM "PRODUCT_REVIEWS"
WHERE
${props => props.minRating ? `avg_rating >= $(minRating):float` : '1=1'}
AND review_count >= $(minReviews):integer
ORDER BY avg_rating DESC, review_count DESC
LIMIT $(limit):integer
`;
// ---------------------------------------------
// 6. Run our queries with different parameters
// ---------------------------------------------
async function demo() {
console.log('π DEMO 1: Order Analytics');
console.log('---------------------------');
// Query 1: Get big spenders (>$500) with custom ordering
console.log('π§Ύ Query 1: Big spenders (>$500) ordered by username');
const bigSpenders = await getOrderAnalytics({
minTotal: true,
paramName: 'minTotalAmount',
minTotalAmount: 500,
orderBy: 'username',
orderDirection: 'ASC',
limit: 10
});
console.log('π Result:', JSON.stringify(bigSpenders, null, 2), '\n');
// Query 2: Get recent orders since March 2025
console.log('π§Ύ Query 2: Recent orders since March 2025');
const recentOrders = await getOrderAnalytics({
afterDate: true,
paramName: 'startDate',
startDate: new Date('2025-03-01'),
limit: 5
});
console.log('π Result:', JSON.stringify(recentOrders, null, 2), '\n');
// Query 3: Get orders for a specific user
console.log('π§Ύ Query 3: Orders for user ID 1');
const userOrders = await getOrderAnalytics({
userId: 1,
limit: 10
});
console.log('π Result:', JSON.stringify(userOrders, null, 2), '\n');
// Query 4: Use the view's default methods
console.log('π§Ύ Query 4: Using the view\'s standard methods');
const allAnalytics = await OrderAnalytics.find();
console.log('π Result:', JSON.stringify(allAnalytics, null, 2), '\n');
console.log('\nπ DEMO 2: Product Reviews');
console.log('---------------------------');
// Query 5: Get top-rated products with at least 50 reviews
console.log('π§Ύ Query 5: Top-rated products (4.5+) with 50+ reviews');
const topProducts = await getTopProducts({
minRating: 4.5,
minReviews: 50,
limit: 5
});
console.log('π Result:', JSON.stringify(topProducts, null, 2), '\n');
}
// Run the demo
console.log('Starting advanced SQL-template-js demo...\n');
demo().catch(console.error);