-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
76 lines (62 loc) · 2.16 KB
/
index.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
70
71
72
73
74
75
76
import { init, Ditto } from '@dittolive/ditto'
let ditto: Ditto
async function main () {
await init()
//go here to create an account
//https://portal.ditto.live
ditto = new Ditto({
type: 'onlinePlayground',
appID: '<YOUR_APP_ID>',
token: '<YOUR_TOKEN>'
})
ditto.startSync()
// For simple use of adding data
// Simple recursive example
setInterval(async () => {
try {
await ditto.store.execute(
"INSERT INTO pharm_orders DOCUMENTS (:newOrder)",
{ newOrder: generateRandomOrder() }
);
console.log("Task inserted successfully.");
} catch (error) {
console.error("Error inserting task:", error);
}
}, 10000); // Runs every 10 seconds
// Blade server to receive all updates for orders from small peers or big peer
ditto.sync.registerSubscription(`
SELECT *
FROM pharm_orders`
)
//if the blade server needs to run business logic on the data its receiving
ditto.store.registerObserver(`
SELECT *
FROM pharm_orders`,
(result) => {
result.items.forEach((element) => {
console.log(element.jsonString());
})
}
)
}
function generateRandomOrder(): { [key: string]: any } {
const priorities = ['High', 'Medium', 'Low'];
const orderPriority = priorities[Math.floor(Math.random() * priorities.length)];
const statuses = ['Pending', 'Shipped', 'Delivered', 'Canceled'];
const orderStatus = statuses[Math.floor(Math.random() * statuses.length)];
const customerNames = ['John Doe', 'Jane Smith', 'Mary Johnson', 'James Brown'];
const customerName = customerNames[Math.floor(Math.random() * customerNames.length)];
const items = ['Ibuprofen', 'Aspirin', 'Tylenol', 'Vitamin D', 'Cough Syrup'];
const item = items[Math.floor(Math.random() * items.length)];
const quantity = Math.floor(Math.random() * 5) + 1;
const randomDate = new Date();
randomDate.setDate(randomDate.getDate() - Math.floor(Math.random() * 7));
return {
orderPriority,
orderStatus,
customerName,
orderDate: randomDate.toISOString().split('T')[0],
items: [{ name: item, quantity }],
};
}
main()