Skip to content

Commit c014e20

Browse files
committed
Common types for web hooks
1 parent 5610fa2 commit c014e20

File tree

8 files changed

+3480
-3842
lines changed

8 files changed

+3480
-3842
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dexie-cloud-common",
3-
"version": "1.0.53",
3+
"version": "1.0.57",
44
"description": "Library for shared code between dexie-cloud-addon, dexie-cloud (CLI) and dexie-cloud-server",
55
"type": "module",
66
"module": "dist/index.js",
@@ -15,13 +15,13 @@
1515
"author": "David Fahlander",
1616
"license": "Apache-2.0",
1717
"devDependencies": {
18-
"@types/jest": "^29.5.2",
19-
"@types/node": "^18.11.18",
20-
"jest": "^29.7.0",
21-
"ts-jest": "^29.2.5",
22-
"typescript": "^5.3.3"
18+
"@types/jest": "^30.0.0",
19+
"@types/node": "^24.10.1",
20+
"jest": "^30.2.0",
21+
"ts-jest": "^29.4.6",
22+
"typescript": "^5.9.3"
2323
},
2424
"dependencies": {
25-
"lib0": "^0.2.97"
25+
"lib0": "^0.2.114"
2626
}
2727
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export type SyncChange = UpsertSyncChange | UpdateSyncChange | DeleteSyncChange;
2+
3+
export interface UpsertSyncChange {
4+
type: 'upsert';
5+
realmId: string;
6+
table: string;
7+
key: string;
8+
value: object;
9+
}
10+
export interface UpdateSyncChange {
11+
type: 'update';
12+
realmId: string;
13+
table: string;
14+
key: string;
15+
updates: { [keyPath: string]: any };
16+
}
17+
export interface DeleteSyncChange {
18+
type: 'delete';
19+
realmId: string;
20+
table: string;
21+
key: string;
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type SyncChangeSet = {
2+
[table: string]: {
3+
upsert?: object[];
4+
update?: {
5+
[key: string]: { [keyPath: string]: any };
6+
};
7+
delete?: string[];
8+
};
9+
};

libs/dexie-cloud-common/src/change-processing/toDBOperationSet.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { DBDeleteOperation, DBOperation, DBUpdateOperation, DBUpsertOperation } from "../DBOperation.js";
22
import { DBOperationsSet } from "../DBOperationsSet.js";
3-
import { randomString } from "../utils.js";
4-
import { DBKeyMutation } from "./DBKeyMutation.js";
53
import { DBKeyMutationSet } from "./DBKeyMutationSet.js";
64

75
/** Convert a DBKeyMutationSet (which is an internal format capable of looking up changes per ID)
@@ -10,10 +8,7 @@ import { DBKeyMutationSet } from "./DBKeyMutationSet.js";
108
* @param inSet
119
* @returns DBOperationsSet representing inSet
1210
*/
13-
export function toDBOperationSet(inSet: DBKeyMutationSet, txid=""): DBOperationsSet<string> {
14-
// Fictive transaction:
15-
if (!txid) txid = randomString(16);
16-
11+
export function toDBOperationSet(inSet: DBKeyMutationSet, txid?: string): DBOperationsSet<string> {
1712
// Convert data into a temporary map to collect mutations of same table and type
1813
const map: {
1914
[table: string]: { [opType: string]: { key: any, val?: any, mod?: any }[] };
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { DBDeleteOperation, DBOperation, DBUpdateOperation, DBUpsertOperation } from "../DBOperation.js";
2+
import { DBOperationsSet } from "../DBOperationsSet.js";
3+
import { SyncChangeSet } from "../SyncChangeSet.js";
4+
import { DBKeyMutationSet } from "./DBKeyMutationSet.js";
5+
6+
/** Convert a DBKeyMutationSet (which is an internal format capable of looking up changes per ID)
7+
* ...into a SyncChangeSet (which is more optimal for performing DB operations into DB SQL UPSERT, UPDATE, DELETE)
8+
*
9+
* @param inSet
10+
* @returns SyncChangeSet representing inSet
11+
*/
12+
export function toSyncChangeSet(inSet: DBKeyMutationSet): SyncChangeSet {
13+
// Convert data into a temporary map to collect mutations of same table and type
14+
const map: {
15+
[table: string]: { [opType: string]: { key: any, val?: any, mod?: any }[] };
16+
} = {};
17+
for (const [table, ops] of Object.entries(inSet)) {
18+
for (const [key, op] of Object.entries(ops)) {
19+
const mapEntry = map[table] || (map[table] = {});
20+
const ops = mapEntry[op.type] || (mapEntry[op.type] = []);
21+
ops.push({ key, ...op }); // DBKeyMutation doesn't contain key, so we need to bring it in.
22+
}
23+
}
24+
25+
// Start computing the resulting format:
26+
const result: SyncChangeSet = {};
27+
28+
for (const [table, ops] of Object.entries(map)) {
29+
const resultEntry: {
30+
upsert?: object[];
31+
update?: { [key: string]: { [keyPath: string]: any } };
32+
delete?: string[];
33+
} = {};
34+
35+
for (const [optype, muts] of Object.entries(ops)) {
36+
switch (optype) {
37+
case "ups": {
38+
resultEntry.upsert = muts.map(mut => mut.val);
39+
break;
40+
}
41+
case "upd": {
42+
const updateMap: { [key: string]: { [keyPath: string]: any } } = {};
43+
for (const mut of muts) {
44+
updateMap[mut.key] = mut.mod;
45+
}
46+
resultEntry.update = updateMap;
47+
break;
48+
}
49+
case "del": {
50+
resultEntry.delete = muts.map(mut => mut.key);
51+
break;
52+
}
53+
}
54+
}
55+
56+
result[table] = resultEntry;
57+
}
58+
59+
return result;
60+
}

libs/dexie-cloud-common/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ export * from "./utils.js"
66
export * from "./DexieCloudSchema.js"
77
export * from "./DBOperationsSet.js"
88
export * from "./DBOperation.js"
9+
export * from "./SyncChange.js"
910
export * from "./validation/isValidSyncableID.js"
1011
export * from "./change-processing/applyOperation.js"
1112
export * from "./change-processing/applyOperations.js"
1213
export * from "./change-processing/DBKeyMutation.js"
1314
export * from "./change-processing/DBKeyMutationSet.js"
1415
export * from "./change-processing/subtractChanges.js"
1516
export * from "./change-processing/toDBOperationSet.js"
17+
export * from "./change-processing/toSyncChangeSet.js"
1618
export * from "./DBPermissionSet.js";
1719
export * from "./entities/DBRealm.js";
1820
export * from "./entities/DBRealmMember.js";

libs/dexie-cloud-common/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"lib": ["es2020", "dom"],
44
"moduleResolution": "node",
55
"importHelpers": true,
6-
"target": "es2017",
6+
"target": "es2020",
77
"module": "esnext",
88
"outDir": "dist",
99
"rootDir": "src",

0 commit comments

Comments
 (0)