-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.cs
41 lines (34 loc) · 1.08 KB
/
Schema.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
namespace PowerSync.Common.DB.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Schema(Dictionary<string, Table> tables)
{
private readonly Dictionary<string, Table> Tables = tables;
public void Validate()
{
foreach (var kvp in Tables)
{
var tableName = kvp.Key;
var table = kvp.Value;
if (Table.InvalidSQLCharacters.IsMatch(tableName))
{
throw new Exception($"Invalid characters in table name: {tableName}");
}
table.Validate();
}
}
public string ToJSON()
{
var jsonObject = new
{
tables = Tables.Select(kv =>
{
var json = JObject.Parse(kv.Value.ToJSON(kv.Key));
var orderedJson = new JObject { ["name"] = kv.Key };
orderedJson.Merge(json, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });
return orderedJson;
}).ToList()
};
return JsonConvert.SerializeObject(jsonObject);
}
}