Skip to content

Commit f352a2d

Browse files
author
Daniele Briggi
committed
chore(sqlitesync): readability
1 parent 2d88341 commit f352a2d

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

examples/sport-tracker-app/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Copy from from the SQLite Cloud Dashboard
22
# eg: sqlitecloud://myhost.cloud:8860/my-remote-database.sqlite
33
VITE_SQLITECLOUD_CONNECTION_STRING=
4+
45
# The database name
56
# eg: my-remote-database.sqlite
67
VITE_SQLITECLOUD_DATABASE=
8+
79
# Your SQLite Cloud API key
810
# Copy it from the SQLite Cloud Dashboard -> Settings -> API Keys
911
VITE_SQLITECLOUD_API_KEY=
12+
1013
# Your SQLite Cloud url for APIs
1114
# Get it from the SQLite Cloud Dashboard in the Weblite section
1215
# eg: https://myhost.cloud

examples/sport-tracker-app/src/SQLiteSync.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ export class SQLiteSync {
2323
}
2424

2525
/**
26-
*
26+
* Prepare the SQLite Sync network with the information
27+
* about the remote database using the SQLite Cloud Connection String.
2728
*/
28-
async initSQLiteSyncNetwork(): Promise<void> {
29-
console.log("SQLite Sync - init network...", import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING);
30-
await this.db.sqliteSyncInitNetwork(import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING);
29+
async initializeNetwork(): Promise<void> {
30+
console.log(
31+
"SQLite Sync - Initialize network with connection string:",
32+
import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING
33+
);
34+
await this.db.sqliteSyncInitNetwork(
35+
import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING
36+
);
37+
console.log("SQLite Sync - Network initialized successfully");
3138
}
3239

3340
/**
@@ -44,7 +51,8 @@ export class SQLiteSync {
4451
// Get valid token (from session or fetch new one)
4552
const token = await this.getValidToken(userId, name);
4653

47-
// Authenticate SQLite Sync with the token
54+
// Authenticate SQLite Sync with the token.
55+
// Set the token everytime it changes because missing or expired.
4856
await this.db.sqliteSyncSetToken(token);
4957
console.log("SQLite Sync setup completed with token for user:", name);
5058
} catch (error) {
@@ -118,7 +126,9 @@ export class SQLiteSync {
118126

119127
const now = new Date();
120128
if (!token) {
121-
console.log("SQLite Sync: No token available, requesting new one from API");
129+
console.log(
130+
"SQLite Sync: No token available, requesting new one from API"
131+
);
122132
const tokenData = await this.fetchNewToken(userId, name);
123133
localStorage.setItem(
124134
SQLiteSync.TOKEN_KEY_PREFIX,

examples/sport-tracker-app/src/components/UserLogin.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const UserLogin: React.FC<UserLoginProps> = ({
2525
onLogout,
2626
onUsersLoad,
2727
onRefresh,
28-
onError
28+
onError,
2929
}) => {
3030
const { db } = useDatabase();
3131
const [selectedUserId, setSelectedUserId] = useState<string>("");
@@ -75,12 +75,13 @@ const UserLogin: React.FC<UserLoginProps> = ({
7575

7676
if (checked) {
7777
console.log("SQLite Sync enabled for user:", currentSession?.name);
78+
await sqliteSync?.initializeNetwork();
7879
} else {
7980
console.log("SQLite Sync disabled");
8081
}
8182
};
8283

83-
const handleLogin = () => {
84+
const handleLogin = async () => {
8485
const selectedUser = users.find((user) => user.id === selectedUserId);
8586
if (selectedUser) {
8687
const session: UserSession = {
@@ -104,8 +105,6 @@ const UserLogin: React.FC<UserLoginProps> = ({
104105
// If SQLite Sync is enabled, sync with cloud before refreshing
105106
if (sqliteSyncEnabled && sqliteSync && currentSession) {
106107
try {
107-
console.log("SQLite Sync - Starting init network...");
108-
await sqliteSync.initSQLiteSyncNetwork();
109108
await sqliteSync.setupWithToken(currentSession);
110109

111110
console.log("SQLite Sync - Starting sync...");
@@ -117,7 +116,8 @@ const UserLogin: React.FC<UserLoginProps> = ({
117116
error
118117
);
119118
console.warn("SQLite Sync: Falling back to local refresh only");
120-
if(onError) onError("SQLite Sync - Failed to sync with SQLite Cloud: " + error);
119+
if (onError)
120+
onError("SQLite Sync - Failed to sync with SQLite Cloud: " + error);
121121
}
122122
} else {
123123
console.log(

examples/sport-tracker-app/src/db/sqliteSyncOperations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export const getSqliteSyncOperations = (db: any) => ({
1414
return version;
1515
},
1616

17+
/**
18+
* Prepare the SQLite Sync network with the information
19+
* about the remote database using the SQLite Cloud Connection String.
20+
*/
1721
sqliteSyncInitNetwork(connectionString: string) {
18-
// Initialize SQLite Sync with the SQLite Cloud Connection String.
19-
// On the SQLite Cloud Dashboard, enable OffSync (SQLite Sync)
20-
// on the remote database and copy the Connection String.
2122
db.exec(
2223
`SELECT cloudsync_network_init('${connectionString}')`
2324
);
24-
console.log("SQLite Sync - init network done", connectionString);
2525
},
2626

2727
/** Authorize SQLite Sync with the user's Access Token. */

0 commit comments

Comments
 (0)