Skip to content

Commit dc58656

Browse files
Add configuration options for Supabase and Node connectors
1 parent 8b3151a commit dc58656

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

demos/CommandLine/.env.template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
SUPABASE_URL=your-supabase-url
22
SUPABASE_ANON_KEY=your_anon_key_here
33
POWERSYNC_URL=your-powersync-url
4+
BACKEND_URL=your-backend-url
5+
SUPABASE_USERNAME=your-supabase-username
6+
SUPABASE_PASSWORD=your-supabase-password
7+
# Set to true if you want to use Supabase as the backend
8+
# Set to false if you want to use the Powersync backend
9+
USE_SUPABASE=true

demos/CommandLine/Demo.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
using CommandLine.Utils;
44
using PowerSync.Common.Client;
5+
using PowerSync.Common.Client.Connection;
56
using Spectre.Console;
67

78
class Demo
89
{
9-
1010
private record ListResult(string id, string name, string owner_id, string created_at);
1111
static async Task Main()
1212
{
@@ -17,11 +17,30 @@ static async Task Main()
1717
});
1818
await db.Init();
1919

20-
//var connector = new NodeConnector();
21-
var config = new SupabaseConfig();
22-
var connector = new SupabaseConnector(config);
20+
var config = new Config();
21+
22+
IPowerSyncBackendConnector connector;
23+
24+
string connectorUserId = "";
25+
26+
if (config.UseSupabase)
27+
{
28+
var supabaseConnector = new SupabaseConnector(config);
29+
30+
await supabaseConnector.Login(config.SupabaseUsername, config.SupabasePassword);
31+
32+
connectorUserId = supabaseConnector.UserId;
33+
34+
connector = supabaseConnector;
35+
}
36+
else
37+
{
38+
var nodeConnector = new NodeConnector(config);
39+
40+
connectorUserId = nodeConnector.UserId;
2341

24-
await connector.Login("[email protected]", "Dean1998");
42+
connector = nodeConnector;
43+
}
2544

2645
var table = new Table()
2746
.AddColumn("id")
@@ -55,7 +74,7 @@ static async Task Main()
5574
// await db.Execute("insert into lists (id, name, owner_id, created_at) values (uuid(), 'New User33', ?, datetime())", [connector.UserId]);
5675

5776
await db.Execute(
58-
"UPDATE lists SET name = ?, created_at = datetime() WHERE owner_id = ? and id = ?", ["update CHCHCHCHCH" , connector.UserId, "0bf55412-d35b-4814-ade9-daea4865df96"]
77+
"UPDATE lists SET name = ?, created_at = datetime() WHERE owner_id = ? and id = ?", ["update CHCHCHCHCH", connectorUserId, "0bf55412-d35b-4814-ade9-daea4865df96"]
5978
);
6079

6180
var _ = Task.Run(async () =>
@@ -71,7 +90,7 @@ await db.Execute(
7190
}
7291
else if (key.Key == ConsoleKey.Enter)
7392
{
74-
await db.Execute("insert into lists (id, name, owner_id, created_at) values (uuid(), 'New User', ?, datetime())", [connector.UserId]);
93+
await db.Execute("insert into lists (id, name, owner_id, created_at) values (uuid(), 'New User', ?, datetime())", [connectorUserId]);
7594
}
7695
else if (key.Key == ConsoleKey.Backspace)
7796
{

demos/CommandLine/NodeConnector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using PowerSync.Common.Client;
1111
using PowerSync.Common.Client.Connection;
1212
using PowerSync.Common.DB.Crud;
13-
13+
using CommandLine.Utils;
1414

1515
public class NodeConnector : IPowerSyncBackendConnector
1616
{
@@ -22,15 +22,15 @@ public class NodeConnector : IPowerSyncBackendConnector
2222
public string UserId { get; private set; }
2323
private string? clientId;
2424

25-
public NodeConnector()
25+
public NodeConnector(Config config)
2626
{
2727
_httpClient = new HttpClient();
2828

2929
// Load or generate User ID
3030
UserId = LoadOrGenerateUserId();
3131

32-
BackendUrl = "http://localhost:6060";
33-
PowerSyncUrl = "http://localhost:8080";
32+
BackendUrl = config.BackendUrl;
33+
PowerSyncUrl = config.PowerSyncUrl;
3434

3535
clientId = null;
3636
}

demos/CommandLine/SupabaseConnector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace CommandLine;
1515
public class SupabaseConnector : IPowerSyncBackendConnector
1616
{
1717
private readonly Supabase.Client _supabase;
18-
private readonly SupabaseConfig _config;
18+
private readonly Config _config;
1919
private Session? _currentSession;
2020

2121
public Session? CurrentSession
@@ -36,7 +36,7 @@ public Session? CurrentSession
3636

3737
public bool Ready { get; private set; }
3838

39-
public SupabaseConnector(SupabaseConfig config)
39+
public SupabaseConnector(Config config)
4040
{
4141
_config = config;
4242
_supabase = new Supabase.Client(config.SupabaseUrl, config.SupabaseAnonKey, new SupabaseOptions

demos/CommandLine/Utils/Config.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1-
namespace CommandLine.Utils;
2-
31
using dotenv.net;
42

5-
public class SupabaseConfig
3+
namespace CommandLine.Utils;
4+
5+
public class Config
66
{
77
public string SupabaseUrl { get; set; }
88
public string SupabaseAnonKey { get; set; }
99
public string PowerSyncUrl { get; set; }
10+
public string BackendUrl { get; set; }
11+
public string SupabaseUsername { get; set; }
12+
public string SupabasePassword { get; set; }
13+
public bool UseSupabase { get; set; }
1014

11-
public SupabaseConfig()
15+
public Config()
1216
{
1317
DotEnv.Load();
1418
Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
19+
1520
// Retrieve the environment variables
1621
SupabaseUrl = Environment.GetEnvironmentVariable("SUPABASE_URL")
1722
?? throw new InvalidOperationException("SUPABASE_URL environment variable is not set.");
1823
SupabaseAnonKey = Environment.GetEnvironmentVariable("SUPABASE_ANON_KEY")
1924
?? throw new InvalidOperationException("SUPABASE_ANON_KEY environment variable is not set.");
2025
PowerSyncUrl = Environment.GetEnvironmentVariable("POWERSYNC_URL")
2126
?? throw new InvalidOperationException("POWERSYNC_URL environment variable is not set.");
27+
BackendUrl = Environment.GetEnvironmentVariable("BACKEND_URL")
28+
?? throw new InvalidOperationException("BACKEND_URL environment variable is not set.");
29+
SupabaseUsername = Environment.GetEnvironmentVariable("SUPABASE_USERNAME")
30+
?? throw new InvalidOperationException("SUPABASE_USERNAME environment variable is not set.");
31+
SupabasePassword = Environment.GetEnvironmentVariable("SUPABASE_PASSWORD")
32+
?? throw new InvalidOperationException("SUPABASE_PASSWORD environment variable is not set.");
33+
34+
// Parse boolean value
35+
string useSupabaseStr = Environment.GetEnvironmentVariable("USE_SUPABASE") ?? "false";
36+
if (!bool.TryParse(useSupabaseStr, out bool useSupabase))
37+
{
38+
throw new InvalidOperationException("USE_SUPABASE environment variable is not a valid boolean.");
39+
}
40+
UseSupabase = useSupabase;
2241
}
2342
}

0 commit comments

Comments
 (0)