-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubscribe.ts
69 lines (60 loc) · 1.85 KB
/
subscribe.ts
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
import { Client } from "https://deno.land/x/postgres/mod.ts";
import { Update } from "./state";
import State, { Update } from "./state.ts";
const client = new Client({
user: "MATERIALIZE_USERNAME",
database: "materialize",
password: "APP_SPECIFIC_PASSWORD",
hostname: "MATERIALIZE_HOST",
port: 6875,
ssl: true,
});
interface CounterSum {
sum: number;
}
const main = async ({ response }: { response: any }) => {
try {
await client.connect()
await client.queryObject('BEGIN');
await client.queryObject('DECLARE c CURSOR FOR SUBSCRIBE (SELECT sum FROM counter_sum) WITH (PROGRESS);');
while (true) {
let buffer: Array<Update<CounterSum>> = [];
const state = new State<CounterSum>();
// Loop indefinitely
while (true) {
const { rows } = await client.queryObject('FETCH ALL c');
rows.forEach(row => {
// Map row fields
const {
mz_timestamp: ts,
mz_progressed: progress,
mz_diff: diff,
sum,
} = row;
// When a progress is detected, get the last values
if (progress) {
if (buffer.length > 0) {
try {
state.update(buffer, ts);
} catch (err) {
console.error(err);
} finally {
console.log("State: ", state.getState());
buffer.splice(0, buffer.length);
}
}
} else {
buffer.push({ value: { sum }, diff });
}
});
}
}
} catch (err) {
console.error(err.toString())
} finally {
await client.end()
}
}
export { main }
// Call the main function
main({ response: {} })