-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-usage.js
More file actions
66 lines (55 loc) · 2.16 KB
/
Copy pathquery-usage.js
File metadata and controls
66 lines (55 loc) · 2.16 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
// query-usage.js — SQL Analytics with Streamline WASM SDK
//
// Demonstrates using Streamline's SQL query capabilities from the browser.
// Queries are executed via the HTTP REST API (not WebSocket).
//
// Usage:
// 1. Start Streamline: docker run -p 9092:9092 -p 9094:9094 ghcr.io/streamlinelabs/streamline:0.2.0
// 2. Include in a bundler or HTML page.
async function main() {
const httpUrl = 'http://localhost:9094';
// Initialize WASM SDK for producing sample data
const wasm = await import('@streamlinelabs/streamline-wasm');
await wasm.default();
const client = new wasm.StreamlineClient('ws://localhost:9094/ws');
await client.connect();
// Create topic and produce sample data
try {
await client.create_topic('query-demo', 1);
} catch (e) { /* topic may exist */ }
console.log('Producing sample events...');
for (let i = 0; i < 10; i++) {
await client.produce('query-demo', JSON.stringify({
user: `user-${i}`,
action: i % 2 === 0 ? 'click' : 'scroll',
value: i * 10,
ts: new Date().toISOString(),
}));
}
console.log('✅ Produced 10 events\n');
// Execute SQL queries via the REST API
async function query(sql) {
const resp = await fetch(`${httpUrl}/v1/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: sql }),
});
if (!resp.ok) throw new Error(`Query failed: ${resp.status} ${resp.statusText}`);
return resp.json();
}
// Query 1: Select all events
console.log('--- All events (LIMIT 5) ---');
const all = await query("SELECT * FROM topic('query-demo') LIMIT 5");
console.table(all.rows || all);
// Query 2: Count by action
console.log('\n--- Count by action ---');
const counts = await query("SELECT action, COUNT(*) as cnt FROM topic('query-demo') GROUP BY action");
console.table(counts.rows || counts);
// Query 3: Filter by value
console.log('\n--- Events with value > 50 ---');
const filtered = await query("SELECT user, value FROM topic('query-demo') WHERE value > 50");
console.table(filtered.rows || filtered);
client.disconnect();
console.log('\n✅ Done');
}
main().catch(console.error);