@slimr/dbsync 0.0.3 → 0.0.6
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 +9 -7
- package/cjs/DbSync.d.ts +47 -3
- package/cjs/DbSync.js +54 -18
- package/cjs/DbSync.js.map +1 -1
- package/cjs/adapters/LocalAdapter.d.ts +18 -0
- package/cjs/adapters/LocalAdapter.js +28 -0
- package/cjs/adapters/LocalAdapter.js.map +1 -0
- package/cjs/adapters/RestAdapter.d.ts +17 -0
- package/cjs/adapters/RestAdapter.js +13 -0
- package/cjs/adapters/RestAdapter.js.map +1 -1
- package/cjs/adapters/index.d.ts +1 -0
- package/cjs/adapters/index.js +1 -0
- package/cjs/adapters/index.js.map +1 -1
- package/cjs/adapters/types.d.ts +13 -0
- package/cjs/internal/AuthManager.d.ts +17 -2
- package/cjs/internal/AuthManager.js +19 -4
- package/cjs/internal/AuthManager.js.map +1 -1
- package/cjs/internal/EventBus.d.ts +12 -0
- package/cjs/internal/EventBus.js +12 -0
- package/cjs/internal/EventBus.js.map +1 -1
- package/cjs/internal/StorageManager.d.ts +21 -0
- package/cjs/internal/StorageManager.js +21 -0
- package/cjs/internal/StorageManager.js.map +1 -1
- package/cjs/internal/SyncEngine.d.ts +29 -3
- package/cjs/internal/SyncEngine.js +31 -5
- package/cjs/internal/SyncEngine.js.map +1 -1
- package/cjs/useDbQuery.js +2 -0
- package/cjs/useDbQuery.js.map +1 -1
- package/cjs/util/promiseWithResolvers.js +3 -0
- package/cjs/util/promiseWithResolvers.js.map +1 -1
- package/docs/Adapters.md +58 -0
- package/docs/LocalAdapter.md +7 -0
- package/docs/RestAdapter.md +69 -0
- package/esm/DbSync.d.ts +47 -3
- package/esm/DbSync.js +54 -18
- package/esm/DbSync.js.map +1 -1
- package/esm/adapters/LocalAdapter.d.ts +18 -0
- package/esm/adapters/LocalAdapter.js +24 -0
- package/esm/adapters/LocalAdapter.js.map +1 -0
- package/esm/adapters/RestAdapter.d.ts +17 -0
- package/esm/adapters/RestAdapter.js +13 -0
- package/esm/adapters/RestAdapter.js.map +1 -1
- package/esm/adapters/index.d.ts +1 -0
- package/esm/adapters/index.js +1 -0
- package/esm/adapters/index.js.map +1 -1
- package/esm/adapters/types.d.ts +13 -0
- package/esm/internal/AuthManager.d.ts +17 -2
- package/esm/internal/AuthManager.js +19 -4
- package/esm/internal/AuthManager.js.map +1 -1
- package/esm/internal/EventBus.d.ts +12 -0
- package/esm/internal/EventBus.js +12 -0
- package/esm/internal/EventBus.js.map +1 -1
- package/esm/internal/StorageManager.d.ts +21 -0
- package/esm/internal/StorageManager.js +21 -0
- package/esm/internal/StorageManager.js.map +1 -1
- package/esm/internal/SyncEngine.d.ts +29 -3
- package/esm/internal/SyncEngine.js +31 -5
- package/esm/internal/SyncEngine.js.map +1 -1
- package/esm/useDbQuery.js +2 -0
- package/esm/useDbQuery.js.map +1 -1
- package/esm/util/promiseWithResolvers.js +3 -0
- package/esm/util/promiseWithResolvers.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -23,13 +23,13 @@ npm install @slimr/dbsync
|
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Quick Start
|
|
26
|
-
You instantiate `dbsync` by supplying your backend [adapter](./
|
|
26
|
+
You instantiate `dbsync` by supplying your backend [adapter](./docs/Adapters.md) and your table/index definitions. Afterwards, you must call `.init()` to safely generate the database.
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
29
|
import { DbSync } from '@slimr/dbsync';
|
|
30
|
-
import { RestAdapter } from '@slimr/dbsync/adapters';
|
|
30
|
+
import { LocalAdapter, RestAdapter } from '@slimr/dbsync/adapters';
|
|
31
31
|
|
|
32
|
-
const dbAdapter = new RestAdapter({ url: 'https://api.myapp.com' });
|
|
32
|
+
const dbAdapter = process.env.TEST ? new LocalAdapter() : new RestAdapter({ url: 'https://api.myapp.com' });
|
|
33
33
|
|
|
34
34
|
const db = new DbSync({
|
|
35
35
|
adapter: dbAdapter,
|
|
@@ -39,10 +39,12 @@ const db = new DbSync({
|
|
|
39
39
|
}
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
//
|
|
43
|
-
await db.
|
|
42
|
+
// Init the db and boot up the background sync engine!
|
|
43
|
+
await db.start();
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
> **Local-Only No-Sync Database:** If you want all the IndexedDB ORM and reactive features, but completely lack a remote database to sync to, simply import `LocalAdapter` from `@slimr/dbsync/adapters` and pass that into the constructor instead. It acts as an empty black box so your operations succeed cleanly entirely on the client.
|
|
47
|
+
|
|
46
48
|
## Schema Versioning
|
|
47
49
|
|
|
48
50
|
`@slimr/dbsync` automatically manages your IndexedDB database versioning to prevent schema mismatches and corruption across tabs and devices.
|
|
@@ -162,10 +164,10 @@ To boot up the network synchronizer to clear those queues and fetch server updat
|
|
|
162
164
|
|
|
163
165
|
```typescript
|
|
164
166
|
// Start polling the swift-crud backend every 5 seconds
|
|
165
|
-
db.
|
|
167
|
+
await db.start();
|
|
166
168
|
|
|
167
169
|
// Stop polling
|
|
168
|
-
db.
|
|
170
|
+
await db.stop();
|
|
169
171
|
```
|
|
170
172
|
|
|
171
173
|
Only one tab on a user's device will act as the "Leader" and poll the network at any time using **Web Locks**. All other tabs remain completely idle and rely on `BroadcastChannel` messages from the Leader tab to reactively update their UI when fresh data drops.
|
package/cjs/DbSync.d.ts
CHANGED
|
@@ -2,50 +2,94 @@ import type { BackendAdapter } from "./adapters/types.js";
|
|
|
2
2
|
import { type SyncState } from "./internal/EventBus.js";
|
|
3
3
|
import { StorageManager } from "./internal/StorageManager.js";
|
|
4
4
|
export interface DbSyncConfig {
|
|
5
|
+
/** The backend adapter used for authentication and synchronization. */
|
|
5
6
|
adapter: BackendAdapter;
|
|
7
|
+
/** Optional fixed IndexedDB version. */
|
|
6
8
|
version?: number;
|
|
9
|
+
/** The object stores and their index definitions. */
|
|
7
10
|
tables: Record<string, {
|
|
8
11
|
indexes?: string[];
|
|
9
12
|
}>;
|
|
10
13
|
}
|
|
11
14
|
export type { SyncState };
|
|
15
|
+
/**
|
|
16
|
+
* The public facade for the dbsync engine.
|
|
17
|
+
*/
|
|
12
18
|
export declare class DbSync {
|
|
19
|
+
/** The polling interval used by the sync engine in milliseconds. */
|
|
13
20
|
syncInterval: number;
|
|
21
|
+
/** The active configuration passed by the consumer. */
|
|
14
22
|
config: DbSyncConfig;
|
|
23
|
+
/** The event bus used for local and cross-tab notifications. */
|
|
15
24
|
private events;
|
|
25
|
+
/** The storage manager handling IndexedDB state and transactions. */
|
|
16
26
|
storage: StorageManager;
|
|
27
|
+
/** The engine responsible for network synchronization. */
|
|
17
28
|
private syncEngine;
|
|
29
|
+
/** The manager handling authentication and logout/reset behavior. */
|
|
18
30
|
private authManager;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new `DbSync` instance using the supplied configuration.
|
|
33
|
+
*
|
|
34
|
+
* @param config The backend adapter, schema version, and table definitions.
|
|
35
|
+
*/
|
|
19
36
|
constructor(config: DbSyncConfig);
|
|
37
|
+
/** Returns a fresh RFC-4122 UUID. */
|
|
20
38
|
genUuid(): `${string}-${string}-${string}-${string}-${string}`;
|
|
39
|
+
/** Whether the storage layer has finished initializing. */
|
|
21
40
|
get initted(): boolean;
|
|
41
|
+
/** Initializes the underlying IndexedDB stores. */
|
|
22
42
|
init(): Promise<void>;
|
|
43
|
+
/** Returns a queued transaction object for batched writes. */
|
|
23
44
|
getTransaction(): import("./internal/DbTransaction.js").DbTransaction;
|
|
45
|
+
/** Reads a typed record by primary key. */
|
|
24
46
|
get<T>(storeName: string, id: string | number): Promise<T | undefined>;
|
|
47
|
+
/** Reads all records from the given object store. */
|
|
25
48
|
findAll<T>(storeName: string): Promise<T[]>;
|
|
49
|
+
/** Inserts a new record into the given object store. */
|
|
26
50
|
add<T>(storeName: string, value: any, key?: string | number): Promise<T>;
|
|
51
|
+
/** Upserts a record into the given object store. */
|
|
27
52
|
put<T>(storeName: string, value: any, key?: string | number): Promise<T>;
|
|
53
|
+
/** Deletes a record from the given object store. */
|
|
28
54
|
delete(storeName: string, key: string | number): Promise<void>;
|
|
55
|
+
/** Clears all records from the given object store. */
|
|
29
56
|
clear(storeName: string): Promise<void>;
|
|
57
|
+
/** Subscribes to store update notifications. */
|
|
30
58
|
subscribe(callback: (stores: string[]) => void): {
|
|
31
59
|
close: () => boolean;
|
|
32
60
|
};
|
|
61
|
+
/** Subscribes to sync state changes. */
|
|
33
62
|
onSyncStateChange(callback: (state: SyncState) => void): {
|
|
34
63
|
close: () => boolean;
|
|
35
64
|
};
|
|
65
|
+
/** Whether the current backend session is authenticated. */
|
|
36
66
|
get isAuth(): boolean;
|
|
67
|
+
/** Verifies authentication against the backend adapter. */
|
|
37
68
|
checkAuth(): Promise<boolean>;
|
|
69
|
+
/** Logs in with the given credentials. */
|
|
38
70
|
login(email: string, code: string): Promise<void>;
|
|
71
|
+
/** Logs out and clears auth state. */
|
|
39
72
|
logout(): Promise<void>;
|
|
73
|
+
/** Logs out and clears the local database. */
|
|
40
74
|
reset(): Promise<void>;
|
|
41
|
-
|
|
75
|
+
/** Whether background sync is currently enabled. */
|
|
76
|
+
get isStarted(): boolean;
|
|
77
|
+
/** Whether the sync engine has recently connected successfully. */
|
|
42
78
|
get isLive(): boolean;
|
|
43
|
-
|
|
44
|
-
|
|
79
|
+
/** Starts periodic background synchronization. */
|
|
80
|
+
start(): Promise<void>;
|
|
81
|
+
/** Stops periodic background synchronization. */
|
|
82
|
+
stop(): Promise<void>;
|
|
83
|
+
/** Waits until the sync engine reports a live connection. */
|
|
45
84
|
waitForLive(): Promise<void>;
|
|
85
|
+
/** Triggers a single sync cycle immediately. */
|
|
46
86
|
triggerSync(): Promise<void>;
|
|
87
|
+
/** Legacy alias for `enable()`. */
|
|
47
88
|
startSyncInterval(): void;
|
|
89
|
+
/** Legacy alias for `disable()`. */
|
|
48
90
|
stopSyncInterval(): void;
|
|
91
|
+
/** Responds to schema changes by disposing state and reloading the page. */
|
|
49
92
|
protected onSchemaChangeDetected(): void;
|
|
93
|
+
/** Disposes all background resources and event listeners. */
|
|
50
94
|
dispose(): void;
|
|
51
95
|
}
|
package/cjs/DbSync.js
CHANGED
|
@@ -6,115 +6,151 @@ const EventBus_js_1 = require("./internal/EventBus.js");
|
|
|
6
6
|
const StorageManager_js_1 = require("./internal/StorageManager.js");
|
|
7
7
|
const SyncEngine_js_1 = require("./internal/SyncEngine.js");
|
|
8
8
|
const utils_js_1 = require("./internal/utils.js");
|
|
9
|
+
/**
|
|
10
|
+
* The public facade for the dbsync engine.
|
|
11
|
+
*/
|
|
9
12
|
class DbSync {
|
|
13
|
+
/** The polling interval used by the sync engine in milliseconds. */
|
|
10
14
|
syncInterval = 5000;
|
|
15
|
+
/** The active configuration passed by the consumer. */
|
|
11
16
|
config;
|
|
12
|
-
|
|
17
|
+
/** The event bus used for local and cross-tab notifications. */
|
|
13
18
|
events;
|
|
19
|
+
/** The storage manager handling IndexedDB state and transactions. */
|
|
14
20
|
storage; // Public for tests currently (we can wrap it later)
|
|
21
|
+
/** The engine responsible for network synchronization. */
|
|
15
22
|
syncEngine;
|
|
23
|
+
/** The manager handling authentication and logout/reset behavior. */
|
|
16
24
|
authManager;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new `DbSync` instance using the supplied configuration.
|
|
27
|
+
*
|
|
28
|
+
* @param config The backend adapter, schema version, and table definitions.
|
|
29
|
+
*/
|
|
17
30
|
constructor(config) {
|
|
18
31
|
this.config = config;
|
|
19
32
|
this.events = new EventBus_js_1.EventBus();
|
|
20
33
|
const adapter = config.adapter;
|
|
21
34
|
const onSchemaChange = () => this.onSchemaChangeDetected();
|
|
22
35
|
this.storage = new StorageManager_js_1.StorageManager(config, this.events, onSchemaChange);
|
|
23
|
-
this.authManager = new AuthManager_js_1.AuthManager(adapter, this.storage, () => this.
|
|
36
|
+
this.authManager = new AuthManager_js_1.AuthManager(adapter, this.storage, () => { void this.stop(); });
|
|
24
37
|
this.syncEngine = new SyncEngine_js_1.SyncEngine(config, this.syncInterval, this.events, this.storage, this.authManager, adapter, onSchemaChange);
|
|
25
38
|
}
|
|
26
|
-
|
|
39
|
+
/** Returns a fresh RFC-4122 UUID. */
|
|
27
40
|
genUuid() {
|
|
28
41
|
return (0, utils_js_1.genUuid)();
|
|
29
42
|
}
|
|
30
|
-
|
|
43
|
+
/** Whether the storage layer has finished initializing. */
|
|
31
44
|
get initted() {
|
|
32
45
|
return this.storage.initted;
|
|
33
46
|
}
|
|
47
|
+
/** Initializes the underlying IndexedDB stores. */
|
|
34
48
|
async init() {
|
|
35
49
|
return this.storage.init();
|
|
36
50
|
}
|
|
51
|
+
/** Returns a queued transaction object for batched writes. */
|
|
37
52
|
getTransaction() {
|
|
38
53
|
return this.storage.getTransaction();
|
|
39
54
|
} // Exported temporarily for tests
|
|
55
|
+
/** Reads a typed record by primary key. */
|
|
40
56
|
async get(storeName, id) {
|
|
41
57
|
return this.storage.get(storeName, id);
|
|
42
58
|
}
|
|
59
|
+
/** Reads all records from the given object store. */
|
|
43
60
|
async findAll(storeName) {
|
|
44
61
|
return this.storage.findAll(storeName);
|
|
45
62
|
}
|
|
63
|
+
/** Inserts a new record into the given object store. */
|
|
46
64
|
async add(storeName, value, key) {
|
|
47
65
|
await this.storage.executeTransaction([{ type: "add", storeName, value, key }]);
|
|
48
66
|
return value;
|
|
49
67
|
}
|
|
68
|
+
/** Upserts a record into the given object store. */
|
|
50
69
|
async put(storeName, value, key) {
|
|
51
70
|
await this.storage.executeTransaction([{ type: "put", storeName, value, key }]);
|
|
52
71
|
return value;
|
|
53
72
|
}
|
|
73
|
+
/** Deletes a record from the given object store. */
|
|
54
74
|
async delete(storeName, key) {
|
|
55
75
|
await this.storage.executeTransaction([{ type: "delete", storeName, key }]);
|
|
56
76
|
}
|
|
77
|
+
/** Clears all records from the given object store. */
|
|
57
78
|
async clear(storeName) {
|
|
58
79
|
await this.storage.executeTransaction([{ type: "clear", storeName }]);
|
|
59
80
|
}
|
|
60
|
-
|
|
81
|
+
/** Subscribes to store update notifications. */
|
|
61
82
|
subscribe(callback) {
|
|
62
83
|
return this.events.subscribe(callback);
|
|
63
84
|
}
|
|
85
|
+
/** Subscribes to sync state changes. */
|
|
64
86
|
onSyncStateChange(callback) {
|
|
65
87
|
return this.events.onSyncStateChange(callback);
|
|
66
88
|
}
|
|
67
|
-
|
|
89
|
+
/** Whether the current backend session is authenticated. */
|
|
68
90
|
get isAuth() {
|
|
69
91
|
return this.authManager.isAuth;
|
|
70
92
|
}
|
|
93
|
+
/** Verifies authentication against the backend adapter. */
|
|
71
94
|
async checkAuth() {
|
|
72
95
|
return this.authManager.checkAuth();
|
|
73
96
|
}
|
|
97
|
+
/** Logs in with the given credentials. */
|
|
74
98
|
async login(email, code) {
|
|
75
99
|
return this.authManager.login(email, code);
|
|
76
100
|
}
|
|
101
|
+
/** Logs out and clears auth state. */
|
|
77
102
|
async logout() {
|
|
78
103
|
return this.authManager.logout();
|
|
79
104
|
}
|
|
105
|
+
/** Logs out and clears the local database. */
|
|
80
106
|
async reset() {
|
|
81
107
|
return this.authManager.reset();
|
|
82
108
|
}
|
|
83
|
-
|
|
84
|
-
get
|
|
85
|
-
return this.syncEngine.
|
|
109
|
+
/** Whether background sync is currently enabled. */
|
|
110
|
+
get isStarted() {
|
|
111
|
+
return this.syncEngine.isStarted;
|
|
86
112
|
}
|
|
113
|
+
/** Whether the sync engine has recently connected successfully. */
|
|
87
114
|
get isLive() {
|
|
88
115
|
return this.syncEngine.isLive;
|
|
89
116
|
}
|
|
90
|
-
|
|
91
|
-
|
|
117
|
+
/** Starts periodic background synchronization. */
|
|
118
|
+
async start() {
|
|
119
|
+
if (!this.initted)
|
|
120
|
+
await this.init();
|
|
121
|
+
this.syncEngine.start();
|
|
92
122
|
}
|
|
93
|
-
|
|
94
|
-
|
|
123
|
+
/** Stops periodic background synchronization. */
|
|
124
|
+
async stop() {
|
|
125
|
+
this.syncEngine.stop();
|
|
95
126
|
}
|
|
127
|
+
/** Waits until the sync engine reports a live connection. */
|
|
96
128
|
async waitForLive() {
|
|
97
129
|
return this.syncEngine.waitForLive();
|
|
98
130
|
}
|
|
131
|
+
/** Triggers a single sync cycle immediately. */
|
|
99
132
|
async triggerSync() {
|
|
100
133
|
return this.syncEngine.triggerSync();
|
|
101
134
|
}
|
|
102
|
-
|
|
135
|
+
/** Legacy alias for `enable()`. */
|
|
103
136
|
startSyncInterval() {
|
|
104
|
-
this.
|
|
137
|
+
this.start();
|
|
105
138
|
}
|
|
139
|
+
/** Legacy alias for `disable()`. */
|
|
106
140
|
stopSyncInterval() {
|
|
107
|
-
this.
|
|
141
|
+
this.stop();
|
|
108
142
|
}
|
|
143
|
+
/** Responds to schema changes by disposing state and reloading the page. */
|
|
109
144
|
onSchemaChangeDetected() {
|
|
110
145
|
this.storage.dispose();
|
|
111
|
-
this.
|
|
146
|
+
this.stop();
|
|
112
147
|
if (typeof window !== "undefined") {
|
|
113
148
|
window.location.reload();
|
|
114
149
|
}
|
|
115
150
|
}
|
|
151
|
+
/** Disposes all background resources and event listeners. */
|
|
116
152
|
dispose() {
|
|
117
|
-
this.
|
|
153
|
+
this.stop();
|
|
118
154
|
this.events.dispose();
|
|
119
155
|
this.storage.dispose();
|
|
120
156
|
}
|
package/cjs/DbSync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DbSync.js","sourceRoot":"","sources":["../src/DbSync.ts"],"names":[],"mappings":";;;AACA,8DAAuD;AACvD,wDAAiE;AACjE,oEAA6D;AAC7D,4DAAqD;AACrD,kDAA6C;
|
|
1
|
+
{"version":3,"file":"DbSync.js","sourceRoot":"","sources":["../src/DbSync.ts"],"names":[],"mappings":";;;AACA,8DAAuD;AACvD,wDAAiE;AACjE,oEAA6D;AAC7D,4DAAqD;AACrD,kDAA6C;AAc7C;;GAEG;AACH,MAAa,MAAM;IAClB,oEAAoE;IAC7D,YAAY,GAAG,IAAI,CAAA;IAC1B,uDAAuD;IAChD,MAAM,CAAc;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,CAAC;IACD,6DAA6D;IACtD,KAAK,CAAC,WAAW;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE,CAAA;IACrC,CAAC;IACD,gDAAgD;IACzC,KAAK,CAAC,WAAW;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE,CAAA;IACrC,CAAC;IAED,mCAAmC;IAC5B,iBAAiB;QACvB,IAAI,CAAC,KAAK,EAAE,CAAA;IACb,CAAC;IACD,oCAAoC;IAC7B,gBAAgB;QACtB,IAAI,CAAC,IAAI,EAAE,CAAA;IACZ,CAAC;IAED,4EAA4E;IAClE,sBAAsB;QAC/B,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAA;QACtB,IAAI,CAAC,IAAI,EAAE,CAAA;QACX,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YACnC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAA;QACzB,CAAC;IACF,CAAC;IAED,6DAA6D;IACtD,OAAO;QACb,IAAI,CAAC,IAAI,EAAE,CAAA;QACX,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAA;QACrB,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACD;AAzKD,wBAyKC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { BackendAdapter, SyncPullResult } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A no-op network adapter for pure local-only usage.
|
|
4
|
+
* Satisfies the DbSync adapter requirement and silently discards sync operations
|
|
5
|
+
* so your local `dirtyQueue` doesn't grow infinitely if synchronization is enabled.
|
|
6
|
+
*/
|
|
7
|
+
export declare class LocalAdapter implements BackendAdapter {
|
|
8
|
+
/** Always returns true so session checks pass locally. */
|
|
9
|
+
checkAuth(): Promise<boolean>;
|
|
10
|
+
/** Always resolves true to mimic successful mock logins. */
|
|
11
|
+
login(): Promise<boolean>;
|
|
12
|
+
/** Does nothing natively. */
|
|
13
|
+
logout(): Promise<void>;
|
|
14
|
+
/** Returns an empty payload because there is no remote source of truth. */
|
|
15
|
+
pull(): Promise<SyncPullResult>;
|
|
16
|
+
/** Silently drops write payloads into the void. */
|
|
17
|
+
push(): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalAdapter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A no-op network adapter for pure local-only usage.
|
|
6
|
+
* Satisfies the DbSync adapter requirement and silently discards sync operations
|
|
7
|
+
* so your local `dirtyQueue` doesn't grow infinitely if synchronization is enabled.
|
|
8
|
+
*/
|
|
9
|
+
class LocalAdapter {
|
|
10
|
+
/** Always returns true so session checks pass locally. */
|
|
11
|
+
async checkAuth() {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
/** Always resolves true to mimic successful mock logins. */
|
|
15
|
+
async login() {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
/** Does nothing natively. */
|
|
19
|
+
async logout() { }
|
|
20
|
+
/** Returns an empty payload because there is no remote source of truth. */
|
|
21
|
+
async pull() {
|
|
22
|
+
return { items: [], hasMore: false };
|
|
23
|
+
}
|
|
24
|
+
/** Silently drops write payloads into the void. */
|
|
25
|
+
async push() { }
|
|
26
|
+
}
|
|
27
|
+
exports.LocalAdapter = LocalAdapter;
|
|
28
|
+
//# sourceMappingURL=LocalAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalAdapter.js","sourceRoot":"","sources":["../../src/adapters/LocalAdapter.ts"],"names":[],"mappings":";;;AAEA;;;;GAIG;AACH,MAAa,YAAY;IACxB,0DAA0D;IAC1D,KAAK,CAAC,SAAS;QACd,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,4DAA4D;IAC5D,KAAK,CAAC,KAAK;QACV,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,6BAA6B;IAC7B,KAAK,CAAC,MAAM,KAAmB,CAAC;IAEhC,2EAA2E;IAC3E,KAAK,CAAC,IAAI;QACT,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;IACrC,CAAC;IAED,mDAAmD;IACnD,KAAK,CAAC,IAAI,KAAmB,CAAC;CAC9B;AArBD,oCAqBC"}
|
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
import type { BackendAdapter, SyncPullResult } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for the built-in REST adapter.
|
|
4
|
+
*/
|
|
2
5
|
export interface RestAdapterConfig {
|
|
6
|
+
/** The base URL of the remote API. */
|
|
3
7
|
url: string;
|
|
4
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* A REST-backed implementation of the backend adapter contract.
|
|
11
|
+
*/
|
|
5
12
|
export declare class RestAdapter implements BackendAdapter {
|
|
6
13
|
private config;
|
|
14
|
+
/**
|
|
15
|
+
* Stores the adapter configuration, including the API base URL.
|
|
16
|
+
*
|
|
17
|
+
* @param config The REST adapter configuration.
|
|
18
|
+
*/
|
|
7
19
|
constructor(config: RestAdapterConfig);
|
|
20
|
+
/** Checks whether the server session cookie is currently authenticated. */
|
|
8
21
|
checkAuth(): Promise<boolean>;
|
|
22
|
+
/** Logs the user in using an email/code pair. */
|
|
9
23
|
login(email: string, code: string): Promise<boolean>;
|
|
24
|
+
/** Logs the current user out of the remote session. */
|
|
10
25
|
logout(): Promise<void>;
|
|
26
|
+
/** Pulls remote records from the REST backend using the provided cursor. */
|
|
11
27
|
pull(cursor: string): Promise<SyncPullResult>;
|
|
28
|
+
/** Pushes queued local mutations to the REST backend. */
|
|
12
29
|
push(payload: any[]): Promise<void>;
|
|
13
30
|
}
|
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RestAdapter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A REST-backed implementation of the backend adapter contract.
|
|
6
|
+
*/
|
|
4
7
|
class RestAdapter {
|
|
5
8
|
config;
|
|
9
|
+
/**
|
|
10
|
+
* Stores the adapter configuration, including the API base URL.
|
|
11
|
+
*
|
|
12
|
+
* @param config The REST adapter configuration.
|
|
13
|
+
*/
|
|
6
14
|
constructor(config) {
|
|
7
15
|
this.config = config;
|
|
8
16
|
}
|
|
17
|
+
/** Checks whether the server session cookie is currently authenticated. */
|
|
9
18
|
async checkAuth() {
|
|
10
19
|
const res = await fetch(`${this.config.url}/api/session`, { credentials: "include" });
|
|
11
20
|
return res.ok;
|
|
12
21
|
}
|
|
22
|
+
/** Logs the user in using an email/code pair. */
|
|
13
23
|
async login(email, code) {
|
|
14
24
|
const res = await fetch(`${this.config.url}/api/session/login`, {
|
|
15
25
|
method: "POST",
|
|
@@ -21,9 +31,11 @@ class RestAdapter {
|
|
|
21
31
|
throw new Error("Login failed");
|
|
22
32
|
return true;
|
|
23
33
|
}
|
|
34
|
+
/** Logs the current user out of the remote session. */
|
|
24
35
|
async logout() {
|
|
25
36
|
await fetch(`${this.config.url}/api/session/logout`, { method: "POST", credentials: "include" });
|
|
26
37
|
}
|
|
38
|
+
/** Pulls remote records from the REST backend using the provided cursor. */
|
|
27
39
|
async pull(cursor) {
|
|
28
40
|
const res = await fetch(`${this.config.url}/api/posts?after=${cursor}&limit=40`, {
|
|
29
41
|
credentials: "include",
|
|
@@ -32,6 +44,7 @@ class RestAdapter {
|
|
|
32
44
|
throw { status: res.status };
|
|
33
45
|
return await res.json();
|
|
34
46
|
}
|
|
47
|
+
/** Pushes queued local mutations to the REST backend. */
|
|
35
48
|
async push(payload) {
|
|
36
49
|
const res = await fetch(`${this.config.url}/api/posts/upsert-many`, {
|
|
37
50
|
method: "POST",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RestAdapter.js","sourceRoot":"","sources":["../../src/adapters/RestAdapter.ts"],"names":[],"mappings":";;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WAAW,EAAE;YAChF,WAAW,EAAE,SAAS;SACtB,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAA;QACzC,OAAO,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;IACxB,CAAC;IAED,yDAAyD;IAClD,KAAK,CAAC,IAAI,CAAC,OAAc;QAC/B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,wBAAwB,EAAE;YACnE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,WAAW,EAAE,SAAS;YACtB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC7B,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAA;IAC1C,CAAC;CACD;AAlDD,kCAkDC"}
|
package/cjs/adapters/index.d.ts
CHANGED
package/cjs/adapters/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./LocalAdapter.js"), exports);
|
|
17
18
|
__exportStar(require("./RestAdapter.js"), exports);
|
|
18
19
|
__exportStar(require("./types.js"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAgC;AAChC,6CAA0B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAiC;AACjC,mDAAgC;AAChC,6CAA0B"}
|
package/cjs/adapters/types.d.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The standard response shape returned by a backend pull request.
|
|
3
|
+
*/
|
|
1
4
|
export interface SyncPullResult {
|
|
5
|
+
/** The records fetched from the remote backend. */
|
|
2
6
|
items: any[];
|
|
7
|
+
/** Whether more results remain to be fetched for the current cursor. */
|
|
3
8
|
hasMore: boolean;
|
|
4
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The contract implemented by all sync backends.
|
|
12
|
+
*/
|
|
5
13
|
export interface BackendAdapter {
|
|
14
|
+
/** Checks whether the current session is authenticated. */
|
|
6
15
|
checkAuth(): Promise<boolean>;
|
|
16
|
+
/** Logs the current user in against the backend. */
|
|
7
17
|
login(email: string, code: string): Promise<boolean>;
|
|
18
|
+
/** Logs the current user out of the backend. */
|
|
8
19
|
logout(): Promise<void>;
|
|
20
|
+
/** Pulls remote changes since the provided cursor. */
|
|
9
21
|
pull(cursor: string): Promise<SyncPullResult>;
|
|
22
|
+
/** Pushes queued local mutations to the backend. */
|
|
10
23
|
push(payload: any[]): Promise<void>;
|
|
11
24
|
}
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import type { BackendAdapter } from "../adapters/types.js";
|
|
2
2
|
import type { StorageManager } from "./StorageManager.js";
|
|
3
|
+
/**
|
|
4
|
+
* Coordinates authentication state and logout/reset behavior for `DbSync`.
|
|
5
|
+
*/
|
|
3
6
|
export declare class AuthManager {
|
|
4
7
|
private adapter;
|
|
5
8
|
private storage;
|
|
6
|
-
private
|
|
9
|
+
private stopSync;
|
|
10
|
+
/** Tracks whether the current backend session is authenticated. */
|
|
7
11
|
isAuth: boolean;
|
|
8
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new auth manager for the provided backend adapter and storage layer.
|
|
14
|
+
*
|
|
15
|
+
* @param adapter The backend adapter used for auth requests.
|
|
16
|
+
* @param storage The storage manager used for reset operations.
|
|
17
|
+
* @param stopSync The callback used to stop background sync.
|
|
18
|
+
*/
|
|
19
|
+
constructor(adapter: BackendAdapter, storage: StorageManager, stopSync: () => Promise<void> | void);
|
|
20
|
+
/** Checks authentication against the backend adapter and stores the result. */
|
|
9
21
|
checkAuth(): Promise<boolean>;
|
|
22
|
+
/** Logs in through the backend adapter and updates auth state. */
|
|
10
23
|
login(email: string, code: string): Promise<void>;
|
|
24
|
+
/** Logs out, stops sync, and clears auth state. */
|
|
11
25
|
logout(): Promise<void>;
|
|
26
|
+
/** Logs out and clears all local IndexedDB stores and auth markers. */
|
|
12
27
|
reset(): Promise<void>;
|
|
13
28
|
}
|
|
@@ -1,28 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthManager = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Coordinates authentication state and logout/reset behavior for `DbSync`.
|
|
6
|
+
*/
|
|
4
7
|
class AuthManager {
|
|
5
8
|
adapter;
|
|
6
9
|
storage;
|
|
7
|
-
|
|
10
|
+
stopSync;
|
|
11
|
+
/** Tracks whether the current backend session is authenticated. */
|
|
8
12
|
isAuth = false;
|
|
9
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new auth manager for the provided backend adapter and storage layer.
|
|
15
|
+
*
|
|
16
|
+
* @param adapter The backend adapter used for auth requests.
|
|
17
|
+
* @param storage The storage manager used for reset operations.
|
|
18
|
+
* @param stopSync The callback used to stop background sync.
|
|
19
|
+
*/
|
|
20
|
+
constructor(adapter, storage, stopSync) {
|
|
10
21
|
this.adapter = adapter;
|
|
11
22
|
this.storage = storage;
|
|
12
|
-
this.
|
|
23
|
+
this.stopSync = stopSync;
|
|
13
24
|
}
|
|
25
|
+
/** Checks authentication against the backend adapter and stores the result. */
|
|
14
26
|
async checkAuth() {
|
|
15
27
|
this.isAuth = await this.adapter.checkAuth();
|
|
16
28
|
return this.isAuth;
|
|
17
29
|
}
|
|
30
|
+
/** Logs in through the backend adapter and updates auth state. */
|
|
18
31
|
async login(email, code) {
|
|
19
32
|
this.isAuth = await this.adapter.login(email, code);
|
|
20
33
|
}
|
|
34
|
+
/** Logs out, stops sync, and clears auth state. */
|
|
21
35
|
async logout() {
|
|
22
|
-
this.
|
|
36
|
+
await this.stopSync();
|
|
23
37
|
this.isAuth = false;
|
|
24
38
|
await this.adapter.logout();
|
|
25
39
|
}
|
|
40
|
+
/** Logs out and clears all local IndexedDB stores and auth markers. */
|
|
26
41
|
async reset() {
|
|
27
42
|
await this.logout();
|
|
28
43
|
await this.storage.clearAllStores();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthManager.js","sourceRoot":"","sources":["../../src/internal/AuthManager.ts"],"names":[],"mappings":";;;AAGA,MAAa,WAAW;
|
|
1
|
+
{"version":3,"file":"AuthManager.js","sourceRoot":"","sources":["../../src/internal/AuthManager.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH,MAAa,WAAW;IAYd;IACA;IACA;IAbT,mEAAmE;IAC5D,MAAM,GAAG,KAAK,CAAA;IAErB;;;;;;OAMG;IACH,YACS,OAAuB,EACvB,OAAuB,EACvB,QAAoC;QAFpC,YAAO,GAAP,OAAO,CAAgB;QACvB,YAAO,GAAP,OAAO,CAAgB;QACvB,aAAQ,GAAR,QAAQ,CAA4B;IAC1C,CAAC;IAEJ,+EAA+E;IACxE,KAAK,CAAC,SAAS;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;QAC5C,OAAO,IAAI,CAAC,MAAM,CAAA;IACnB,CAAC;IAED,kEAAkE;IAC3D,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,IAAY;QAC7C,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC;IAED,mDAAmD;IAC5C,KAAK,CAAC,MAAM;QAClB,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAA;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAA;IAC5B,CAAC;IAED,uEAAuE;IAChE,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,MAAM,EAAE,CAAA;QACnB,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,CAAA;QACnC,YAAY,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAA;QAC/C,YAAY,CAAC,UAAU,CAAC,uBAAuB,CAAC,CAAA;IACjD,CAAC;CACD;AA1CD,kCA0CC"}
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
export type SyncState = "idle" | "syncing" | "offline" | "error";
|
|
2
|
+
/**
|
|
3
|
+
* Broadcasts store updates and sync state changes to local subscribers.
|
|
4
|
+
*/
|
|
2
5
|
export declare class EventBus {
|
|
6
|
+
/** Subscribers that listen for store updates. */
|
|
3
7
|
private subscribers;
|
|
8
|
+
/** Subscribers that listen for sync state transitions. */
|
|
4
9
|
private stateListeners;
|
|
10
|
+
/** Broadcast channel used to mirror updates across tabs. */
|
|
5
11
|
private bc;
|
|
12
|
+
/** Initializes the broadcast listener for cross-tab update propagation. */
|
|
6
13
|
constructor();
|
|
14
|
+
/** Adds a store-update subscriber and returns a handle for removing it. */
|
|
7
15
|
subscribe(callback: (stores: string[]) => void): {
|
|
8
16
|
close: () => boolean;
|
|
9
17
|
};
|
|
18
|
+
/** Notifies all store subscribers and mirrors the update to other tabs. */
|
|
10
19
|
notifySubscribers(stores: string[]): void;
|
|
20
|
+
/** Adds a sync-state subscriber and returns a handle for removing it. */
|
|
11
21
|
onSyncStateChange(callback: (state: SyncState) => void): {
|
|
12
22
|
close: () => boolean;
|
|
13
23
|
};
|
|
24
|
+
/** Broadcasts a sync-state transition to all subscribers. */
|
|
14
25
|
setState(state: SyncState): void;
|
|
26
|
+
/** Tears down the broadcast channel and stops cross-tab propagation. */
|
|
15
27
|
dispose(): void;
|
|
16
28
|
}
|