@slimr/dbsync 0.0.3
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/README.md +183 -0
- package/cjs/DbRepository.d.ts +59 -0
- package/cjs/DbRepository.js +78 -0
- package/cjs/DbRepository.js.map +1 -0
- package/cjs/DbSync.d.ts +51 -0
- package/cjs/DbSync.js +123 -0
- package/cjs/DbSync.js.map +1 -0
- package/cjs/DbTransaction.d.ts +60 -0
- package/cjs/DbTransaction.js +82 -0
- package/cjs/DbTransaction.js.map +1 -0
- package/cjs/DbTxRepository.d.ts +41 -0
- package/cjs/DbTxRepository.js +56 -0
- package/cjs/DbTxRepository.js.map +1 -0
- package/cjs/MigrationManager.d.ts +60 -0
- package/cjs/MigrationManager.js +69 -0
- package/cjs/MigrationManager.js.map +1 -0
- package/cjs/Repository.d.ts +59 -0
- package/cjs/Repository.js +78 -0
- package/cjs/Repository.js.map +1 -0
- package/cjs/adapters/RestAdapter.d.ts +13 -0
- package/cjs/adapters/RestAdapter.js +47 -0
- package/cjs/adapters/RestAdapter.js.map +1 -0
- package/cjs/adapters/index.d.ts +2 -0
- package/cjs/adapters/index.js +19 -0
- package/cjs/adapters/index.js.map +1 -0
- package/cjs/adapters/types.d.ts +11 -0
- package/cjs/adapters/types.js +3 -0
- package/cjs/adapters/types.js.map +1 -0
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +20 -0
- package/cjs/index.js.map +1 -0
- package/cjs/internal/AuthManager.d.ts +13 -0
- package/cjs/internal/AuthManager.js +34 -0
- package/cjs/internal/AuthManager.js.map +1 -0
- package/cjs/internal/DbTransaction.d.ts +60 -0
- package/cjs/internal/DbTransaction.js +82 -0
- package/cjs/internal/DbTransaction.js.map +1 -0
- package/cjs/internal/EventBus.d.ts +16 -0
- package/cjs/internal/EventBus.js +41 -0
- package/cjs/internal/EventBus.js.map +1 -0
- package/cjs/internal/MigrationManager.d.ts +60 -0
- package/cjs/internal/MigrationManager.js +69 -0
- package/cjs/internal/MigrationManager.js.map +1 -0
- package/cjs/internal/StorageManager.d.ts +20 -0
- package/cjs/internal/StorageManager.js +148 -0
- package/cjs/internal/StorageManager.js.map +1 -0
- package/cjs/internal/SyncEngine.d.ts +27 -0
- package/cjs/internal/SyncEngine.js +191 -0
- package/cjs/internal/SyncEngine.js.map +1 -0
- package/cjs/internal/utils.d.ts +1 -0
- package/cjs/internal/utils.js +7 -0
- package/cjs/internal/utils.js.map +1 -0
- package/cjs/react.d.ts +1 -0
- package/cjs/react.js +18 -0
- package/cjs/react.js.map +1 -0
- package/cjs/useDbQuery.d.ts +13 -0
- package/cjs/useDbQuery.js +59 -0
- package/cjs/useDbQuery.js.map +1 -0
- package/cjs/util/promiseWithResolvers.d.ts +11 -0
- package/cjs/util/promiseWithResolvers.js +19 -0
- package/cjs/util/promiseWithResolvers.js.map +1 -0
- package/esm/DbRepository.d.ts +59 -0
- package/esm/DbRepository.js +74 -0
- package/esm/DbRepository.js.map +1 -0
- package/esm/DbSync.d.ts +51 -0
- package/esm/DbSync.js +119 -0
- package/esm/DbSync.js.map +1 -0
- package/esm/DbTransaction.d.ts +60 -0
- package/esm/DbTransaction.js +78 -0
- package/esm/DbTransaction.js.map +1 -0
- package/esm/DbTxRepository.d.ts +41 -0
- package/esm/DbTxRepository.js +52 -0
- package/esm/DbTxRepository.js.map +1 -0
- package/esm/MigrationManager.d.ts +60 -0
- package/esm/MigrationManager.js +65 -0
- package/esm/MigrationManager.js.map +1 -0
- package/esm/Repository.d.ts +59 -0
- package/esm/Repository.js +74 -0
- package/esm/Repository.js.map +1 -0
- package/esm/adapters/RestAdapter.d.ts +13 -0
- package/esm/adapters/RestAdapter.js +43 -0
- package/esm/adapters/RestAdapter.js.map +1 -0
- package/esm/adapters/index.d.ts +2 -0
- package/esm/adapters/index.js +3 -0
- package/esm/adapters/index.js.map +1 -0
- package/esm/adapters/types.d.ts +11 -0
- package/esm/adapters/types.js +2 -0
- package/esm/adapters/types.js.map +1 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +4 -0
- package/esm/index.js.map +1 -0
- package/esm/internal/AuthManager.d.ts +13 -0
- package/esm/internal/AuthManager.js +30 -0
- package/esm/internal/AuthManager.js.map +1 -0
- package/esm/internal/DbTransaction.d.ts +60 -0
- package/esm/internal/DbTransaction.js +78 -0
- package/esm/internal/DbTransaction.js.map +1 -0
- package/esm/internal/EventBus.d.ts +16 -0
- package/esm/internal/EventBus.js +37 -0
- package/esm/internal/EventBus.js.map +1 -0
- package/esm/internal/MigrationManager.d.ts +60 -0
- package/esm/internal/MigrationManager.js +65 -0
- package/esm/internal/MigrationManager.js.map +1 -0
- package/esm/internal/StorageManager.d.ts +20 -0
- package/esm/internal/StorageManager.js +144 -0
- package/esm/internal/StorageManager.js.map +1 -0
- package/esm/internal/SyncEngine.d.ts +27 -0
- package/esm/internal/SyncEngine.js +187 -0
- package/esm/internal/SyncEngine.js.map +1 -0
- package/esm/internal/utils.d.ts +1 -0
- package/esm/internal/utils.js +4 -0
- package/esm/internal/utils.js.map +1 -0
- package/esm/react.d.ts +1 -0
- package/esm/react.js +2 -0
- package/esm/react.js.map +1 -0
- package/esm/useDbQuery.d.ts +13 -0
- package/esm/useDbQuery.js +56 -0
- package/esm/useDbQuery.js.map +1 -0
- package/esm/util/promiseWithResolvers.d.ts +11 -0
- package/esm/util/promiseWithResolvers.js +16 -0
- package/esm/util/promiseWithResolvers.js.map +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SyncEngine = void 0;
|
|
4
|
+
const promiseWithResolvers_js_1 = require("../util/promiseWithResolvers.js");
|
|
5
|
+
class SyncEngine {
|
|
6
|
+
config;
|
|
7
|
+
syncInterval;
|
|
8
|
+
events;
|
|
9
|
+
storage;
|
|
10
|
+
auth;
|
|
11
|
+
adapter;
|
|
12
|
+
onSchemaChange;
|
|
13
|
+
syncSetInterval = null;
|
|
14
|
+
constructor(config, syncInterval, events, storage, auth, adapter, onSchemaChange) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.syncInterval = syncInterval;
|
|
17
|
+
this.events = events;
|
|
18
|
+
this.storage = storage;
|
|
19
|
+
this.auth = auth;
|
|
20
|
+
this.adapter = adapter;
|
|
21
|
+
this.onSchemaChange = onSchemaChange;
|
|
22
|
+
}
|
|
23
|
+
enable() {
|
|
24
|
+
if (!this.syncSetInterval) {
|
|
25
|
+
this.syncSetInterval = setInterval(() => this.sync(), this.syncInterval);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
disable() {
|
|
29
|
+
if (this.syncSetInterval) {
|
|
30
|
+
clearInterval(this.syncSetInterval);
|
|
31
|
+
this.syncSetInterval = null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
get isEnabled() {
|
|
35
|
+
return !!this.syncSetInterval;
|
|
36
|
+
}
|
|
37
|
+
get isLive() {
|
|
38
|
+
const lastSuccess = localStorage.getItem("dbsync-lastSuccessAt");
|
|
39
|
+
if (!lastSuccess)
|
|
40
|
+
return false;
|
|
41
|
+
return Date.now() - new Date(lastSuccess).getTime() < this.syncInterval * 4;
|
|
42
|
+
}
|
|
43
|
+
async waitForLive() {
|
|
44
|
+
const { promise, resolve, reject } = (0, promiseWithResolvers_js_1.promiseWithResolvers)();
|
|
45
|
+
if (!this.isEnabled) {
|
|
46
|
+
reject(new Error("Sync disabled"));
|
|
47
|
+
return promise;
|
|
48
|
+
}
|
|
49
|
+
const check = setInterval(() => {
|
|
50
|
+
if (this.isLive) {
|
|
51
|
+
clearInterval(check);
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
}, this.syncInterval);
|
|
55
|
+
return promise;
|
|
56
|
+
}
|
|
57
|
+
async triggerSync() {
|
|
58
|
+
await this.sync();
|
|
59
|
+
}
|
|
60
|
+
async sync() {
|
|
61
|
+
if (typeof navigator !== "undefined" && navigator.locks) {
|
|
62
|
+
await navigator.locks.request("dbsync-leader", async () => {
|
|
63
|
+
await this.performSync();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
await this.performSync();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async performSync() {
|
|
71
|
+
this.events.setState("syncing");
|
|
72
|
+
try {
|
|
73
|
+
await this.syncPull();
|
|
74
|
+
await this.syncPush();
|
|
75
|
+
this.events.setState("idle");
|
|
76
|
+
this.auth.isAuth = true;
|
|
77
|
+
localStorage.setItem("dbsync-lastSuccessAt", new Date().toISOString());
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
if (err.status === 401) {
|
|
81
|
+
this.auth.isAuth = false;
|
|
82
|
+
this.disable();
|
|
83
|
+
}
|
|
84
|
+
this.events.setState("error");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
get schemaSignature() {
|
|
88
|
+
const tables = Object.keys(this.config.tables)
|
|
89
|
+
.sort()
|
|
90
|
+
.map((table) => {
|
|
91
|
+
return {
|
|
92
|
+
table,
|
|
93
|
+
indexes: this.config.tables[table].indexes?.slice().sort() || [],
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
return JSON.stringify(tables);
|
|
97
|
+
}
|
|
98
|
+
async syncPull() {
|
|
99
|
+
let hasMore = true;
|
|
100
|
+
while (hasMore) {
|
|
101
|
+
const cursor = localStorage.getItem("dbsync-pullSyncedUpTo") || "";
|
|
102
|
+
const data = await this.adapter.pull(cursor);
|
|
103
|
+
if (data.items && data.items.length > 0) {
|
|
104
|
+
const tx = this.storage.getTransaction();
|
|
105
|
+
data.items.forEach((post) => {
|
|
106
|
+
if (post.variant === "__dbsync_system" && post.id === "version") {
|
|
107
|
+
const remoteContent = JSON.parse(post.content);
|
|
108
|
+
if (this.config.version !== undefined) {
|
|
109
|
+
if (remoteContent.version && remoteContent.version > this.config.version) {
|
|
110
|
+
this.onSchemaChange();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const currentSignature = this.schemaSignature;
|
|
115
|
+
if (remoteContent.signature && remoteContent.signature !== currentSignature) {
|
|
116
|
+
this.onSchemaChange();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const storeName = post.variant;
|
|
122
|
+
if (post.isDeleted)
|
|
123
|
+
tx.delete(storeName, post.id);
|
|
124
|
+
else
|
|
125
|
+
tx.put(storeName, { id: post.id, ...JSON.parse(post.content) });
|
|
126
|
+
});
|
|
127
|
+
await tx.commit();
|
|
128
|
+
localStorage.setItem("dbsync-pullSyncedUpTo", data.items[data.items.length - 1].updatedAt);
|
|
129
|
+
}
|
|
130
|
+
hasMore = data.hasMore;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async syncPush() {
|
|
134
|
+
const dirty = await this.storage.findAll("dirtyQueue");
|
|
135
|
+
const deleted = await this.storage.findAll("deletedQueue");
|
|
136
|
+
const payload = [
|
|
137
|
+
...dirty.map((d) => ({
|
|
138
|
+
id: d.id,
|
|
139
|
+
variant: d.table,
|
|
140
|
+
content: JSON.stringify(d.payload),
|
|
141
|
+
isDeleted: false,
|
|
142
|
+
updatedAt: new Date(d.timestamp).toISOString(),
|
|
143
|
+
})),
|
|
144
|
+
...deleted.map((d) => ({
|
|
145
|
+
id: d.id,
|
|
146
|
+
variant: d.table,
|
|
147
|
+
content: "{}",
|
|
148
|
+
isDeleted: true,
|
|
149
|
+
updatedAt: new Date(d.timestamp).toISOString(),
|
|
150
|
+
})),
|
|
151
|
+
];
|
|
152
|
+
if (this.config.version !== undefined) {
|
|
153
|
+
const localVersion = Number(localStorage.getItem("dbsync-version") || "0");
|
|
154
|
+
if (this.config.version > localVersion) {
|
|
155
|
+
payload.push({
|
|
156
|
+
id: "version",
|
|
157
|
+
variant: "__dbsync_system",
|
|
158
|
+
content: JSON.stringify({ version: this.config.version }),
|
|
159
|
+
isDeleted: false,
|
|
160
|
+
updatedAt: new Date().toISOString(),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const currentSignature = this.schemaSignature;
|
|
166
|
+
const storedSyncSig = localStorage.getItem("dbsync-sync-signature") || "";
|
|
167
|
+
if (currentSignature !== storedSyncSig) {
|
|
168
|
+
payload.push({
|
|
169
|
+
id: "version",
|
|
170
|
+
variant: "__dbsync_system",
|
|
171
|
+
content: JSON.stringify({ signature: currentSignature }),
|
|
172
|
+
isDeleted: false,
|
|
173
|
+
updatedAt: new Date().toISOString(),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (payload.length > 0) {
|
|
178
|
+
await this.adapter.push(payload);
|
|
179
|
+
if (this.config.version !== undefined)
|
|
180
|
+
localStorage.setItem("dbsync-version", String(this.config.version));
|
|
181
|
+
else
|
|
182
|
+
localStorage.setItem("dbsync-sync-signature", this.schemaSignature);
|
|
183
|
+
const tx = this.storage.getTransaction();
|
|
184
|
+
dirty.forEach((d) => tx.delete("dirtyQueue", d.id));
|
|
185
|
+
deleted.forEach((d) => tx.delete("deletedQueue", d.id));
|
|
186
|
+
await tx.commit();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
exports.SyncEngine = SyncEngine;
|
|
191
|
+
//# sourceMappingURL=SyncEngine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SyncEngine.js","sourceRoot":"","sources":["../../src/internal/SyncEngine.ts"],"names":[],"mappings":";;;AAEA,6EAAsE;AAKtE,MAAa,UAAU;IAIb;IACA;IACA;IACA;IACA;IACA;IACA;IATD,eAAe,GAAQ,IAAI,CAAA;IAEnC,YACS,MAAoB,EACpB,YAAoB,EACpB,MAAgB,EAChB,OAAuB,EACvB,IAAiB,EACjB,OAAuB,EACvB,cAA0B;QAN1B,WAAM,GAAN,MAAM,CAAc;QACpB,iBAAY,GAAZ,YAAY,CAAQ;QAC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"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/internal/utils.ts"],"names":[],"mappings":";;AAAA,0BAEC;AAFD,SAAgB,OAAO;IACtB,OAAO,MAAM,CAAC,UAAU,EAAE,CAAA;AAC3B,CAAC"}
|
package/cjs/react.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useDbQuery.js";
|
package/cjs/react.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useDbQuery.js"), exports);
|
|
18
|
+
//# sourceMappingURL=react.js.map
|
package/cjs/react.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DbSync } from "./DbSync.js";
|
|
2
|
+
/**
|
|
3
|
+
* A React hook that subscribes to one or more `DbSync` tables and natively
|
|
4
|
+
* re-evaluates a strictly typed query function whenever local mutations or
|
|
5
|
+
* background sync events alter the underlying data.
|
|
6
|
+
*
|
|
7
|
+
* @param db The initialized `DbSync` instance.
|
|
8
|
+
* @param stores A string or array of strings containing the table names the query function reads from.
|
|
9
|
+
* @param queryFn An asynchronous function invoked to pull data from IndexedDB.
|
|
10
|
+
* @param deps A standard React dependency array for parameters referenced inside the `queryFn`.
|
|
11
|
+
* @returns The resolved data of the `queryFn`, or `undefined` while the initial fetch evaluates.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useDbQuery<T>(db: DbSync, stores: string | string[], queryFn: () => Promise<T>, deps?: any[]): T | undefined;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDbQuery = useDbQuery;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* A React hook that subscribes to one or more `DbSync` tables and natively
|
|
7
|
+
* re-evaluates a strictly typed query function whenever local mutations or
|
|
8
|
+
* background sync events alter the underlying data.
|
|
9
|
+
*
|
|
10
|
+
* @param db The initialized `DbSync` instance.
|
|
11
|
+
* @param stores A string or array of strings containing the table names the query function reads from.
|
|
12
|
+
* @param queryFn An asynchronous function invoked to pull data from IndexedDB.
|
|
13
|
+
* @param deps A standard React dependency array for parameters referenced inside the `queryFn`.
|
|
14
|
+
* @returns The resolved data of the `queryFn`, or `undefined` while the initial fetch evaluates.
|
|
15
|
+
*/
|
|
16
|
+
function useDbQuery(db, stores, queryFn, deps = []) {
|
|
17
|
+
const [data, setData] = (0, react_1.useState)(undefined);
|
|
18
|
+
const storeArray = Array.isArray(stores) ? stores : [stores];
|
|
19
|
+
(0, react_1.useEffect)(() => {
|
|
20
|
+
let isMounted = true;
|
|
21
|
+
const fetchData = async () => {
|
|
22
|
+
if (!db.initted) {
|
|
23
|
+
// If attempting to query before init(), safely wait
|
|
24
|
+
await new Promise((resolve) => {
|
|
25
|
+
const check = setInterval(() => {
|
|
26
|
+
if (!isMounted || db.initted) {
|
|
27
|
+
clearInterval(check);
|
|
28
|
+
resolve();
|
|
29
|
+
}
|
|
30
|
+
}, 50);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const result = await queryFn();
|
|
35
|
+
if (isMounted)
|
|
36
|
+
setData(result);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
console.error("[dbsync useDbQuery]: Query failed", err);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
// Initial fetch
|
|
43
|
+
fetchData();
|
|
44
|
+
// Subscribe to relevant stores
|
|
45
|
+
const sub = db.subscribe((updatedStores) => {
|
|
46
|
+
const shouldUpdate = updatedStores.some((s) => storeArray.includes(s));
|
|
47
|
+
if (shouldUpdate) {
|
|
48
|
+
fetchData();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return () => {
|
|
52
|
+
isMounted = false;
|
|
53
|
+
sub.close();
|
|
54
|
+
};
|
|
55
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
56
|
+
}, [db, JSON.stringify(storeArray), ...deps]);
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=useDbQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDbQuery.js","sourceRoot":"","sources":["../src/useDbQuery.ts"],"names":[],"mappings":";;AAcA,gCAmDC;AAjED,iCAA2C;AAG3C;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CACzB,EAAU,EACV,MAAyB,EACzB,OAAyB,EACzB,OAAc,EAAE;IAEhB,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAAgB,SAAS,CAAC,CAAA;IAC1D,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAA;IAE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,CAAC,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAA;IAE7C,OAAO,IAAI,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise together with its external resolve and reject handlers.
|
|
3
|
+
*
|
|
4
|
+
* This mirrors `Promise.withResolvers()` in environments that do not provide
|
|
5
|
+
* the built-in helper yet.
|
|
6
|
+
*/
|
|
7
|
+
export declare function promiseWithResolvers<T>(): {
|
|
8
|
+
promise: Promise<T>;
|
|
9
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
10
|
+
reject: (reason?: any) => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseWithResolvers = promiseWithResolvers;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a promise together with its external resolve and reject handlers.
|
|
6
|
+
*
|
|
7
|
+
* This mirrors `Promise.withResolvers()` in environments that do not provide
|
|
8
|
+
* the built-in helper yet.
|
|
9
|
+
*/
|
|
10
|
+
function promiseWithResolvers() {
|
|
11
|
+
let resolve;
|
|
12
|
+
let reject;
|
|
13
|
+
const promise = new Promise((res, rej) => {
|
|
14
|
+
resolve = res;
|
|
15
|
+
reject = rej;
|
|
16
|
+
});
|
|
17
|
+
return { promise, resolve, reject };
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=promiseWithResolvers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promiseWithResolvers.js","sourceRoot":"","sources":["../../src/util/promiseWithResolvers.ts"],"names":[],"mappings":";;AAMA,oDAQC;AAdD;;;;;GAKG;AACH,SAAgB,oBAAoB;IACnC,IAAI,OAA6C,CAAA;IACjD,IAAI,MAA+B,CAAA;IACnC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC3C,OAAO,GAAG,GAAG,CAAA;QACb,MAAM,GAAG,GAAG,CAAA;IACb,CAAC,CAAC,CAAA;IACF,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAA;AACpC,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { DbSync } from "./DbSync.js";
|
|
2
|
+
/**
|
|
3
|
+
* A typed repository wrapper for a single `DbSync` object store.
|
|
4
|
+
* Provides a localized CRUD interface to minimize boilerplate and
|
|
5
|
+
* enforce consistent data types for a specific table.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DbRepository<T> {
|
|
8
|
+
protected db: DbSync;
|
|
9
|
+
readonly storeName: string;
|
|
10
|
+
/**
|
|
11
|
+
* Initializes a new repository tied to a specific object store.
|
|
12
|
+
*
|
|
13
|
+
* @param db The centralized `DbSync` instance.
|
|
14
|
+
* @param storeName The name of the object store this repository will manage.
|
|
15
|
+
*/
|
|
16
|
+
constructor(db: DbSync, storeName: string);
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves a single record by its primary key.
|
|
19
|
+
*
|
|
20
|
+
* @param id The primary key of the record to retrieve.
|
|
21
|
+
* @returns A promise resolving to the typed record, or `undefined` if not found.
|
|
22
|
+
*/
|
|
23
|
+
findById(id: string | number): Promise<T | undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves all records currently held in the object store.
|
|
26
|
+
*
|
|
27
|
+
* @returns A promise resolving to an array of all typed records.
|
|
28
|
+
*/
|
|
29
|
+
findAll(): Promise<T[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Inserts a new record into the object store.
|
|
32
|
+
*
|
|
33
|
+
* @param value The object payload to insert.
|
|
34
|
+
* @param key An optional explicit primary key.
|
|
35
|
+
* @returns A promise resolving to the newly inserted record.
|
|
36
|
+
*/
|
|
37
|
+
add(value: Partial<T>, key?: string | number): Promise<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Inserts or updates an existing record in the object store.
|
|
40
|
+
*
|
|
41
|
+
* @param value The complete object to upsert.
|
|
42
|
+
* @param key An optional explicit primary key.
|
|
43
|
+
* @returns A promise resolving to the upserted record.
|
|
44
|
+
*/
|
|
45
|
+
put(value: T, key?: string | number): Promise<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Removes a record from the object store by its primary key.
|
|
48
|
+
*
|
|
49
|
+
* @param key The primary key of the record to delete.
|
|
50
|
+
* @returns A promise resolving when the deletion is complete.
|
|
51
|
+
*/
|
|
52
|
+
delete(key: string | number): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Discards all records present in the object store.
|
|
55
|
+
*
|
|
56
|
+
* @returns A promise resolving when the store has been completely cleared.
|
|
57
|
+
*/
|
|
58
|
+
clear(): Promise<void>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A typed repository wrapper for a single `DbSync` object store.
|
|
3
|
+
* Provides a localized CRUD interface to minimize boilerplate and
|
|
4
|
+
* enforce consistent data types for a specific table.
|
|
5
|
+
*/
|
|
6
|
+
export class DbRepository {
|
|
7
|
+
db;
|
|
8
|
+
storeName;
|
|
9
|
+
/**
|
|
10
|
+
* Initializes a new repository tied to a specific object store.
|
|
11
|
+
*
|
|
12
|
+
* @param db The centralized `DbSync` instance.
|
|
13
|
+
* @param storeName The name of the object store this repository will manage.
|
|
14
|
+
*/
|
|
15
|
+
constructor(db, storeName) {
|
|
16
|
+
this.db = db;
|
|
17
|
+
this.storeName = storeName;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves a single record by its primary key.
|
|
21
|
+
*
|
|
22
|
+
* @param id The primary key of the record to retrieve.
|
|
23
|
+
* @returns A promise resolving to the typed record, or `undefined` if not found.
|
|
24
|
+
*/
|
|
25
|
+
async findById(id) {
|
|
26
|
+
return this.db.get(this.storeName, id);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves all records currently held in the object store.
|
|
30
|
+
*
|
|
31
|
+
* @returns A promise resolving to an array of all typed records.
|
|
32
|
+
*/
|
|
33
|
+
async findAll() {
|
|
34
|
+
return this.db.findAll(this.storeName);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Inserts a new record into the object store.
|
|
38
|
+
*
|
|
39
|
+
* @param value The object payload to insert.
|
|
40
|
+
* @param key An optional explicit primary key.
|
|
41
|
+
* @returns A promise resolving to the newly inserted record.
|
|
42
|
+
*/
|
|
43
|
+
async add(value, key) {
|
|
44
|
+
return this.db.add(this.storeName, value, key);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Inserts or updates an existing record in the object store.
|
|
48
|
+
*
|
|
49
|
+
* @param value The complete object to upsert.
|
|
50
|
+
* @param key An optional explicit primary key.
|
|
51
|
+
* @returns A promise resolving to the upserted record.
|
|
52
|
+
*/
|
|
53
|
+
async put(value, key) {
|
|
54
|
+
return this.db.put(this.storeName, value, key);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Removes a record from the object store by its primary key.
|
|
58
|
+
*
|
|
59
|
+
* @param key The primary key of the record to delete.
|
|
60
|
+
* @returns A promise resolving when the deletion is complete.
|
|
61
|
+
*/
|
|
62
|
+
async delete(key) {
|
|
63
|
+
return this.db.delete(this.storeName, key);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Discards all records present in the object store.
|
|
67
|
+
*
|
|
68
|
+
* @returns A promise resolving when the store has been completely cleared.
|
|
69
|
+
*/
|
|
70
|
+
async clear() {
|
|
71
|
+
return this.db.clear(this.storeName);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=DbRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DbRepository.js","sourceRoot":"","sources":["../src/DbRepository.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,YAAY;IAQb;IACM;IARjB;;;;;OAKG;IACH,YACW,EAAU,EACJ,SAAiB;QADvB,OAAE,GAAF,EAAE,CAAQ;QACJ,cAAS,GAAT,SAAS,CAAQ;IAC/B,CAAC;IAEJ;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,CAAC,EAAmB;QACjC,OAAO,IAAI,CAAC,EAAE,CAAC,GAAG,CAAI,IAAI,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA;IAC1C,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO;QACZ,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,CAAI,IAAI,CAAC,SAAS,CAAC,CAAA;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,GAAG,CAAC,KAAiB,EAAE,GAAqB;QACjD,OAAO,IAAI,CAAC,EAAE,CAAC,GAAG,CAAI,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;IAClD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,GAAG,CAAC,KAAQ,EAAE,GAAqB;QACxC,OAAO,IAAI,CAAC,EAAE,CAAC,GAAG,CAAI,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;IAClD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM,CAAC,GAAoB;QAChC,OAAO,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IAC3C,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK;QACV,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IACrC,CAAC;CACD"}
|
package/esm/DbSync.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { BackendAdapter } from "./adapters/types.js";
|
|
2
|
+
import { type SyncState } from "./internal/EventBus.js";
|
|
3
|
+
import { StorageManager } from "./internal/StorageManager.js";
|
|
4
|
+
export interface DbSyncConfig {
|
|
5
|
+
adapter: BackendAdapter;
|
|
6
|
+
version?: number;
|
|
7
|
+
tables: Record<string, {
|
|
8
|
+
indexes?: string[];
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
export type { SyncState };
|
|
12
|
+
export declare class DbSync {
|
|
13
|
+
syncInterval: number;
|
|
14
|
+
config: DbSyncConfig;
|
|
15
|
+
private events;
|
|
16
|
+
storage: StorageManager;
|
|
17
|
+
private syncEngine;
|
|
18
|
+
private authManager;
|
|
19
|
+
constructor(config: DbSyncConfig);
|
|
20
|
+
genUuid(): `${string}-${string}-${string}-${string}-${string}`;
|
|
21
|
+
get initted(): boolean;
|
|
22
|
+
init(): Promise<void>;
|
|
23
|
+
getTransaction(): import("./internal/DbTransaction.js").DbTransaction;
|
|
24
|
+
get<T>(storeName: string, id: string | number): Promise<T | undefined>;
|
|
25
|
+
findAll<T>(storeName: string): Promise<T[]>;
|
|
26
|
+
add<T>(storeName: string, value: any, key?: string | number): Promise<T>;
|
|
27
|
+
put<T>(storeName: string, value: any, key?: string | number): Promise<T>;
|
|
28
|
+
delete(storeName: string, key: string | number): Promise<void>;
|
|
29
|
+
clear(storeName: string): Promise<void>;
|
|
30
|
+
subscribe(callback: (stores: string[]) => void): {
|
|
31
|
+
close: () => boolean;
|
|
32
|
+
};
|
|
33
|
+
onSyncStateChange(callback: (state: SyncState) => void): {
|
|
34
|
+
close: () => boolean;
|
|
35
|
+
};
|
|
36
|
+
get isAuth(): boolean;
|
|
37
|
+
checkAuth(): Promise<boolean>;
|
|
38
|
+
login(email: string, code: string): Promise<void>;
|
|
39
|
+
logout(): Promise<void>;
|
|
40
|
+
reset(): Promise<void>;
|
|
41
|
+
get isEnabled(): boolean;
|
|
42
|
+
get isLive(): boolean;
|
|
43
|
+
enable(): void;
|
|
44
|
+
disable(): void;
|
|
45
|
+
waitForLive(): Promise<void>;
|
|
46
|
+
triggerSync(): Promise<void>;
|
|
47
|
+
startSyncInterval(): void;
|
|
48
|
+
stopSyncInterval(): void;
|
|
49
|
+
protected onSchemaChangeDetected(): void;
|
|
50
|
+
dispose(): void;
|
|
51
|
+
}
|
package/esm/DbSync.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { AuthManager } from "./internal/AuthManager.js";
|
|
2
|
+
import { EventBus } from "./internal/EventBus.js";
|
|
3
|
+
import { StorageManager } from "./internal/StorageManager.js";
|
|
4
|
+
import { SyncEngine } from "./internal/SyncEngine.js";
|
|
5
|
+
import { genUuid } from "./internal/utils.js";
|
|
6
|
+
export class DbSync {
|
|
7
|
+
syncInterval = 5000;
|
|
8
|
+
config;
|
|
9
|
+
// Internal managers
|
|
10
|
+
events;
|
|
11
|
+
storage; // Public for tests currently (we can wrap it later)
|
|
12
|
+
syncEngine;
|
|
13
|
+
authManager;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.events = new EventBus();
|
|
17
|
+
const adapter = config.adapter;
|
|
18
|
+
const onSchemaChange = () => this.onSchemaChangeDetected();
|
|
19
|
+
this.storage = new StorageManager(config, this.events, onSchemaChange);
|
|
20
|
+
this.authManager = new AuthManager(adapter, this.storage, () => this.disable());
|
|
21
|
+
this.syncEngine = new SyncEngine(config, this.syncInterval, this.events, this.storage, this.authManager, adapter, onSchemaChange);
|
|
22
|
+
}
|
|
23
|
+
// Facade to Utils
|
|
24
|
+
genUuid() {
|
|
25
|
+
return genUuid();
|
|
26
|
+
}
|
|
27
|
+
// Facade to StorageManager
|
|
28
|
+
get initted() {
|
|
29
|
+
return this.storage.initted;
|
|
30
|
+
}
|
|
31
|
+
async init() {
|
|
32
|
+
return this.storage.init();
|
|
33
|
+
}
|
|
34
|
+
getTransaction() {
|
|
35
|
+
return this.storage.getTransaction();
|
|
36
|
+
} // Exported temporarily for tests
|
|
37
|
+
async get(storeName, id) {
|
|
38
|
+
return this.storage.get(storeName, id);
|
|
39
|
+
}
|
|
40
|
+
async findAll(storeName) {
|
|
41
|
+
return this.storage.findAll(storeName);
|
|
42
|
+
}
|
|
43
|
+
async add(storeName, value, key) {
|
|
44
|
+
await this.storage.executeTransaction([{ type: "add", storeName, value, key }]);
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
async put(storeName, value, key) {
|
|
48
|
+
await this.storage.executeTransaction([{ type: "put", storeName, value, key }]);
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
async delete(storeName, key) {
|
|
52
|
+
await this.storage.executeTransaction([{ type: "delete", storeName, key }]);
|
|
53
|
+
}
|
|
54
|
+
async clear(storeName) {
|
|
55
|
+
await this.storage.executeTransaction([{ type: "clear", storeName }]);
|
|
56
|
+
}
|
|
57
|
+
// Facade to EventBus
|
|
58
|
+
subscribe(callback) {
|
|
59
|
+
return this.events.subscribe(callback);
|
|
60
|
+
}
|
|
61
|
+
onSyncStateChange(callback) {
|
|
62
|
+
return this.events.onSyncStateChange(callback);
|
|
63
|
+
}
|
|
64
|
+
// Facade to AuthManager
|
|
65
|
+
get isAuth() {
|
|
66
|
+
return this.authManager.isAuth;
|
|
67
|
+
}
|
|
68
|
+
async checkAuth() {
|
|
69
|
+
return this.authManager.checkAuth();
|
|
70
|
+
}
|
|
71
|
+
async login(email, code) {
|
|
72
|
+
return this.authManager.login(email, code);
|
|
73
|
+
}
|
|
74
|
+
async logout() {
|
|
75
|
+
return this.authManager.logout();
|
|
76
|
+
}
|
|
77
|
+
async reset() {
|
|
78
|
+
return this.authManager.reset();
|
|
79
|
+
}
|
|
80
|
+
// Facade to SyncEngine
|
|
81
|
+
get isEnabled() {
|
|
82
|
+
return this.syncEngine.isEnabled;
|
|
83
|
+
}
|
|
84
|
+
get isLive() {
|
|
85
|
+
return this.syncEngine.isLive;
|
|
86
|
+
}
|
|
87
|
+
enable() {
|
|
88
|
+
this.syncEngine.enable();
|
|
89
|
+
}
|
|
90
|
+
disable() {
|
|
91
|
+
this.syncEngine.disable();
|
|
92
|
+
}
|
|
93
|
+
async waitForLive() {
|
|
94
|
+
return this.syncEngine.waitForLive();
|
|
95
|
+
}
|
|
96
|
+
async triggerSync() {
|
|
97
|
+
return this.syncEngine.triggerSync();
|
|
98
|
+
}
|
|
99
|
+
// Legacy aliases
|
|
100
|
+
startSyncInterval() {
|
|
101
|
+
this.enable();
|
|
102
|
+
}
|
|
103
|
+
stopSyncInterval() {
|
|
104
|
+
this.disable();
|
|
105
|
+
}
|
|
106
|
+
onSchemaChangeDetected() {
|
|
107
|
+
this.storage.dispose();
|
|
108
|
+
this.disable();
|
|
109
|
+
if (typeof window !== "undefined") {
|
|
110
|
+
window.location.reload();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
dispose() {
|
|
114
|
+
this.disable();
|
|
115
|
+
this.events.dispose();
|
|
116
|
+
this.storage.dispose();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=DbSync.js.map
|