@jcbuisson/express-x 3.0.3 → 3.0.4
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/.claude/settings.local.json +3 -1
- package/package.json +18 -3
- package/src/server.mjs +58 -0
- package/test/round-trip.test.mjs +1368 -0
- package/test/sync.test.mjs +127 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jcbuisson/express-x",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/server.mjs",
|
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
"publishConfig": {
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
|
+
"imports": {
|
|
18
|
+
"#root/*": "./*"
|
|
19
|
+
},
|
|
17
20
|
"scripts": {
|
|
18
|
-
"
|
|
19
|
-
"migrate": "npx prisma migrate dev --name init"
|
|
21
|
+
"test": "node --import tsx/esm --test --test-force-exit test/round-trip.test.mjs"
|
|
20
22
|
},
|
|
21
23
|
"keywords": [],
|
|
22
24
|
"dependencies": {
|
|
@@ -24,5 +26,18 @@
|
|
|
24
26
|
"config": "^3.3.9",
|
|
25
27
|
"express": "^4.18.2",
|
|
26
28
|
"socket.io": "^4.6.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@electric-sql/pglite": "^0.4.5",
|
|
32
|
+
"@jcbuisson/express-x-client": "^3.0.5",
|
|
33
|
+
"@jcbuisson/express-x-drizzle": "^1.0.5",
|
|
34
|
+
"@vueuse/core": "^14.3.0",
|
|
35
|
+
"dexie": "^4.4.2",
|
|
36
|
+
"drizzle-orm": "^0.45.2",
|
|
37
|
+
"fake-indexeddb": "^6.2.5",
|
|
38
|
+
"rxjs": "^7.8.2",
|
|
39
|
+
"socket.io-client": "^4.8.3",
|
|
40
|
+
"tsx": "^4.22.1",
|
|
41
|
+
"uuid": "^14.0.0"
|
|
27
42
|
}
|
|
28
43
|
}
|
package/src/server.mjs
CHANGED
|
@@ -48,6 +48,64 @@ export class Mutex {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
////////////////////////// SYNC ALGORITHM (common to all offline plugins) //////////////////////////
|
|
52
|
+
|
|
53
|
+
export function computeSyncResult(databaseValuesDict, clientMetadataDict, databaseMetadataDict) {
|
|
54
|
+
const onlyDatabaseIds = new Set()
|
|
55
|
+
const onlyClientIds = new Set()
|
|
56
|
+
const databaseAndClientIds = new Set()
|
|
57
|
+
|
|
58
|
+
for (const uid in databaseValuesDict) {
|
|
59
|
+
if (uid in clientMetadataDict) databaseAndClientIds.add(uid)
|
|
60
|
+
else onlyDatabaseIds.add(uid)
|
|
61
|
+
}
|
|
62
|
+
for (const uid in clientMetadataDict) {
|
|
63
|
+
if (uid in databaseValuesDict) databaseAndClientIds.add(uid)
|
|
64
|
+
else onlyClientIds.add(uid)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const addDatabase = [], updateDatabase = [], deleteDatabase = []
|
|
68
|
+
const addClient = [], updateClient = [], deleteClient = []
|
|
69
|
+
|
|
70
|
+
for (const uid of onlyDatabaseIds) {
|
|
71
|
+
const databaseMetaData = databaseMetadataDict[uid] || { uid, created_at: null }
|
|
72
|
+
addClient.push([databaseValuesDict[uid], databaseMetaData])
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (const uid of onlyClientIds) {
|
|
76
|
+
const clientMetaData = clientMetadataDict[uid]
|
|
77
|
+
if (clientMetaData.deleted_at) {
|
|
78
|
+
deleteClient.push([uid, clientMetaData.deleted_at])
|
|
79
|
+
} else {
|
|
80
|
+
addDatabase.push(clientMetaData)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const uid of databaseAndClientIds) {
|
|
85
|
+
const clientMetaData = clientMetadataDict[uid]
|
|
86
|
+
if (clientMetaData.deleted_at) {
|
|
87
|
+
deleteDatabase.push(uid)
|
|
88
|
+
deleteClient.push([uid, clientMetaData.deleted_at])
|
|
89
|
+
} else {
|
|
90
|
+
const databaseMetaData = databaseMetadataDict[uid] || { uid, created_at: null }
|
|
91
|
+
const clientUpdatedAt = new Date(clientMetaData.updated_at || clientMetaData.created_at)
|
|
92
|
+
const databaseUpdatedAt = new Date(databaseMetaData.updated_at || databaseMetaData.created_at)
|
|
93
|
+
const diff = clientUpdatedAt - databaseUpdatedAt
|
|
94
|
+
if (diff > 0) updateDatabase.push(clientMetaData)
|
|
95
|
+
else if (diff < 0) updateClient.push([databaseValuesDict[uid], databaseMetaData])
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
addClient,
|
|
101
|
+
updateClient,
|
|
102
|
+
deleteClient,
|
|
103
|
+
addDatabase,
|
|
104
|
+
updateDatabase,
|
|
105
|
+
deleteDatabase,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
51
109
|
////////////////////////// EXPRESSX //////////////////////////
|
|
52
110
|
|
|
53
111
|
export function expressX(config) {
|