-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trial.cs
117 lines (104 loc) · 5.28 KB
/
Trial.cs
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
using System;
using System.Linq;
using System.Threading.Tasks;
using Friflo.Json.Fliox.Hub.Client;
using Friflo.Json.Fliox.Hub.Host;
using Friflo.Json.Fliox.Hub.Remote;
using Friflo.Json.Fliox.Hub.SQLite;
namespace TodoTest;
internal static class Trial
{
// custom entry point to run test snippets with: dotnet run
internal static async Task Main(string[] args)
{
// await DirectDatabaseExample();
// await RemoteDatabaseExample();
await QueryAll(args);
await SubscribeChangesAndMessages();
}
// Example used at: https://github.com/friflo/Friflo.Json.Fliox#direct-database-access
private static async Task DirectDatabaseExample() {
var schema = DatabaseSchema.Create<TodoClient>();
var database = new SQLiteDatabase("todo_db", "Data Source=todo.sqlite3", schema);
await database.SetupDatabaseAsync(); // for development: create database or update ist schema
var hub = new FlioxHub(database);
var client = new TodoClient(hub);
client.Jobs.UpsertRange(new[] {
new Job { id = 1, title = "Buy milk", completed = true },
new Job { id = 2, title = "Buy cheese", completed = false }
});
var jobs = client.Jobs.Query(job => job.completed == true);
await client.SyncTasks(); // execute UpsertRange & Query task
foreach (var job in jobs.Result) {
Console.WriteLine($"{job.id}: {job.title}");
}
// output: 1: Buy milk
}
// Example used at: https://github.com/friflo/Friflo.Json.Fliox#remote-database-access
private static async Task RemoteDatabaseExample() {
var hub = new WebSocketClientHub("todo_db", "ws://localhost:5000/fliox/");
var client = new TodoClient(hub);
var jobs = client.Jobs.Query(job => job.completed == true);
client.Jobs.SubscribeChanges(Change.All, (changes, context) => {
Console.WriteLine(changes);
});
await client.SyncTasks(); // execute Query & SubscribeChanges task
foreach (var job in jobs.Result) {
Console.WriteLine($"{job.id}: {job.title}");
}
// output: 1: Buy milk
Console.WriteLine("\n wait for events ... (exit with: CTRL + C)\n note: generate events by clicking 'Save' on a record in the Hub Explorer\n");
await Task.Delay(3_600_000); // wait 1 hour
}
private static async Task QueryAll(string[] args)
{
var option = args.FirstOrDefault() ?? "http";
var hub = CreateHub(option);
var client = new TodoClient(hub);
var jobs = client.Jobs.QueryAll();
await client.SyncTasks();
Console.WriteLine($"\n--- jobs:");
foreach (var job in jobs.Result) {
Console.WriteLine($"id: {job.id}, title: {job.title}, completed: {job.completed}");
}
}
// after calling this method open: 'Hub Explorer > main_db > articles'
// changing records in 'articles' trigger the subscription handler in this method.
private static async Task SubscribeChangesAndMessages()
{
var hub = CreateHub("ws");
var client = new TodoClient(hub) { UserId = "admin", Token = "admin" };
client.Jobs.SubscribeChanges(Change.All, (changes, context) => {
foreach (var upsert in changes.Upserts) {
var job = upsert.entity;
Console.WriteLine($"EventSeq: {context.EventSeq} - upsert job: {job.id}, name: {job.title}");
}
foreach (var key in changes.Deletes) {
Console.WriteLine($"EventSeq: {context.EventSeq} - delete job: {key}");
}
});
// subscribe all messages
client.SubscribeMessage("*", (message, context) => {
Console.WriteLine($"EventSeq: {context.EventSeq} - message: {message}");
});
client.SubscribeMessage<string>("TestMessage", (message, context) => {
message.GetParam (out string param, out _);
Console.WriteLine($"EventSeq: {context.EventSeq} - TestMessage ('{param}')");
});
await client.SyncTasks();
Console.WriteLine("\n wait for events ... (exit with: CTRL + C)\n note: generate events by clicking 'Save' on a record in the Hub Explorer\n");
await Task.Delay(3_600_000); // wait 1 hour
}
private static readonly DatabaseSchema Schema = DatabaseSchema.Create<TodoClient>();
private static FlioxHub CreateHub(string option)
{
switch (option) {
case "http": return new HttpClientHub ("todo_db", "http://localhost:5000/fliox/");
case "ws": return new WebSocketClientHub ("todo_db", "ws://localhost:5000/fliox/");
case "memory": return new FlioxHub(new MemoryDatabase ("todo_db", Schema));
case "file": return new FlioxHub(new FileDatabase ("todo_db", "../Test/DB/main_db", Schema));
case "sqlite": return new FlioxHub(new SQLiteDatabase ("todo_db", "Data Source=todo.sqlite3", Schema));
}
throw new InvalidOperationException($"unknown option: '{option}' use: [http, ws, file, memory]");
}
}