Skip to content

Commit cc9eb26

Browse files
Added comments to the SupabasePatchHelper and PATCH Event in SupabaseConnector
1 parent dc58656 commit cc9eb26

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

Diff for: demos/CommandLine/Helpers/SupabasePatchHelper.cs

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,32 @@ namespace CommandLine.Helpers;
77

88
public static class SupabasePatchHelper
99
{
10+
// Applies a "SET" operation to the table, setting the value of a specific property.
1011
public static IPostgrestTable<T> ApplySet<T>(
11-
IPostgrestTable<T> table,
12-
string jsonPropertyName,
13-
object value
14-
) where T : BaseModel, new()
12+
IPostgrestTable<T> table, // The table to apply the operation to
13+
string jsonPropertyName, // The name of the JSON property to update
14+
object value // The new value to set for the property
15+
) where T : BaseModel, new() // Ensures T is a subclass of BaseModel with a parameterless constructor
1516
{
16-
// Find the property that matches the JsonProperty name
17+
// Find the property on the model that matches the JSON property name
1718
var property = typeof(T)
18-
.GetProperties()
19+
.GetProperties() // Get all properties of the model type
1920
.FirstOrDefault(p =>
21+
// Check if the property has a JsonPropertyAttribute
2022
p.GetCustomAttributes(typeof(JsonPropertyAttribute), true)
2123
.FirstOrDefault() is JsonPropertyAttribute attr &&
22-
attr.PropertyName == jsonPropertyName);
24+
attr.PropertyName == jsonPropertyName); // Check if the JSON property name matches
2325

2426
if (property == null)
2527
throw new ArgumentException($"'{jsonPropertyName}' is not a valid property on type '{typeof(T).Name}'");
2628

27-
var parameter = Expression.Parameter(typeof(T), "x");
28-
var propertyAccess = Expression.Property(parameter, property.Name);
29-
var converted = Expression.Convert(propertyAccess, typeof(object));
30-
var lambda = Expression.Lambda<Func<T, object>>(converted, parameter);
29+
// Create an expression to access the specified property on the model
30+
var parameter = Expression.Parameter(typeof(T), "x"); // Define a parameter for the expression
31+
var propertyAccess = Expression.Property(parameter, property.Name); // Access the property
32+
var converted = Expression.Convert(propertyAccess, typeof(object)); // Convert the value to object type
33+
var lambda = Expression.Lambda<Func<T, object>>(converted, parameter); // Create a lambda expression for the property
3134

35+
// Apply the "SET" operation to the table using the lambda expression
3236
return table.Set(lambda, value);
3337
}
34-
}
38+
}

Diff for: demos/CommandLine/SupabaseConnector.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,33 @@ public async Task UploadData(IPowerSyncDatabase database)
108108

109109
if (op.Table.ToLower().Trim() == "lists")
110110
{
111+
// Create an update query for the 'Todo' table where the 'Id' matches 'op.Id'
111112
IPostgrestTable<List> updateQuery = _supabase
112113
.From<List>()
113114
.Where(x => x.Id == op.Id);
114115

116+
// Loop through each key-value pair in the operation data (op.OpData) to apply updates dynamically
115117
foreach (var kvp in op.OpData)
116118
{
119+
// Apply the "SET" operation for each key-value pair.
120+
// The key represents the JSON property name and the value is the new value to be set
117121
updateQuery = SupabasePatchHelper.ApplySet(updateQuery, kvp.Key, kvp.Value);
118122
}
119123

120124
_ = await updateQuery.Update();
121125
}
122126
else if (op.Table.ToLower().Trim() == "todos")
123127
{
128+
// Create an update query for the 'Todo' table where the 'Id' matches 'op.Id'
124129
IPostgrestTable<Todo> updateQuery = _supabase
125-
.From<Todo>()
126-
.Where(x => x.Id == op.Id);
130+
.From<Todo>()
131+
.Where(x => x.Id == op.Id);
127132

133+
// Loop through each key-value pair in the operation data (op.OpData) to apply updates dynamically
128134
foreach (var kvp in op.OpData)
129135
{
136+
// Apply the "SET" operation for each key-value pair.
137+
// The key represents the JSON property name and the value is the new value to be set
130138
updateQuery = SupabasePatchHelper.ApplySet(updateQuery, kvp.Key, kvp.Value);
131139
}
132140

0 commit comments

Comments
 (0)