-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTable.cs
More file actions
150 lines (128 loc) · 4.44 KB
/
Table.cs
File metadata and controls
150 lines (128 loc) · 4.44 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace PowerSync.Common.DB.Schema;
using Newtonsoft.Json;
using PowerSync.Common.DB.Schema.Attributes;
public class TableOptions(
Dictionary<string, List<string>>? indexes = null,
bool? localOnly = null,
bool? insertOnly = null,
string? viewName = null,
bool? trackMetadata = null,
TrackPreviousOptions? trackPreviousValues = null,
bool? ignoreEmptyUpdates = null
)
{
public Dictionary<string, List<string>> Indexes { get; set; } = indexes ?? [];
public bool LocalOnly { get; set; } = localOnly ?? false;
public bool InsertOnly { get; set; } = insertOnly ?? false;
public string? ViewName { get; set; } = viewName;
/// <summary>
/// Whether to add a hidden `_metadata` column that will be enabled for updates to attach custom
/// information about writes that will be reported through [CrudEntry.metadata].
/// </summary>
public bool TrackMetadata { get; set; } = trackMetadata ?? false;
/// <summary>
/// When set to a non-null value, track old values of columns
/// </summary>
public TrackPreviousOptions? TrackPreviousValues { get; set; } = trackPreviousValues;
/// <summary>
/// Whether an `UPDATE` statement that doesn't change any values should be ignored when creating
/// CRUD entries.
/// </summary>
public bool IgnoreEmptyUpdates { get; set; } = ignoreEmptyUpdates ?? false;
}
/// <summary>
/// Whether to include previous column values when PowerSync tracks local changes.
/// Including old values may be helpful for some backend connector implementations,
/// which is why it can be enabled on a per-table or per-column basis.
/// </summary>
public class TrackPreviousOptions
{
/// <summary>
/// When defined, a list of column names for which old values should be tracked.
/// </summary>
[JsonProperty("columns")]
public List<string>? Columns { get; set; }
/// <summary>
/// When enabled, only include values that have actually been changed by an update.
/// </summary>
[JsonProperty("onlyWhenChanged")]
public bool? OnlyWhenChanged { get; set; }
}
public class Table
{
public const int MAX_AMOUNT_OF_COLUMNS = 1999;
public Dictionary<string, ColumnType> Columns { get; set; }
public TableOptions Options { get; set; }
public string Name { get; set; } = null!;
// Accessors
public Dictionary<string, List<string>> Indexes
{
get { return Options.Indexes; }
set { Options.Indexes = value; }
}
public bool LocalOnly
{
get { return Options.LocalOnly; }
set { Options.LocalOnly = value; }
}
public bool InsertOnly
{
get { return Options.InsertOnly; }
set { Options.InsertOnly = value; }
}
public string? ViewName
{
get { return Options.ViewName; }
set { Options.ViewName = value; }
}
public bool TrackMetadata
{
get { return Options.TrackMetadata; }
set { Options.TrackMetadata = value; }
}
public TrackPreviousOptions? TrackPreviousValues
{
get { return Options.TrackPreviousValues; }
set { Options.TrackPreviousValues = value; }
}
public bool IgnoreEmptyUpdates
{
get { return Options.IgnoreEmptyUpdates; }
set { Options.IgnoreEmptyUpdates = value; }
}
public Table()
{
Columns = new Dictionary<string, ColumnType>();
Options = new TableOptions();
}
public Table(Type type, TableOptions? options = null)
{
var parser = new AttributeParser(type);
Name = parser.TableName;
Columns = parser.ParseColumns();
Options = options ?? parser.ParseTableOptions();
parser.RegisterDapperTypeMap();
}
public Table(Table other, TableOptions? options = null)
{
if (other == null) throw new ArgumentNullException(nameof(other));
Name = other.Name;
Columns = other.Columns;
Options = options ?? other.Options;
}
// Mirrors the legacy syntax, as well as the syntax found in the other SDKs.
public Table(string name, Dictionary<string, ColumnType> columns, TableOptions? options = null)
{
Name = name;
Columns = columns;
Options = options ?? new TableOptions();
}
internal CompiledTable Compile()
{
if (string.IsNullOrWhiteSpace(Name))
{
throw new InvalidOperationException("Table name is required.");
}
return new CompiledTable(Name, Columns, Options);
}
}