-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdatabase-client.ts
52 lines (40 loc) · 1.31 KB
/
database-client.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
// TODO: Remove once global `EventSource` makes it out of experimental
// in nodejs LTS.
import EventSource from "eventsource";
import { fetchJSON } from "@skipruntime/helpers";
/*
This is the client simulator of database example
*/
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const port = 8082;
const url = `http://localhost:${port.toString()}`;
console.log("Connect to replication server for resource /users");
const evSource = new EventSource(`${url}/users`);
evSource.addEventListener("init", (e: MessageEvent<string>) => {
const initial_data = JSON.parse(e.data);
console.log("Init", initial_data);
});
evSource.addEventListener("update", (e: MessageEvent<string>) => {
const updates = JSON.parse(e.data);
console.log("Update", updates);
});
evSource.onerror = (e) => {
console.log(e);
};
await sleep(1000);
console.log('Set /user/123 to { name: "daniel", country: "UK" }');
await fetchJSON(
`${url}/user/123`,
"PUT",
{},
{ name: "daniel", country: "UK" },
);
console.log("Get /user/123", (await fetchJSON(`${url}/user/123`, "GET"))[0]);
await sleep(1000);
console.log("Delete /user/123");
await fetchJSON(`${url}/user/123`, "DELETE", {});
console.log("Get /user/123", (await fetchJSON(`${url}/user/123`, "GET"))[0]);
await sleep(1000);
evSource.close();