@aventoapp/framework 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/offline/index.js +399 -0
- package/dist/offline/index.js.map +7 -0
- package/dist/server/index.js +75 -0
- package/dist/server/index.js.map +3 -3
- package/package.json +79 -81
- package/src/offline/clock.js +12 -0
- package/src/offline/coalesce.js +45 -0
- package/src/offline/diff.js +17 -0
- package/src/offline/idb.js +52 -0
- package/src/offline/index.js +13 -0
- package/src/offline/kit.js +121 -0
- package/src/offline/outbox.js +83 -0
- package/src/offline/store.js +26 -0
- package/src/offline/syncer.js +55 -0
- package/src/server/index.js +5 -1
- package/src/server/offline/field-lww.js +66 -0
- package/src/server/offline/index.js +1 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Per-field last-write-wins resolution for offline replays.
|
|
2
|
+
// Pure builder (testable) + a thin applyFieldLWW that does the DB round-trip.
|
|
3
|
+
|
|
4
|
+
export function buildFieldLWWUpdate({ fields, fieldTimes = {}, storedFieldTs = {} }) {
|
|
5
|
+
const winners = {};
|
|
6
|
+
const nextFieldTs = { ...storedFieldTs };
|
|
7
|
+
for (const col of Object.keys(fields)) {
|
|
8
|
+
const incomingTs = Number.isFinite(fieldTimes[col]) ? fieldTimes[col] : 0;
|
|
9
|
+
const storedTs = Number.isFinite(storedFieldTs[col]) ? storedFieldTs[col] : -Infinity;
|
|
10
|
+
if (incomingTs >= storedTs) {
|
|
11
|
+
winners[col] = fields[col];
|
|
12
|
+
nextFieldTs[col] = incomingTs;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return { winners, nextFieldTs };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function applyFieldLWW(db, {
|
|
19
|
+
table, id, tenantId, fields, fieldTimes,
|
|
20
|
+
idCol = 'id', tenantCol = 'tenant_id', fieldTsCol = 'field_ts',
|
|
21
|
+
}) {
|
|
22
|
+
assertSafeIdent(table);
|
|
23
|
+
assertSafeIdent(idCol);
|
|
24
|
+
assertSafeIdent(tenantCol);
|
|
25
|
+
assertSafeIdent(fieldTsCol);
|
|
26
|
+
|
|
27
|
+
const readSql = `SELECT ${fieldTsCol} AS field_ts FROM ${table} WHERE ${idCol} = $1 AND ${tenantCol} = $2`;
|
|
28
|
+
const read = await db.query(readSql, [id, tenantId]);
|
|
29
|
+
if (read.rows.length === 0) {
|
|
30
|
+
return { applied: [], skipped: Object.keys(fields), notFound: true };
|
|
31
|
+
}
|
|
32
|
+
const storedFieldTs = read.rows[0].field_ts || {};
|
|
33
|
+
const { winners, nextFieldTs } = buildFieldLWWUpdate({ fields, fieldTimes, storedFieldTs });
|
|
34
|
+
|
|
35
|
+
const winnerCols = Object.keys(winners);
|
|
36
|
+
if (winnerCols.length === 0) {
|
|
37
|
+
return { applied: [], skipped: Object.keys(fields) };
|
|
38
|
+
}
|
|
39
|
+
winnerCols.forEach(assertSafeIdent);
|
|
40
|
+
|
|
41
|
+
const sets = [];
|
|
42
|
+
const params = [];
|
|
43
|
+
let p = 1;
|
|
44
|
+
for (const col of winnerCols) {
|
|
45
|
+
sets.push(`${col} = $${p++}`);
|
|
46
|
+
params.push(winners[col]);
|
|
47
|
+
}
|
|
48
|
+
sets.push(`${fieldTsCol} = $${p++}`);
|
|
49
|
+
params.push(JSON.stringify(nextFieldTs));
|
|
50
|
+
sets.push(`updated_at = now()`);
|
|
51
|
+
params.push(id); const idParam = p++;
|
|
52
|
+
params.push(tenantId); const tenantParam = p++;
|
|
53
|
+
|
|
54
|
+
const sql = `UPDATE ${table} SET ${sets.join(', ')} WHERE ${idCol} = $${idParam} AND ${tenantCol} = $${tenantParam}`;
|
|
55
|
+
await db.query(sql, params);
|
|
56
|
+
|
|
57
|
+
const skipped = Object.keys(fields).filter((c) => !winnerCols.includes(c));
|
|
58
|
+
return { applied: winnerCols, skipped };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Column/table names come from server code (never user input), but guard anyway.
|
|
62
|
+
function assertSafeIdent(name) {
|
|
63
|
+
if (!/^[a-z_][a-z0-9_]*$/i.test(name)) {
|
|
64
|
+
throw new Error(`Unsafe SQL identifier: ${name}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { buildFieldLWWUpdate, applyFieldLWW } from './field-lww.js';
|