Skip to content

Commit 2b75e9a

Browse files
authored
Merge pull request #25 from powersync-ja/feat/app-metadata-main
(feat) Custom App Metadata
2 parents 71172a3 + 00ee1a0 commit 2b75e9a

7 files changed

Lines changed: 54 additions & 3 deletions

File tree

PowerSync/PowerSync.Common/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
- Reporting progress information about downloaded rows. Sync progress is available through `SyncStatus.DownloadProgress()`.
1010
- Support bucket priorities.
1111
- Report `PriorityStatusEntries` on `SyncStatus`.
12+
- Added ability to specify `AppMetadata` for sync/stream requests.
13+
14+
Note: This requires a PowerSync service version `>=1.17.0` in order for logs to display metadata.
15+
16+
```csharp
17+
db.Connect(connector, new PowerSync.Common.Client.Sync.Stream.PowerSyncConnectionOptions
18+
{
19+
// This will be included in PowerSync service logs
20+
AppMetadata = new Dictionary<string, string>
21+
{
22+
{ "app_version", myAppVersion },
23+
}
24+
});
25+
```
1226

1327
## 0.0.5-alpha.1
1428
- Using the latest version (0.4.9) of the core extension, it introduces support for the Rust Sync implementation and also makes it the default - users can still opt out and use the legacy C# sync implementation as option when calling `connect()`.

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncImplementation.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ public class StreamingSyncImplementationOptions : AdditionalConnectionOptions
5050
public ILogger? Logger { get; init; }
5151
}
5252

53-
public class BaseConnectionOptions(Dictionary<string, object>? parameters = null)
53+
public class BaseConnectionOptions(Dictionary<string, object>? parameters = null, Dictionary<string, string>? appMetadata = null)
5454
{
55+
/// <summary>
56+
/// A set of metadata to be included in service logs.
57+
/// </summary>
58+
public Dictionary<string, string>? AppMetadata { get; set; } = appMetadata;
59+
5560
/// <summary>
5661
/// These parameters are passed to the sync rules and will be available under the `user_parameters` object.
5762
/// </summary>
@@ -60,6 +65,9 @@ public class BaseConnectionOptions(Dictionary<string, object>? parameters = null
6065

6166
public class RequiredPowerSyncConnectionOptions : BaseConnectionOptions
6267
{
68+
69+
public new Dictionary<string, string> AppMetadata { get; set; } = new();
70+
6371
public new Dictionary<string, object> Params { get; set; } = new();
6472
}
6573

@@ -97,6 +105,7 @@ public class StreamingSyncImplementation : EventStream<StreamingSyncImplementati
97105
{
98106
public static RequiredPowerSyncConnectionOptions DEFAULT_STREAM_CONNECTION_OPTIONS = new()
99107
{
108+
AppMetadata = [],
100109
Params = [],
101110
};
102111

@@ -381,6 +390,7 @@ protected async Task<StreamingSyncIterationResult> StreamingSyncIteration(Cancel
381390
{
382391
var resolvedOptions = new RequiredPowerSyncConnectionOptions
383392
{
393+
AppMetadata = options?.AppMetadata ?? DEFAULT_STREAM_CONNECTION_OPTIONS.AppMetadata,
384394
Params = options?.Params ?? DEFAULT_STREAM_CONNECTION_OPTIONS.Params,
385395
};
386396

@@ -550,7 +560,7 @@ async Task HandleInstruction(Instruction instruction)
550560

551561
try
552562
{
553-
await Control(PowerSyncControlCommand.START, JsonConvert.SerializeObject(new { parameters = resolvedOptions.Params }));
563+
await Control(PowerSyncControlCommand.START, JsonConvert.SerializeObject(new { parameters = resolvedOptions.Params, app_metadata = resolvedOptions.AppMetadata }));
554564

555565
notifyCompletedUploads = () =>
556566
{

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncTypes.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class StreamingSyncRequest
6666
[JsonProperty("raw_data")]
6767
public bool RawData { get; set; }
6868

69+
[JsonProperty("app_metadata")]
70+
public Dictionary<string, string>? AppMetadata { get; set; }
71+
6972
[JsonProperty("parameters")]
7073
public Dictionary<string, object>? Parameters { get; set; }
7174

PowerSync/PowerSync.Maui/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.0.4-alpha.1
44
- Upstream PowerSync.Common version bump (See Powersync.Common changelog for more information)
5+
- Added ability to specify `AppMetadata` sync/stream requests (see Common changelog).
56

67
## 0.0.3-alpha.1
78
- Upstream PowerSync.Common version bump

Tests/PowerSync/PowerSync.Common.IntegrationTests/SyncIntegrationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public async Task InitializeAsync()
4343
Console.WriteLine($"Using User ID: {userId}");
4444
try
4545
{
46+
await db.Connect(connector, new PowerSyncConnectionOptions
47+
{
48+
AppMetadata = new Dictionary<string, string>
49+
{
50+
{ "app_version", "1.0.0-integration-tests" },
51+
{ "environment", "integration-tests" }
52+
}
53+
});
4654
await db.Connect(connector);
4755
await db.WaitForFirstSync();
4856
}

demos/CommandLine/CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5+
<Version>0.0.1</Version>
56
<TargetFramework>net8.0</TargetFramework>
67
<LangVersion>12</LangVersion>
78
<ImplicitUsings>enable</ImplicitUsings>

demos/CommandLine/Demo.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace CommandLine;
22

3+
using System.Reflection;
34
using CommandLine.Utils;
45
using PowerSync.Common.Client;
56
using PowerSync.Common.Client.Connection;
@@ -96,7 +97,14 @@ static async Task Main()
9697
}
9798
});
9899

99-
await db.Connect(connector);
100+
await db.Connect(connector, new PowerSync.Common.Client.Sync.Stream.PowerSyncConnectionOptions
101+
{
102+
ClientImplementation = PowerSync.Common.Client.Sync.Stream.SyncClientImplementation.RUST,
103+
AppMetadata = new Dictionary<string, string>
104+
{
105+
{ "app_version", GetAppVersion() },
106+
}
107+
});
100108
await db.WaitForFirstSync();
101109

102110
var panel = new Panel(table)
@@ -128,4 +136,10 @@ await AnsiConsole.Live(panel)
128136

129137
Console.WriteLine("\nExited live table. Press any key to exit.");
130138
}
139+
140+
private static string GetAppVersion()
141+
{
142+
var version = Assembly.GetExecutingAssembly().GetName().Version;
143+
return version?.ToString() ?? "unknown";
144+
}
131145
}

0 commit comments

Comments
 (0)