Merge branch 'main' of https://repos.gfranz.ch/Gabriel20xx/CollabTable
This commit is contained in:
commit
55f9b89063
28 files changed
+483
-290
No files matched your search
@@ -33,3 +33,19 @@ PGPORT=5432
|
||||
PGUSER=postgres
|
||||
PGPASSWORD=
|
||||
PGDATABASE=collabtable
|
||||
|
||||
#################################################################
|
||||
# Startup resilience and sync/auth tuning
|
||||
#################################################################
|
||||
# DB init retry interval in milliseconds (default: 5000)
|
||||
DB_INIT_RETRY_INTERVAL_MS=5000
|
||||
|
||||
# Max DB init retry attempts. 0 means retry forever (default: 0)
|
||||
DB_INIT_RETRY_MAX_ATTEMPTS=0
|
||||
|
||||
# Suppress repetitive auth warning logs for the same path/method (default: 30000)
|
||||
AUTH_LOG_THROTTLE_MS=30000
|
||||
|
||||
# Allow at most this much client clock skew into sync timestamps (default: 0)
|
||||
# Keep this at 0 to prevent future-dated rows from being re-sent in hot loops.
|
||||
SYNC_MAX_FUTURE_SKEW_MS=0
|
||||
@@ -230,6 +230,10 @@ docker-compose up -d --build
|
||||
- `DB_PATH` - Path to SQLite database file (default: ./data/collabtable.db for local, /data/collabtable.db in Docker)
|
||||
- `DATABASE_URL` - PostgreSQL connection URL (optional alternative to PG* variables)
|
||||
- `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, `PGDATABASE` - PostgreSQL connection parameters
|
||||
- `DB_INIT_RETRY_INTERVAL_MS` - Delay between DB init retries in ms (default: `5000`)
|
||||
- `DB_INIT_RETRY_MAX_ATTEMPTS` - Max DB init retries; `0` retries forever (default: `0`)
|
||||
- `AUTH_LOG_THROTTLE_MS` - Minimum interval before repeating the same auth warning log (default: `30000`)
|
||||
- `SYNC_MAX_FUTURE_SKEW_MS` - Allowed future timestamp skew from clients during sync (default: `0`)
|
||||
|
||||
## Data Persistence
|
||||
|
||||
|
||||
Binary file not shown.
@@ -14,6 +14,11 @@ services:
|
||||
- DB_CLIENT=sqlite
|
||||
# SQLite path (used when DB_CLIENT=sqlite)
|
||||
- DB_PATH=/data/collabtable.db
|
||||
# Retry DB initialization instead of crashing immediately on startup races
|
||||
- DB_INIT_RETRY_INTERVAL_MS=5000
|
||||
- DB_INIT_RETRY_MAX_ATTEMPTS=0
|
||||
# Clamp future client timestamps during sync to avoid resend loops
|
||||
- SYNC_MAX_FUTURE_SKEW_MS=0
|
||||
# PostgreSQL (used when DB_CLIENT=postgres) - choose either DATABASE_URL or the PG* vars
|
||||
# - DATABASE_URL=postgres://user:password@postgres:5432/collabtable
|
||||
# - PGHOST=postgres
|
||||
|
||||
Generated
+4
-4
@@ -27,7 +27,7 @@
|
||||
"@types/node": "^20.10.6",
|
||||
"@types/pg": "^8.10.2",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "5.5.4"
|
||||
}
|
||||
},
|
||||
"node_modules/@cspotcode/source-map-support": {
|
||||
@@ -3607,9 +3607,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.9.3",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
|
||||
"version": "5.5.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz",
|
||||
"integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==",
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
"@types/node": "^20.10.6",
|
||||
"@types/better-sqlite3": "^7.6.11",
|
||||
"@types/pg": "^8.10.2",
|
||||
"typescript": "^5.3.3",
|
||||
"typescript": "5.5.4",
|
||||
"ts-node-dev": "^2.0.0"
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,39 @@ dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const DB_INIT_RETRY_INTERVAL_MS = Math.max(250, Number(process.env.DB_INIT_RETRY_INTERVAL_MS || 5000));
|
||||
const DB_INIT_RETRY_MAX_ATTEMPTS = Math.max(0, Number(process.env.DB_INIT_RETRY_MAX_ATTEMPTS || 0));
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function initializeDatabaseWithRetry() {
|
||||
let attempt = 0;
|
||||
|
||||
for (;;) {
|
||||
attempt += 1;
|
||||
|
||||
try {
|
||||
await initializeDatabase();
|
||||
if (attempt > 1) {
|
||||
console.log(`[DB] Connected after ${attempt} attempt(s)`);
|
||||
}
|
||||
return;
|
||||
} catch (error) {
|
||||
const reachedLimit = DB_INIT_RETRY_MAX_ATTEMPTS > 0 && attempt >= DB_INIT_RETRY_MAX_ATTEMPTS;
|
||||
|
||||
console.error(`[DB] Initialization attempt ${attempt} failed`, error);
|
||||
|
||||
if (reachedLimit) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`[DB] Retrying in ${DB_INIT_RETRY_INTERVAL_MS}ms...`);
|
||||
await sleep(DB_INIT_RETRY_INTERVAL_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
@@ -44,7 +77,7 @@ app.use('/', webRoutes);
|
||||
// Initialize database and start server
|
||||
(async () => {
|
||||
try {
|
||||
await initializeDatabase();
|
||||
await initializeDatabaseWithRetry();
|
||||
console.log('Database synchronized successfully');
|
||||
console.log('Database models loaded:');
|
||||
console.log('- Lists');
|
||||
|
||||
@@ -1,6 +1,33 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
let warningLogged = false;
|
||||
const AUTH_LOG_THROTTLE_MS = Math.max(1000, Number(process.env.AUTH_LOG_THROTTLE_MS || 30000));
|
||||
const lastAuthLogAt = new Map<string, number>();
|
||||
const commonProbePathPatterns = [
|
||||
/^\/\./,
|
||||
/client_secret\.json$/i,
|
||||
/phpinfo/i,
|
||||
/\.git/i,
|
||||
/wp-admin/i,
|
||||
/wp-login/i,
|
||||
/id_rsa/i,
|
||||
/config(\.|$)/i
|
||||
];
|
||||
|
||||
function isCommonProbePath(pathname: string) {
|
||||
return commonProbePathPatterns.some(pattern => pattern.test(pathname));
|
||||
}
|
||||
|
||||
function shouldLogAuthWarning(kind: string, req: Request) {
|
||||
const key = `${kind}:${req.method}:${req.path}`;
|
||||
const now = Date.now();
|
||||
const lastSeen = lastAuthLogAt.get(key) || 0;
|
||||
if (now - lastSeen < AUTH_LOG_THROTTLE_MS) {
|
||||
return false;
|
||||
}
|
||||
lastAuthLogAt.set(key, now);
|
||||
return true;
|
||||
}
|
||||
|
||||
export const authenticatePassword = (req: Request, res: Response, next: NextFunction) => {
|
||||
const serverPassword = process.env.SERVER_PASSWORD;
|
||||
@@ -18,12 +45,13 @@ export const authenticatePassword = (req: Request, res: Response, next: NextFunc
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
// Log diagnostic info to help trace intermittent 401s
|
||||
console.warn('[AUTH] Missing Authorization header', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
});
|
||||
if (!isCommonProbePath(req.path) && shouldLogAuthWarning('missingHeader', req)) {
|
||||
console.warn('[AUTH] Missing Authorization header', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
});
|
||||
}
|
||||
return res.status(401).json({
|
||||
error: 'Unauthorized',
|
||||
message: 'No authorization header provided'
|
||||
@@ -33,13 +61,15 @@ export const authenticatePassword = (req: Request, res: Response, next: NextFunc
|
||||
// Parse the authorization header
|
||||
const parts = authHeader.split(' ');
|
||||
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
||||
console.warn('[AUTH] Invalid authorization header format', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
// Do not log full header to avoid leaking secrets; include prefix only
|
||||
headerPreview: authHeader.substring(0, Math.min(authHeader.length, 20)) + '...'
|
||||
});
|
||||
if (shouldLogAuthWarning('invalidHeaderFormat', req)) {
|
||||
console.warn('[AUTH] Invalid authorization header format', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
// Do not log full header to avoid leaking secrets; include prefix only
|
||||
headerPreview: authHeader.substring(0, Math.min(authHeader.length, 20)) + '...'
|
||||
});
|
||||
}
|
||||
return res.status(401).json({
|
||||
error: 'Unauthorized',
|
||||
message: 'Invalid authorization header format. Expected: Bearer <password>'
|
||||
@@ -51,13 +81,15 @@ export const authenticatePassword = (req: Request, res: Response, next: NextFunc
|
||||
// Validate password
|
||||
if (providedPassword !== serverPassword) {
|
||||
// Avoid logging the full password; log only length and a small preview
|
||||
const safePreview = providedPassword ? `${providedPassword.substring(0, 2)}…(${providedPassword.length})` : 'null';
|
||||
console.warn('[AUTH] Invalid password', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
providedPreview: safePreview,
|
||||
});
|
||||
if (shouldLogAuthWarning('invalidPassword', req)) {
|
||||
const safePreview = providedPassword ? `${providedPassword.substring(0, 2)}..(${providedPassword.length})` : 'null';
|
||||
console.warn('[AUTH] Invalid password', {
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
ip: req.ip,
|
||||
providedPreview: safePreview,
|
||||
});
|
||||
}
|
||||
return res.status(401).json({
|
||||
error: 'Unauthorized',
|
||||
message: 'Invalid password'
|
||||
|
||||
@@ -57,7 +57,9 @@ router.post('/values', async (req: Request, res: Response) => {
|
||||
if (listId) {
|
||||
await enqueueNotification(dbAdapter, (req as any).deviceId, 'listContentUpdated', 'value', id, listId, Date.now());
|
||||
}
|
||||
} catch {}
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
res.json(itemValue);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Failed to save item value' });
|
||||
@@ -101,7 +103,9 @@ router.delete('/:id', async (req: Request, res: Response) => {
|
||||
const item = await dbAdapter.queryOne('SELECT * FROM items WHERE id = ?', [req.params.id]);
|
||||
const listId = item ? ((item as any).listId ?? (item as any).listid) : undefined;
|
||||
await enqueueNotification(dbAdapter, (req as any).deviceId, 'deleted', 'item', req.params.id, listId, updatedAt);
|
||||
} catch {}
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
res.json({ message: 'Item deleted successfully' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Failed to delete item' });
|
||||
|
||||
@@ -12,6 +12,36 @@ interface SyncRequest {
|
||||
itemValues: any[];
|
||||
}
|
||||
|
||||
const SYNC_MAX_FUTURE_SKEW_MS = Math.max(0, Number(process.env.SYNC_MAX_FUTURE_SKEW_MS || 0));
|
||||
|
||||
function normalizeTimestamp(raw: any, fallback: number, nowMs: number) {
|
||||
const numeric = Number(raw);
|
||||
if (!Number.isFinite(numeric) || numeric <= 0) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
let ts = Math.trunc(numeric);
|
||||
if (ts < 1e12) {
|
||||
ts *= 1000;
|
||||
}
|
||||
|
||||
const maxAllowed = nowMs + SYNC_MAX_FUTURE_SKEW_MS;
|
||||
if (ts > maxAllowed) {
|
||||
return maxAllowed;
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
function normalizeEntityTimestamps(createdAtRaw: any, updatedAtRaw: any, nowMs: number) {
|
||||
const createdAt = normalizeTimestamp(createdAtRaw, nowMs, nowMs);
|
||||
const updatedAt = normalizeTimestamp(updatedAtRaw, createdAt, nowMs);
|
||||
return {
|
||||
createdAt,
|
||||
updatedAt: Math.max(createdAt, updatedAt)
|
||||
};
|
||||
}
|
||||
|
||||
// Stats tracking
|
||||
let syncStats = {
|
||||
totalSyncs: 0,
|
||||
@@ -55,7 +85,7 @@ setInterval(() => {
|
||||
lastReset: Date.now()
|
||||
};
|
||||
}
|
||||
}, 60000); // Changed from 30s to 60s
|
||||
}, 60000).unref?.(); // Changed from 30s to 60s
|
||||
|
||||
// Helper function to get prepared statements (lazy initialization)
|
||||
// We'll perform upserts with positional params for cross-DB portability
|
||||
@@ -66,15 +96,23 @@ const UPSERT_ITEM_VALUE = 'INSERT INTO item_values (id, itemId, fieldId, value,
|
||||
|
||||
router.post('/sync', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { lastSyncTimestamp, lists, fields, items, itemValues }: SyncRequest = req.body;
|
||||
const {
|
||||
lastSyncTimestamp,
|
||||
lists = [],
|
||||
fields = [],
|
||||
items = [],
|
||||
itemValues = []
|
||||
}: Partial<SyncRequest> = req.body || {};
|
||||
const deviceId = (req as any).deviceId as string | undefined;
|
||||
const nowMs = Date.now();
|
||||
const safeLastSyncTimestamp = normalizeTimestamp(lastSyncTimestamp, 0, nowMs);
|
||||
|
||||
// Track stats
|
||||
syncStats.totalSyncs++;
|
||||
const incomingLists = lists?.length || 0;
|
||||
const incomingFields = fields?.length || 0;
|
||||
const incomingItems = items?.length || 0;
|
||||
const incomingValues = itemValues?.length || 0;
|
||||
const incomingLists = lists.length;
|
||||
const incomingFields = fields.length;
|
||||
const incomingItems = items.length;
|
||||
const incomingValues = itemValues.length;
|
||||
|
||||
syncStats.listsReceived += incomingLists;
|
||||
syncStats.fieldsReceived += incomingFields;
|
||||
@@ -83,7 +121,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
|
||||
// Only log significant sync events (not empty syncs)
|
||||
const hasIncomingData = incomingLists > 0 || incomingFields > 0 || incomingItems > 0 || incomingValues > 0;
|
||||
const isInitialSync = lastSyncTimestamp === 0;
|
||||
const isInitialSync = safeLastSyncTimestamp === 0;
|
||||
|
||||
if (hasIncomingData) {
|
||||
const timestamp = new Date().toLocaleTimeString();
|
||||
@@ -92,7 +130,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
if (incomingLists > 0) {
|
||||
console.log(` [LISTS] ${incomingLists} list(s)`);
|
||||
lists.forEach(list => {
|
||||
const action = list.isDeleted ? 'Deleted' : (lastSyncTimestamp === 0 ? 'Created' : 'Updated');
|
||||
const action = list.isDeleted ? 'Deleted' : (safeLastSyncTimestamp === 0 ? 'Created' : 'Updated');
|
||||
console.log(` ${action}: "${list.name}"`);
|
||||
});
|
||||
}
|
||||
@@ -117,25 +155,28 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
await dbAdapter.transaction(async (tx) => {
|
||||
if (lists && lists.length > 0) {
|
||||
for (const list of lists) {
|
||||
const listTs = normalizeEntityTimestamps(list.createdAt, list.updatedAt, nowMs);
|
||||
await tx.execute(UPSERT_LIST, [
|
||||
list.id,
|
||||
list.name,
|
||||
list.createdAt,
|
||||
list.updatedAt,
|
||||
listTs.createdAt,
|
||||
listTs.updatedAt,
|
||||
list.isDeleted ? 1 : 0
|
||||
]);
|
||||
// enqueue list-level notifications
|
||||
try {
|
||||
const ev = list.isDeleted ? 'deleted' : (lastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'list', list.id, list.id, list.updatedAt);
|
||||
} catch {}
|
||||
const ev = list.isDeleted ? 'deleted' : (safeLastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'list', list.id, list.id, listTs.updatedAt);
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
// If a list was deleted, cascade the deletion to child records on server
|
||||
// - mark fields and items as deleted (tombstones) so other clients learn about them
|
||||
// - remove item_values belonging to items under this list (no tombstone support for values)
|
||||
if (list.isDeleted) {
|
||||
try {
|
||||
await tx.execute('UPDATE fields SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [list.updatedAt, list.id]);
|
||||
await tx.execute('UPDATE items SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [list.updatedAt, list.id]);
|
||||
await tx.execute('UPDATE fields SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [listTs.updatedAt, list.id]);
|
||||
await tx.execute('UPDATE items SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [listTs.updatedAt, list.id]);
|
||||
await tx.execute('DELETE FROM item_values WHERE itemId IN (SELECT id FROM items WHERE listId = ?)', [list.id]);
|
||||
} catch (err) {
|
||||
console.warn('[SYNC] Cascade delete for list failed:', String(err));
|
||||
@@ -146,6 +187,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
|
||||
if (fields && fields.length > 0) {
|
||||
for (const field of fields) {
|
||||
const fieldTs = normalizeEntityTimestamps(field.createdAt, field.updatedAt, nowMs);
|
||||
await tx.execute(UPSERT_FIELD, [
|
||||
field.id,
|
||||
field.name,
|
||||
@@ -154,14 +196,16 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
(field.alignment ?? 'start'),
|
||||
field.listId,
|
||||
field.order,
|
||||
field.createdAt,
|
||||
field.updatedAt,
|
||||
fieldTs.createdAt,
|
||||
fieldTs.updatedAt,
|
||||
field.isDeleted ? 1 : 0
|
||||
]);
|
||||
try {
|
||||
const ev = field.isDeleted ? 'deleted' : (lastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'field', field.id, field.listId, field.updatedAt);
|
||||
} catch {}
|
||||
const ev = field.isDeleted ? 'deleted' : (safeLastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'field', field.id, field.listId, fieldTs.updatedAt);
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
if (field.isDeleted) {
|
||||
// Remove item_values for this field
|
||||
try {
|
||||
@@ -175,7 +219,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
const remainingCount = remaining ? (remaining.cnt ?? remaining.CNT ?? remaining.count ?? 0) : 0;
|
||||
if (remainingCount === 0) {
|
||||
// Soft delete items in this list and purge their values
|
||||
await tx.execute('UPDATE items SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [field.updatedAt, field.listId]);
|
||||
await tx.execute('UPDATE items SET isDeleted = 1, updatedAt = ? WHERE listId = ?', [fieldTs.updatedAt, field.listId]);
|
||||
await tx.execute('DELETE FROM item_values WHERE itemId IN (SELECT id FROM items WHERE listId = ?)', [field.listId]);
|
||||
console.log('[SYNC] Last field deleted; cascaded item+value cleanup for list', field.listId);
|
||||
}
|
||||
@@ -188,17 +232,20 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
|
||||
if (items && items.length > 0) {
|
||||
for (const item of items) {
|
||||
const itemTs = normalizeEntityTimestamps(item.createdAt, item.updatedAt, nowMs);
|
||||
await tx.execute(UPSERT_ITEM, [
|
||||
item.id,
|
||||
item.listId,
|
||||
item.createdAt,
|
||||
item.updatedAt,
|
||||
itemTs.createdAt,
|
||||
itemTs.updatedAt,
|
||||
item.isDeleted ? 1 : 0
|
||||
]);
|
||||
try {
|
||||
const ev = item.isDeleted ? 'deleted' : (lastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'item', item.id, item.listId, item.updatedAt);
|
||||
} catch {}
|
||||
const ev = item.isDeleted ? 'deleted' : (safeLastSyncTimestamp === 0 ? 'created' : 'updated');
|
||||
await enqueueNotification(tx, deviceId, ev, 'item', item.id, item.listId, itemTs.updatedAt);
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
// If an item was deleted, remove its values (no tombstone support for values)
|
||||
if (item.isDeleted) {
|
||||
try {
|
||||
@@ -220,9 +267,9 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
// Helper to build IN clause placeholders (?,?,?) matching adapter's ? substitution
|
||||
const makeIn = (arr: string[]) => arr.map(() => '?').join(', ');
|
||||
|
||||
let existingItemIds = new Set<string>();
|
||||
let existingFieldIds = new Set<string>();
|
||||
const fieldToList: Record<string, string> = {};
|
||||
let existingItemIds = new Set<string>();
|
||||
let existingFieldIds = new Set<string>();
|
||||
const fieldToList: Record<string, string> = {};
|
||||
try {
|
||||
if (distinctItemIds.length > 0) {
|
||||
const rows = await tx.queryAll(`SELECT id FROM items WHERE id IN (${makeIn(distinctItemIds)})`, distinctItemIds);
|
||||
@@ -238,6 +285,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
}
|
||||
|
||||
for (const value of itemValues) {
|
||||
const valueUpdatedAt = normalizeTimestamp(value.updatedAt, nowMs, nowMs);
|
||||
const hasItem = existingItemIds.has(value.itemId);
|
||||
const hasField = existingFieldIds.has(value.fieldId);
|
||||
if (!hasField) {
|
||||
@@ -252,7 +300,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
orphanValues.push({ id: value.id, itemId: value.itemId, fieldId: value.fieldId, reason: 'missingItemNoList' });
|
||||
continue;
|
||||
}
|
||||
const ts = value.updatedAt ?? Date.now();
|
||||
const ts = valueUpdatedAt;
|
||||
try {
|
||||
await tx.execute(UPSERT_ITEM, [
|
||||
value.itemId,
|
||||
@@ -274,15 +322,17 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
value.itemId,
|
||||
value.fieldId,
|
||||
value.value,
|
||||
value.updatedAt
|
||||
valueUpdatedAt
|
||||
]);
|
||||
// Coarse list content update notification
|
||||
try {
|
||||
const lId = fieldToList[value.fieldId];
|
||||
if (lId) {
|
||||
await enqueueNotification(tx, deviceId, 'listContentUpdated', 'value', value.id, lId, value.updatedAt);
|
||||
await enqueueNotification(tx, deviceId, 'listContentUpdated', 'value', value.id, lId, valueUpdatedAt);
|
||||
}
|
||||
} catch {}
|
||||
} catch (notificationError) {
|
||||
void notificationError;
|
||||
}
|
||||
} catch (err: any) {
|
||||
// Catch FK violation just in case race or deletion happened inside same sync
|
||||
if (err && err.code === '23503') {
|
||||
@@ -309,17 +359,17 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
// IMPORTANT: Include deleted items so clients can sync deletions
|
||||
let serverLists, serverFields, serverItems, serverItemValues;
|
||||
|
||||
if (lastSyncTimestamp === 0) {
|
||||
if (safeLastSyncTimestamp === 0) {
|
||||
// Include deleted rows on initial sync to propagate tombstones
|
||||
serverLists = await dbAdapter.queryAll('SELECT * FROM lists');
|
||||
serverFields = await dbAdapter.queryAll('SELECT * FROM fields');
|
||||
serverItems = await dbAdapter.queryAll('SELECT * FROM items');
|
||||
serverItemValues = await dbAdapter.queryAll('SELECT * FROM item_values');
|
||||
} else {
|
||||
serverLists = await dbAdapter.queryAll('SELECT * FROM lists WHERE updatedAt >= ?', [lastSyncTimestamp]);
|
||||
serverFields = await dbAdapter.queryAll('SELECT * FROM fields WHERE updatedAt >= ?', [lastSyncTimestamp]);
|
||||
serverItems = await dbAdapter.queryAll('SELECT * FROM items WHERE updatedAt >= ?', [lastSyncTimestamp]);
|
||||
serverItemValues = await dbAdapter.queryAll('SELECT * FROM item_values WHERE updatedAt >= ?', [lastSyncTimestamp]);
|
||||
serverLists = await dbAdapter.queryAll('SELECT * FROM lists WHERE updatedAt >= ?', [safeLastSyncTimestamp]);
|
||||
serverFields = await dbAdapter.queryAll('SELECT * FROM fields WHERE updatedAt >= ?', [safeLastSyncTimestamp]);
|
||||
serverItems = await dbAdapter.queryAll('SELECT * FROM items WHERE updatedAt >= ?', [safeLastSyncTimestamp]);
|
||||
serverItemValues = await dbAdapter.queryAll('SELECT * FROM item_values WHERE updatedAt >= ?', [safeLastSyncTimestamp]);
|
||||
}
|
||||
|
||||
// Normalize output for clients (camelCase keys, ms timestamps, boolean isDeleted)
|
||||
@@ -383,7 +433,7 @@ router.post('/sync', async (req: Request, res: Response) => {
|
||||
|
||||
// Only log when sending significant data back
|
||||
const hasOutgoingData = (serverLists as any[]).length > 0 || (serverFields as any[]).length > 0 || (serverItems as any[]).length > 0 || (serverItemValues as any[]).length > 0;
|
||||
if (hasOutgoingData && !isInitialSync) {
|
||||
if (hasOutgoingData && !isInitialSync && hasIncomingData) {
|
||||
console.log(` [OUT] Sending back: ${(serverLists as any[]).length} lists, ${(serverFields as any[]).length} fields, ${(serverItems as any[]).length} items, ${(serverItemValues as any[]).length} values`);
|
||||
} else if (isInitialSync && hasOutgoingData) {
|
||||
console.log(` [OUT] Sending initial data: ${(serverLists as any[]).length} lists, ${(serverFields as any[]).length} fields, ${(serverItems as any[]).length} items, ${(serverItemValues as any[]).length} values`);
|
||||
|
||||
Reference in new issue
Block a user