Skip to content

Commit 70f9349

Browse files
committed
Merge branch 'main' into feat/common-sdk
2 parents 0cae6fa + 5aee3ec commit 70f9349

File tree

11 files changed

+202
-1
lines changed

11 files changed

+202
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ TestResults/
6060
#Core binaries - add rules after ps extension downloading is supported
6161
*.dylib
6262
*.dll
63-
*.so
63+
*.so

commands

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
dotnet new sln -n root
2+
dotnet new classlib -n Common
3+
dotnet sln root.sln add src/Common/Common.csproj
4+
dotnet add reference ../../packages/CommonUtilities/CommonUtilities.csproj
5+
6+
dotnet pack packages/CommonUtilities -o ../local-nuget
7+
dotnet add package CommonUtilities --version "*"
8+
9+
dotnet new xunit -n Common.Tests
10+
dotnet sln ../../root.sln add Common.Tests/Common.Tests.csproj
11+
12+
dotnet test src/Common/Common.Tests
13+
dotnet test --logger "console;verbosity=detailed" src/Common/Common.Tests
14+
dotnet test --logger "console;verbosity=detailed"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="xunit" Version="2.5.3" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Using Include="Xunit" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\Common\Common.csproj" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Common.Tests;
2+
using Common.Client.Connection;
3+
public class PowerSyncCredentialsTests
4+
{
5+
[Fact]
6+
public void SimpleTest()
7+
{
8+
var endpoint = "http://localhost";
9+
var token = "token";
10+
var expiresAt = new DateTime();
11+
PowerSyncCredentials credentials = new PowerSyncCredentials(endpoint, token, expiresAt);
12+
Assert.Equal(endpoint, credentials.Endpoint);
13+
Assert.Equal(token, credentials.Token);
14+
Assert.Equal(expiresAt, credentials.ExpiresAt);
15+
}
16+
}

src/Common/Class1.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Common;
2+
3+
public class Class1
4+
{
5+
public void Write()
6+
{
7+
Console.WriteLine("Hello from Common!");
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Common.DB.Crud;
2+
3+
namespace Common.Client;
4+
public abstract class AbstractPowerSyncDatabase {
5+
6+
// Returns true if the connection is closed.
7+
bool closed;
8+
bool ready;
9+
10+
string sdkVersion;
11+
SyncStatus syncStatus;
12+
13+
public AbstractPowerSyncDatabase() {
14+
this.syncStatus = new SyncStatus(new SyncStatusOptions());
15+
this.closed = false;
16+
this.ready = false;
17+
18+
this.sdkVersion = "";
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Common.Client.Connection;
2+
3+
public interface IPowerSyncBackendConnector
4+
{
5+
// Allows the PowerSync client to retrieve an authentication token from your backend
6+
// which is used to authenticate against the PowerSync service.
7+
//
8+
// This should always fetch a fresh set of credentials - don't use cached
9+
// values.
10+
//
11+
// Return null if the user is not signed in. Throw an error if credentials
12+
// cannot be fetched due to a network error or other temporary error.
13+
//
14+
// This token is kept for the duration of a sync connection.
15+
Task<PowerSyncCredentials?> FetchCredentials();
16+
17+
// Upload local changes to the app backend.
18+
//
19+
// Use {@link AbstractPowerSyncDatabase.getCrudBatch} to get a batch of changes to upload.
20+
//
21+
// Any thrown errors will result in a retry after the configured wait period (default: 5 seconds).
22+
Task UploadData(AbstractPowerSyncDatabase database);
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Common.Client.Connection;
2+
public class PowerSyncCredentials(string endpoint, string token, DateTime? expiresAt = null)
3+
{
4+
public string Endpoint { get; set; } = endpoint;
5+
public string Token { get; set; } = token;
6+
public DateTime? ExpiresAt { get; set; } = expiresAt;
7+
}

src/Common/Common.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

src/Common/DB/Crud/SyncStatus.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace Common.DB.Crud;
2+
3+
using System.Text.Json;
4+
5+
6+
public class SyncDataFlowStatus
7+
{
8+
public bool Downloading { get; set; } = false;
9+
public bool Uploading { get; set; } = false;
10+
}
11+
12+
13+
public class SyncStatusOptions
14+
{
15+
public bool? Connected { get; set; }
16+
public SyncDataFlowStatus? DataFlow { get; set; }
17+
public DateTime? LastSyncedAt { get; set; }
18+
public bool? HasSynced { get; set; }
19+
}
20+
21+
public class SyncStatus(SyncStatusOptions options)
22+
{
23+
protected SyncStatusOptions options = options;
24+
25+
public bool Connected => options.Connected ?? false;
26+
27+
public DateTime? LastSyncedAt => options.LastSyncedAt;
28+
29+
public bool? HasSynced => options.HasSynced;
30+
31+
public SyncDataFlowStatus DataFlowStatus => options.DataFlow ?? new SyncDataFlowStatus();
32+
33+
public bool IsEqual(SyncStatus status)
34+
{
35+
return JsonSerializer.Serialize(options) == JsonSerializer.Serialize(status.options);
36+
}
37+
38+
public string GetMessage()
39+
{
40+
return $"SyncStatus<connected: {Connected}, lastSyncedAt: {LastSyncedAt}, hasSynced: {HasSynced}, " +
41+
$"downloading: {DataFlowStatus.Downloading}, uploading: {DataFlowStatus.Uploading}>";
42+
}
43+
44+
public SyncStatusOptions ToJson()
45+
{
46+
return new SyncStatusOptions
47+
{
48+
Connected = Connected,
49+
DataFlow = DataFlowStatus,
50+
LastSyncedAt = LastSyncedAt,
51+
HasSynced = HasSynced
52+
};
53+
}
54+
55+
public override string ToString()
56+
{
57+
return GetMessage();
58+
}
59+
}

0 commit comments

Comments
 (0)