@omnistreamai/data-core 0.1.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/index.d.mts +290 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +302 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +18 -0
- package/src/index.ts +5 -0
- package/src/store.test.ts +452 -0
- package/src/store.ts +469 -0
- package/src/sync-manager.test.ts +212 -0
- package/src/sync-manager.ts +80 -0
- package/src/types.ts +230 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.ts +11 -0
- package/vitest.config.ts +10 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Adapter type: local or remote
|
|
4
|
+
*/
|
|
5
|
+
declare enum AdapterType {
|
|
6
|
+
Local = "local",
|
|
7
|
+
Remote = "remote",
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Authentication type
|
|
11
|
+
*/
|
|
12
|
+
declare enum AuthType {
|
|
13
|
+
Token = "token",
|
|
14
|
+
Basic = "basic",
|
|
15
|
+
Bearer = "bearer",
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Token authentication configuration
|
|
19
|
+
*/
|
|
20
|
+
interface TokenAuth {
|
|
21
|
+
access_token: string;
|
|
22
|
+
token_type: string;
|
|
23
|
+
expires_in?: number;
|
|
24
|
+
refresh_token?: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Authentication configuration
|
|
29
|
+
*/
|
|
30
|
+
interface Authentication {
|
|
31
|
+
authType: AuthType;
|
|
32
|
+
token?: TokenAuth;
|
|
33
|
+
username?: string;
|
|
34
|
+
password?: string;
|
|
35
|
+
bearer?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Service configuration (for remote adapters)
|
|
39
|
+
*/
|
|
40
|
+
interface ServiceConfig {
|
|
41
|
+
endpoint: string;
|
|
42
|
+
authentication?: Authentication;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Query options
|
|
46
|
+
*/
|
|
47
|
+
interface QueryOptions<T = Record<string, unknown>> {
|
|
48
|
+
source?: 'local' | 'remote';
|
|
49
|
+
page?: number;
|
|
50
|
+
limit?: number;
|
|
51
|
+
where?: Partial<T>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Paginated result
|
|
55
|
+
*/
|
|
56
|
+
interface PaginatedResult<T> {
|
|
57
|
+
data: T[];
|
|
58
|
+
totalCount: number;
|
|
59
|
+
page: number;
|
|
60
|
+
limit: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Query function and initial data (for React Query, etc.)
|
|
64
|
+
*/
|
|
65
|
+
interface QueryResult<T> {
|
|
66
|
+
queryKey: unknown[];
|
|
67
|
+
queryFn: () => Promise<T>;
|
|
68
|
+
getInitialData: () => Promise<T>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Store configuration
|
|
72
|
+
*/
|
|
73
|
+
interface StoreConfig {
|
|
74
|
+
indexes?: string[];
|
|
75
|
+
idKey?: string;
|
|
76
|
+
maxLocalEntries?: number;
|
|
77
|
+
onChange?: () => void;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Base adapter interface
|
|
81
|
+
*/
|
|
82
|
+
interface BaseAdapter {
|
|
83
|
+
readonly type: AdapterType;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Local adapter interface
|
|
88
|
+
*/
|
|
89
|
+
interface LocalAdapter extends BaseAdapter {
|
|
90
|
+
readonly type: AdapterType.Local;
|
|
91
|
+
/**
|
|
92
|
+
* Add data
|
|
93
|
+
*/
|
|
94
|
+
add<T extends Record<string, unknown>>(storeName: string, data: T, idKey?: string): Promise<T>;
|
|
95
|
+
/**
|
|
96
|
+
* Update data
|
|
97
|
+
*/
|
|
98
|
+
update<T extends Record<string, unknown>>(storeName: string, id: string, data: Partial<T>, idKey?: string): Promise<T>;
|
|
99
|
+
/**
|
|
100
|
+
* Delete data
|
|
101
|
+
*/
|
|
102
|
+
delete(storeName: string, id: string, idKey?: string): Promise<void>;
|
|
103
|
+
/**
|
|
104
|
+
* Get data by ID
|
|
105
|
+
*/
|
|
106
|
+
getData<T extends Record<string, unknown>>(storeName: string, id: string, idKey?: string): Promise<T | null>;
|
|
107
|
+
/**
|
|
108
|
+
* Get list data (with pagination support)
|
|
109
|
+
*/
|
|
110
|
+
getList<T extends Record<string, unknown>>(storeName: string, options?: QueryOptions<T>, idKey?: string): Promise<T[]>;
|
|
111
|
+
/**
|
|
112
|
+
* Clear store
|
|
113
|
+
*/
|
|
114
|
+
clear(storeName: string): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Initialize store
|
|
117
|
+
*/
|
|
118
|
+
initStore(storeName: string, indexes?: string[], idKey?: string): Promise<void>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Remote adapter interface
|
|
122
|
+
*/
|
|
123
|
+
interface RemoteAdapter extends BaseAdapter {
|
|
124
|
+
readonly type: AdapterType.Remote;
|
|
125
|
+
/**
|
|
126
|
+
* 添加数据
|
|
127
|
+
*/
|
|
128
|
+
add<T extends Record<string, unknown>>(storeName: string, data: T, idKey?: string): Promise<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Update data
|
|
131
|
+
*/
|
|
132
|
+
update<T extends Record<string, unknown>>(storeName: string, id: string, data: Partial<T>, idKey?: string): Promise<T>;
|
|
133
|
+
/**
|
|
134
|
+
* Delete data
|
|
135
|
+
*/
|
|
136
|
+
delete(storeName: string, id: string, idKey?: string): Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* Get data by ID
|
|
139
|
+
*/
|
|
140
|
+
getData<T extends Record<string, unknown>>(storeName: string, id: string, idKey?: string): Promise<T | null>;
|
|
141
|
+
/**
|
|
142
|
+
* Get list data (with pagination support)
|
|
143
|
+
*/
|
|
144
|
+
getList<T extends Record<string, unknown>>(storeName: string, options?: QueryOptions<T>, idKey?: string): Promise<PaginatedResult<T>>;
|
|
145
|
+
/**
|
|
146
|
+
* Clear store
|
|
147
|
+
*/
|
|
148
|
+
clear(storeName: string): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Initialize store (create if not exists)
|
|
151
|
+
*/
|
|
152
|
+
initStore(storeName: string, indexes?: string[], idKey?: string): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Set service configuration
|
|
155
|
+
*/
|
|
156
|
+
setService(service: ServiceConfig): void;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Adapter union type
|
|
160
|
+
*/
|
|
161
|
+
type Adapter = LocalAdapter | RemoteAdapter;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/store.d.ts
|
|
164
|
+
/**
|
|
165
|
+
* Store configuration
|
|
166
|
+
*/
|
|
167
|
+
interface StoreOptions<_T extends Record<string, unknown>> {
|
|
168
|
+
localAdapter: LocalAdapter | null;
|
|
169
|
+
remoteAdapter: RemoteAdapter | null;
|
|
170
|
+
idKey: string;
|
|
171
|
+
indexes?: string[];
|
|
172
|
+
/**
|
|
173
|
+
* Maximum number of local storage entries
|
|
174
|
+
* When local storage entries exceed this limit, oldest entries will be automatically deleted
|
|
175
|
+
* Can be used with sortByKey parameter to specify sorting field for deletion order
|
|
176
|
+
* If not set, no limit on local storage entries
|
|
177
|
+
*/
|
|
178
|
+
maxLocalEntries?: number;
|
|
179
|
+
/**
|
|
180
|
+
* Field name for sorting when maxLocalEntries is in effect
|
|
181
|
+
* If this field is specified, entries will be sorted by this field's value before deleting oldest entries
|
|
182
|
+
* If not specified, entries will be deleted in default array order (usually insertion order)
|
|
183
|
+
* Supports number and string type field values
|
|
184
|
+
*/
|
|
185
|
+
sortByKey?: string;
|
|
186
|
+
onChange?: () => void;
|
|
187
|
+
notThrowLocalError: boolean;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Store class
|
|
191
|
+
*/
|
|
192
|
+
declare class Store<T extends Record<string, unknown>> {
|
|
193
|
+
private storeName;
|
|
194
|
+
private localAdapter;
|
|
195
|
+
private remoteAdapter;
|
|
196
|
+
private idKey;
|
|
197
|
+
private indexes?;
|
|
198
|
+
private maxLocalEntries?;
|
|
199
|
+
private sortByKey?;
|
|
200
|
+
private onChange?;
|
|
201
|
+
private notThrowLocalError;
|
|
202
|
+
private initialized;
|
|
203
|
+
constructor(storeName: string, options: StoreOptions<T>);
|
|
204
|
+
/**
|
|
205
|
+
* Initialize Store
|
|
206
|
+
*/
|
|
207
|
+
init(): Promise<void>;
|
|
208
|
+
private ensureInitialized;
|
|
209
|
+
private triggerChange;
|
|
210
|
+
private executeLocal;
|
|
211
|
+
private executeRemote;
|
|
212
|
+
/**
|
|
213
|
+
* Enforce maximum local storage entries limit
|
|
214
|
+
*/
|
|
215
|
+
private enforceMaxLocalEntries;
|
|
216
|
+
/**
|
|
217
|
+
* Sort entries by specified field
|
|
218
|
+
* @param entries Array of entries to sort
|
|
219
|
+
* @param key Sorting field name
|
|
220
|
+
* @returns Sorted array of entries
|
|
221
|
+
*/
|
|
222
|
+
private sortEntries;
|
|
223
|
+
/**
|
|
224
|
+
* Add data
|
|
225
|
+
*/
|
|
226
|
+
add(data: T): Promise<T>;
|
|
227
|
+
/**
|
|
228
|
+
* Update data
|
|
229
|
+
*/
|
|
230
|
+
update(id: string, data: Partial<T>): Promise<T>;
|
|
231
|
+
/**
|
|
232
|
+
* Delete data
|
|
233
|
+
*/
|
|
234
|
+
delete(id: string): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Get data by ID
|
|
237
|
+
*/
|
|
238
|
+
getData(id: string): Promise<T | null>;
|
|
239
|
+
/**
|
|
240
|
+
* Get list data
|
|
241
|
+
*/
|
|
242
|
+
getList(options?: QueryOptions<T>): Promise<T[]>;
|
|
243
|
+
/**
|
|
244
|
+
* Get query object for list data
|
|
245
|
+
*/
|
|
246
|
+
getListQuery(options?: QueryOptions<T>): Promise<QueryResult<T[]>>;
|
|
247
|
+
/**
|
|
248
|
+
* Clear store
|
|
249
|
+
*/
|
|
250
|
+
clear(): Promise<void>;
|
|
251
|
+
}
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/sync-manager.d.ts
|
|
254
|
+
/**
|
|
255
|
+
* SyncManager configuration options
|
|
256
|
+
*/
|
|
257
|
+
interface SyncManagerOptions {
|
|
258
|
+
localAdapter?: LocalAdapter;
|
|
259
|
+
remoteAdapter?: RemoteAdapter;
|
|
260
|
+
notThrowLocalError?: boolean;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Sync manager
|
|
264
|
+
*/
|
|
265
|
+
declare class SyncManager {
|
|
266
|
+
private localAdapter;
|
|
267
|
+
private remoteAdapter;
|
|
268
|
+
private notThrowLocalError;
|
|
269
|
+
private stores;
|
|
270
|
+
constructor(options?: SyncManagerOptions);
|
|
271
|
+
/**
|
|
272
|
+
* Set local adapter
|
|
273
|
+
*/
|
|
274
|
+
setLocalAdapter(adapter: LocalAdapter): void;
|
|
275
|
+
/**
|
|
276
|
+
* Set remote adapter
|
|
277
|
+
*/
|
|
278
|
+
setRemoteAdapter(adapter: RemoteAdapter): void;
|
|
279
|
+
/**
|
|
280
|
+
* Create Store
|
|
281
|
+
*/
|
|
282
|
+
createStore<T extends Record<string, unknown>>(storeName: string, config?: StoreConfig): Store<T>;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Create sync manager
|
|
286
|
+
*/
|
|
287
|
+
declare function createSyncManager(options?: SyncManagerOptions): SyncManager;
|
|
288
|
+
//#endregion
|
|
289
|
+
export { Adapter, AdapterType, AuthType, Authentication, BaseAdapter, LocalAdapter, PaginatedResult, QueryOptions, QueryResult, RemoteAdapter, ServiceConfig, Store, StoreConfig, StoreOptions, SyncManager, SyncManagerOptions, TokenAuth, createSyncManager };
|
|
290
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/store.ts","../src/sync-manager.ts"],"sourcesContent":[],"mappings":";;AAGA;AAQA;AASiB,aAjBL,WAAA;EA4BK,KAAA,GAAA,OAAA;EAWA,MAAA,GAAA,QAAa;AAQ9B;;;;AAIiB,aA3CL,QAAA;EAiDK,KAAA,GAAA,OAAA;EAUA,KAAA,GAAA,OAAW;EAEH,MAAA,GAAA,QAAA;;;;;AAOR,UA3DA,SAAA,CA2DW;EAUX,YAAA,EAAW,MAAA;EAQX,UAAA,EAAA,MAAa;EACb,UAAY,CAAA,EAAA,MAAA;EAKb,aAAA,CAAA,EAAA,MAAA;EAEN,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,OAAA;;;;;AAUA,UApFO,cAAA,CAoFP;EAEG,QAAA,EArFD,QAqFC;EAAR,KAAA,CAAA,EApFK,SAoFL;EAKoD,QAAA,CAAA,EAAA,MAAA;EAKrC,QAAA,CAAA,EAAA,MAAA;EAIP,MAAA,CAAA,EAAA,MAAA;;;;;AASA,UAlGI,aAAA,CAkGJ;EAAR,QAAA,EAAA,MAAA;EAKuB,cAAA,CAAA,EArGT,cAqGS;;;;AAe5B;AACiB,UA/GA,YA+GY,CAAA,IA/GK,MA+GL,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA,CAAA;EAKb,MAAA,CAAA,EAAA,OAAA,GAAA,QAAA;EAEN,IAAA,CAAA,EAAA,MAAA;EAEG,KAAA,CAAA,EAAA,MAAA;EAAR,KAAA,CAAA,EApHK,OAoHL,CApHa,CAoHb,CAAA;;;;;AAUA,UAxHY,eAwHZ,CAAA,CAAA,CAAA,CAAA;EAKoD,IAAA,EA5HjD,CA4HiD,EAAA;EAKrC,UAAA,EAAA,MAAA;EAIP,IAAA,EAAA,MAAA;EAAR,KAAA,EAAA,MAAA;;;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JE;;;EA8BuB,KAAA,CAAA,CAAA,EAyMxB,OAzMwB,CAAA,IAAA,CAAA;;;;ADhPzC;AASA;AAWA;AAWiB,UEhCA,kBAAA,CFkCE;EAMF,YAAA,CAAA,EEvCA,YFuCY;EAAK,aAAA,CAAA,EEtChB,aFsCgB;EAIhB,kBAAA,CAAA,EAAA,OAAA;;;AAMlB;AAUA;AAEyB,cErDZ,WAAA,CFqDY;EAAR,QAAA,YAAA;EACe,QAAA,aAAA;EAAR,QAAA,kBAAA;EAAO,QAAA,MAAA;EAMd,WAAA,CAAA,OAAW,CAAA,EEtDL,kBFsDK;EAUX;AAQjB;;EAMgB,eAAA,CAAA,OAAA,EErEW,YFqEX,CAAA,EAAA,IAAA;EAEN;;;EAOS,gBAAA,CAAA,OAAA,EEvES,aFuET,CAAA,EAAA,IAAA;EAGD;;;EAEb,WAAA,CAAA,UErEmB,MFqEnB,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA,CAAA,SAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EEnEO,WFmEP,CAAA,EElEA,KFkEA,CElEM,CFkEN,CAAA;;;;;AAmBe,iBE5DJ,iBAAA,CF4DI,OAAA,CAAA,EE5DuB,kBF4DvB,CAAA,EE5DiD,WF4DjD"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
//#region src/types.ts
|
|
2
|
+
/**
|
|
3
|
+
* Adapter type: local or remote
|
|
4
|
+
*/
|
|
5
|
+
let AdapterType = /* @__PURE__ */ function(AdapterType$1) {
|
|
6
|
+
AdapterType$1["Local"] = "local";
|
|
7
|
+
AdapterType$1["Remote"] = "remote";
|
|
8
|
+
return AdapterType$1;
|
|
9
|
+
}({});
|
|
10
|
+
/**
|
|
11
|
+
* Authentication type
|
|
12
|
+
*/
|
|
13
|
+
let AuthType = /* @__PURE__ */ function(AuthType$1) {
|
|
14
|
+
AuthType$1["Token"] = "token";
|
|
15
|
+
AuthType$1["Basic"] = "basic";
|
|
16
|
+
AuthType$1["Bearer"] = "bearer";
|
|
17
|
+
return AuthType$1;
|
|
18
|
+
}({});
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/store.ts
|
|
22
|
+
/**
|
|
23
|
+
* Store class
|
|
24
|
+
*/
|
|
25
|
+
var Store = class {
|
|
26
|
+
storeName;
|
|
27
|
+
localAdapter;
|
|
28
|
+
remoteAdapter;
|
|
29
|
+
idKey;
|
|
30
|
+
indexes;
|
|
31
|
+
maxLocalEntries;
|
|
32
|
+
sortByKey;
|
|
33
|
+
onChange;
|
|
34
|
+
notThrowLocalError;
|
|
35
|
+
initialized = false;
|
|
36
|
+
constructor(storeName, options) {
|
|
37
|
+
this.storeName = storeName;
|
|
38
|
+
this.localAdapter = options.localAdapter;
|
|
39
|
+
this.remoteAdapter = options.remoteAdapter;
|
|
40
|
+
this.idKey = options.idKey;
|
|
41
|
+
this.indexes = options.indexes;
|
|
42
|
+
this.maxLocalEntries = options.maxLocalEntries;
|
|
43
|
+
this.sortByKey = options.sortByKey;
|
|
44
|
+
this.onChange = options.onChange;
|
|
45
|
+
this.notThrowLocalError = options.notThrowLocalError;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Initialize Store
|
|
49
|
+
*/
|
|
50
|
+
async init() {
|
|
51
|
+
if (this.initialized) return;
|
|
52
|
+
const promises = [];
|
|
53
|
+
if (this.localAdapter) promises.push(this.executeLocal(() => this.localAdapter.initStore(this.storeName, this.indexes, this.idKey)));
|
|
54
|
+
if (this.remoteAdapter) promises.push(this.executeRemote(() => this.remoteAdapter.initStore(this.storeName, this.indexes, this.idKey)));
|
|
55
|
+
await Promise.all(promises);
|
|
56
|
+
this.initialized = true;
|
|
57
|
+
}
|
|
58
|
+
async ensureInitialized() {
|
|
59
|
+
if (!this.initialized) await this.init();
|
|
60
|
+
}
|
|
61
|
+
triggerChange() {
|
|
62
|
+
if (this.onChange) this.onChange();
|
|
63
|
+
}
|
|
64
|
+
async executeLocal(operation) {
|
|
65
|
+
if (!this.localAdapter) return null;
|
|
66
|
+
try {
|
|
67
|
+
return await operation();
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (this.notThrowLocalError) {
|
|
70
|
+
console.warn("Local adapter error:", error);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async executeRemote(operation) {
|
|
77
|
+
return await operation();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Enforce maximum local storage entries limit
|
|
81
|
+
*/
|
|
82
|
+
async enforceMaxLocalEntries() {
|
|
83
|
+
if (!this.localAdapter || !this.maxLocalEntries) return;
|
|
84
|
+
try {
|
|
85
|
+
let currentEntries = await this.localAdapter.getList(this.storeName, {}, this.idKey);
|
|
86
|
+
if (currentEntries.length > this.maxLocalEntries) {
|
|
87
|
+
if (this.sortByKey) currentEntries = this.sortEntries(currentEntries, this.sortByKey);
|
|
88
|
+
const entriesToDelete = currentEntries.length - this.maxLocalEntries;
|
|
89
|
+
for (let i = 0; i < entriesToDelete; i++) {
|
|
90
|
+
const entryToDelete = currentEntries[i];
|
|
91
|
+
if (entryToDelete) {
|
|
92
|
+
const id = String(entryToDelete[this.idKey]);
|
|
93
|
+
await this.executeLocal(() => this.localAdapter.delete(this.storeName, id, this.idKey));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (this.notThrowLocalError) console.warn("Failed to enforce max local entries:", error);
|
|
99
|
+
else throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Sort entries by specified field
|
|
104
|
+
* @param entries Array of entries to sort
|
|
105
|
+
* @param key Sorting field name
|
|
106
|
+
* @returns Sorted array of entries
|
|
107
|
+
*/
|
|
108
|
+
sortEntries(entries, key) {
|
|
109
|
+
return [...entries].sort((a, b) => {
|
|
110
|
+
const aValue = a[key];
|
|
111
|
+
const bValue = b[key];
|
|
112
|
+
if (aValue == null && bValue == null) return 0;
|
|
113
|
+
if (aValue == null) return 1;
|
|
114
|
+
if (bValue == null) return -1;
|
|
115
|
+
if (typeof aValue === "number" && typeof bValue === "number") return aValue - bValue;
|
|
116
|
+
if (typeof aValue === "string" && typeof bValue === "string") return aValue.localeCompare(bValue);
|
|
117
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
118
|
+
const aDate = new Date(aValue);
|
|
119
|
+
const bDate = new Date(bValue);
|
|
120
|
+
if (!isNaN(aDate.getTime()) && !isNaN(bDate.getTime())) return aDate.getTime() - bDate.getTime();
|
|
121
|
+
}
|
|
122
|
+
return String(aValue).localeCompare(String(bValue));
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Add data
|
|
127
|
+
*/
|
|
128
|
+
async add(data) {
|
|
129
|
+
await this.ensureInitialized();
|
|
130
|
+
const promises = [];
|
|
131
|
+
if (this.localAdapter) {
|
|
132
|
+
const localResult = await this.executeLocal(() => this.localAdapter.add(this.storeName, data, this.idKey));
|
|
133
|
+
if (localResult) {
|
|
134
|
+
promises.push(Promise.resolve(localResult));
|
|
135
|
+
await this.enforceMaxLocalEntries();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (this.remoteAdapter) promises.push(this.remoteAdapter.add(this.storeName, data, this.idKey));
|
|
139
|
+
await Promise.all(promises);
|
|
140
|
+
this.triggerChange();
|
|
141
|
+
return data;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Update data
|
|
145
|
+
*/
|
|
146
|
+
async update(id, data) {
|
|
147
|
+
await this.ensureInitialized();
|
|
148
|
+
const promises = [];
|
|
149
|
+
if (this.localAdapter) {
|
|
150
|
+
const localResult = await this.executeLocal(() => this.localAdapter.update(this.storeName, id, data, this.idKey));
|
|
151
|
+
if (localResult) promises.push(Promise.resolve(localResult));
|
|
152
|
+
}
|
|
153
|
+
if (this.remoteAdapter) promises.push(this.remoteAdapter.update(this.storeName, id, data, this.idKey));
|
|
154
|
+
const results = await Promise.all(promises);
|
|
155
|
+
this.triggerChange();
|
|
156
|
+
return results.find((result) => result !== null && result !== void 0);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Delete data
|
|
160
|
+
*/
|
|
161
|
+
async delete(id) {
|
|
162
|
+
await this.ensureInitialized();
|
|
163
|
+
const promises = [];
|
|
164
|
+
if (this.localAdapter) promises.push(this.executeLocal(() => this.localAdapter.delete(this.storeName, id, this.idKey)));
|
|
165
|
+
if (this.remoteAdapter) promises.push(this.remoteAdapter.delete(this.storeName, id, this.idKey));
|
|
166
|
+
await Promise.all(promises);
|
|
167
|
+
this.triggerChange();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get data by ID
|
|
171
|
+
*/
|
|
172
|
+
async getData(id) {
|
|
173
|
+
await this.ensureInitialized();
|
|
174
|
+
if (this.localAdapter) try {
|
|
175
|
+
const localResult = await this.executeLocal(() => this.localAdapter.getData(this.storeName, id, this.idKey));
|
|
176
|
+
if (localResult) return localResult;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (!this.notThrowLocalError) throw error;
|
|
179
|
+
}
|
|
180
|
+
if (this.remoteAdapter) return await this.remoteAdapter.getData(this.storeName, id, this.idKey);
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Get list data
|
|
185
|
+
*/
|
|
186
|
+
async getList(options = {}) {
|
|
187
|
+
await this.ensureInitialized();
|
|
188
|
+
const source = options.source;
|
|
189
|
+
if (source === "local" && this.localAdapter) return await this.localAdapter.getList(this.storeName, options, this.idKey);
|
|
190
|
+
if (source === "remote" && this.remoteAdapter) return (await this.remoteAdapter.getList(this.storeName, options, this.idKey)).data;
|
|
191
|
+
if (this.remoteAdapter) try {
|
|
192
|
+
const result = await this.remoteAdapter.getList(this.storeName, options, this.idKey);
|
|
193
|
+
if (this.localAdapter && result.data.length > 0) {
|
|
194
|
+
await Promise.all(result.data.map((item) => this.executeLocal(() => this.localAdapter.add(this.storeName, item, this.idKey))));
|
|
195
|
+
await this.enforceMaxLocalEntries();
|
|
196
|
+
}
|
|
197
|
+
return result.data;
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (this.localAdapter) return await this.localAdapter.getList(this.storeName, options, this.idKey);
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
if (this.localAdapter) return await this.localAdapter.getList(this.storeName, options, this.idKey);
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Get query object for list data
|
|
207
|
+
*/
|
|
208
|
+
async getListQuery(options = {}) {
|
|
209
|
+
await this.ensureInitialized();
|
|
210
|
+
const source = options.source;
|
|
211
|
+
const queryKey = [
|
|
212
|
+
"store",
|
|
213
|
+
this.storeName,
|
|
214
|
+
source || "default",
|
|
215
|
+
options
|
|
216
|
+
];
|
|
217
|
+
const queryFn = async () => {
|
|
218
|
+
return this.getList(options);
|
|
219
|
+
};
|
|
220
|
+
const getInitialData = async () => {
|
|
221
|
+
if (this.localAdapter) try {
|
|
222
|
+
return await this.localAdapter.getList(this.storeName, options, this.idKey);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
return [];
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
queryKey,
|
|
230
|
+
queryFn,
|
|
231
|
+
getInitialData
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Clear store
|
|
236
|
+
*/
|
|
237
|
+
async clear() {
|
|
238
|
+
await this.ensureInitialized();
|
|
239
|
+
const promises = [];
|
|
240
|
+
if (this.localAdapter) await this.executeLocal(() => this.localAdapter.clear(this.storeName));
|
|
241
|
+
if (this.remoteAdapter) promises.push(this.remoteAdapter.clear(this.storeName));
|
|
242
|
+
await Promise.all(promises);
|
|
243
|
+
this.triggerChange();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/sync-manager.ts
|
|
249
|
+
/**
|
|
250
|
+
* Sync manager
|
|
251
|
+
*/
|
|
252
|
+
var SyncManager = class {
|
|
253
|
+
localAdapter = null;
|
|
254
|
+
remoteAdapter = null;
|
|
255
|
+
notThrowLocalError;
|
|
256
|
+
stores = /* @__PURE__ */ new Map();
|
|
257
|
+
constructor(options = {}) {
|
|
258
|
+
this.localAdapter = options.localAdapter ?? null;
|
|
259
|
+
this.remoteAdapter = options.remoteAdapter ?? null;
|
|
260
|
+
this.notThrowLocalError = options.notThrowLocalError ?? false;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Set local adapter
|
|
264
|
+
*/
|
|
265
|
+
setLocalAdapter(adapter) {
|
|
266
|
+
this.localAdapter = adapter;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Set remote adapter
|
|
270
|
+
*/
|
|
271
|
+
setRemoteAdapter(adapter) {
|
|
272
|
+
this.remoteAdapter = adapter;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Create Store
|
|
276
|
+
*/
|
|
277
|
+
createStore(storeName, config = {}) {
|
|
278
|
+
const existingStore = this.stores.get(storeName);
|
|
279
|
+
if (existingStore) return existingStore;
|
|
280
|
+
const store = new Store(storeName, {
|
|
281
|
+
localAdapter: this.localAdapter,
|
|
282
|
+
remoteAdapter: this.remoteAdapter,
|
|
283
|
+
idKey: config.idKey ?? "id",
|
|
284
|
+
indexes: config.indexes,
|
|
285
|
+
maxLocalEntries: config.maxLocalEntries,
|
|
286
|
+
onChange: config.onChange,
|
|
287
|
+
notThrowLocalError: this.notThrowLocalError
|
|
288
|
+
});
|
|
289
|
+
this.stores.set(storeName, store);
|
|
290
|
+
return store;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Create sync manager
|
|
295
|
+
*/
|
|
296
|
+
function createSyncManager(options = {}) {
|
|
297
|
+
return new SyncManager(options);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
//#endregion
|
|
301
|
+
export { AdapterType, AuthType, Store, SyncManager, createSyncManager };
|
|
302
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["promises: Promise<void | null>[]","promises: Promise<T>[]","promises: Promise<void>[]"],"sources":["../src/types.ts","../src/store.ts","../src/sync-manager.ts"],"sourcesContent":["/**\n * Adapter type: local or remote\n */\nexport enum AdapterType {\n Local = 'local',\n Remote = 'remote',\n}\n\n/**\n * Authentication type\n */\nexport enum AuthType {\n Token = 'token',\n Basic = 'basic',\n Bearer = 'bearer',\n}\n\n/**\n * Token authentication configuration\n */\nexport interface TokenAuth {\n access_token: string;\n token_type: string;\n expires_in?: number;\n refresh_token?: string;\n [key: string]: unknown;\n}\n\n/**\n * Authentication configuration\n */\nexport interface Authentication {\n authType: AuthType;\n token?: TokenAuth;\n username?: string;\n password?: string;\n bearer?: string;\n}\n\n/**\n * Service configuration (for remote adapters)\n */\nexport interface ServiceConfig {\n endpoint: string;\n authentication?: Authentication;\n}\n\n/**\n * Query options\n */\nexport interface QueryOptions<T = Record<string, unknown>> {\n source?: 'local' | 'remote';\n page?: number;\n limit?: number;\n where?: Partial<T>;\n}\n\n/**\n * Paginated result\n */\nexport interface PaginatedResult<T> {\n data: T[];\n totalCount: number;\n page: number;\n limit: number;\n}\n\n/**\n * Query function and initial data (for React Query, etc.)\n */\nexport interface QueryResult<T> {\n queryKey: unknown[];\n queryFn: () => Promise<T>;\n getInitialData: () => Promise<T>;\n}\n\n/**\n * Store configuration\n */\nexport interface StoreConfig {\n indexes?: string[];\n idKey?: string;\n maxLocalEntries?: number;\n onChange?: () => void;\n}\n\n/**\n * Base adapter interface\n */\nexport interface BaseAdapter {\n readonly type: AdapterType;\n readonly name: string;\n}\n\n/**\n * Local adapter interface\n */\nexport interface LocalAdapter extends BaseAdapter {\n readonly type: AdapterType.Local;\n \n /**\n * Add data\n */\n add<T extends Record<string, unknown>>(\n storeName: string,\n data: T,\n idKey?: string\n ): Promise<T>;\n \n /**\n * Update data\n */\n update<T extends Record<string, unknown>>(\n storeName: string,\n id: string,\n data: Partial<T>,\n idKey?: string\n ): Promise<T>;\n \n /**\n * Delete data\n */\n delete(storeName: string, id: string, idKey?: string): Promise<void>;\n \n /**\n * Get data by ID\n */\n getData<T extends Record<string, unknown>>(\n storeName: string,\n id: string,\n idKey?: string\n ): Promise<T | null>;\n \n /**\n * Get list data (with pagination support)\n */\n getList<T extends Record<string, unknown>>(\n storeName: string,\n options?: QueryOptions<T>,\n idKey?: string\n ): Promise<T[]>;\n \n /**\n * Clear store\n */\n clear(storeName: string): Promise<void>;\n \n /**\n * Initialize store\n */\n initStore(\n storeName: string,\n indexes?: string[],\n idKey?: string\n ): Promise<void>;\n}\n\n/**\n * Remote adapter interface\n */\nexport interface RemoteAdapter extends BaseAdapter {\n readonly type: AdapterType.Remote;\n \n /**\n * 添加数据\n */\n add<T extends Record<string, unknown>>(\n storeName: string,\n data: T,\n idKey?: string\n ): Promise<T>;\n \n /**\n * Update data\n */\n update<T extends Record<string, unknown>>(\n storeName: string,\n id: string,\n data: Partial<T>,\n idKey?: string\n ): Promise<T>;\n \n /**\n * Delete data\n */\n delete(storeName: string, id: string, idKey?: string): Promise<void>;\n \n /**\n * Get data by ID\n */\n getData<T extends Record<string, unknown>>(\n storeName: string,\n id: string,\n idKey?: string\n ): Promise<T | null>;\n \n /**\n * Get list data (with pagination support)\n */\n getList<T extends Record<string, unknown>>(\n storeName: string,\n options?: QueryOptions<T>,\n idKey?: string\n ): Promise<PaginatedResult<T>>;\n \n /**\n * Clear store\n */\n clear(storeName: string): Promise<void>;\n \n /**\n * Initialize store (create if not exists)\n */\n initStore(\n storeName: string,\n indexes?: string[],\n idKey?: string\n ): Promise<void>;\n \n /**\n * Set service configuration\n */\n setService(service: ServiceConfig): void;\n}\n\n/**\n * Adapter union type\n */\nexport type Adapter = LocalAdapter | RemoteAdapter;\n\n","import {\n type LocalAdapter,\n type RemoteAdapter,\n type QueryOptions,\n type QueryResult,\n} from \"./types.js\";\n\n/**\n * Store configuration\n */\nexport interface StoreOptions<_T extends Record<string, unknown>> {\n localAdapter: LocalAdapter | null;\n remoteAdapter: RemoteAdapter | null;\n idKey: string;\n indexes?: string[];\n /**\n * Maximum number of local storage entries\n * When local storage entries exceed this limit, oldest entries will be automatically deleted\n * Can be used with sortByKey parameter to specify sorting field for deletion order\n * If not set, no limit on local storage entries\n */\n maxLocalEntries?: number;\n /**\n * Field name for sorting when maxLocalEntries is in effect\n * If this field is specified, entries will be sorted by this field's value before deleting oldest entries\n * If not specified, entries will be deleted in default array order (usually insertion order)\n * Supports number and string type field values\n */\n sortByKey?: string;\n onChange?: () => void;\n notThrowLocalError: boolean;\n}\n\n/**\n * Store class\n */\nexport class Store<T extends Record<string, unknown>> {\n private storeName: string;\n private localAdapter: LocalAdapter | null;\n private remoteAdapter: RemoteAdapter | null;\n private idKey: string;\n private indexes?: string[];\n private maxLocalEntries?: number;\n private sortByKey?: string;\n private onChange?: () => void;\n private notThrowLocalError: boolean;\n private initialized = false;\n\n constructor(storeName: string, options: StoreOptions<T>) {\n this.storeName = storeName;\n this.localAdapter = options.localAdapter;\n this.remoteAdapter = options.remoteAdapter;\n this.idKey = options.idKey;\n this.indexes = options.indexes;\n this.maxLocalEntries = options.maxLocalEntries;\n this.sortByKey = options.sortByKey;\n this.onChange = options.onChange;\n this.notThrowLocalError = options.notThrowLocalError;\n }\n\n /**\n * Initialize Store\n */\n async init(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n const promises: Promise<void | null>[] = [];\n\n if (this.localAdapter) {\n promises.push(\n this.executeLocal(() =>\n this.localAdapter!.initStore(\n this.storeName,\n this.indexes,\n this.idKey,\n ),\n ),\n );\n }\n\n if (this.remoteAdapter) {\n promises.push(\n this.executeRemote(() =>\n this.remoteAdapter!.initStore(\n this.storeName,\n this.indexes,\n this.idKey,\n ),\n ),\n );\n }\n\n await Promise.all(promises);\n this.initialized = true;\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.init();\n }\n }\n\n private triggerChange(): void {\n if (this.onChange) {\n this.onChange();\n }\n }\n\n private async executeLocal<TResult>(\n operation: () => Promise<TResult>,\n ): Promise<TResult | null> {\n if (!this.localAdapter) {\n return null;\n }\n\n try {\n return await operation();\n } catch (error) {\n if (this.notThrowLocalError) {\n console.warn(\"Local adapter error:\", error);\n return null;\n }\n throw error;\n }\n }\n\n private async executeRemote<TResult>(\n operation: () => Promise<TResult>,\n ): Promise<TResult> {\n return await operation();\n }\n\n /**\n * Enforce maximum local storage entries limit\n */\n private async enforceMaxLocalEntries(): Promise<void> {\n if (!this.localAdapter || !this.maxLocalEntries) {\n return;\n }\n\n try {\n let currentEntries = await this.localAdapter.getList<T>(\n this.storeName,\n {},\n this.idKey,\n );\n\n if (currentEntries.length > this.maxLocalEntries) {\n // If sorting field is specified, sort by that field\n if (this.sortByKey) {\n currentEntries = this.sortEntries(currentEntries, this.sortByKey);\n }\n\n // Calculate number of entries to delete\n const entriesToDelete = currentEntries.length - this.maxLocalEntries;\n\n // Delete oldest entries (first element after sorting is the oldest)\n for (let i = 0; i < entriesToDelete; i++) {\n const entryToDelete = currentEntries[i];\n if (entryToDelete) {\n const id = String(entryToDelete[this.idKey]);\n await this.executeLocal(() =>\n this.localAdapter!.delete(this.storeName, id, this.idKey),\n );\n }\n }\n }\n } catch (error) {\n if (this.notThrowLocalError) {\n console.warn(\"Failed to enforce max local entries:\", error);\n } else {\n throw error;\n }\n }\n }\n\n /**\n * Sort entries by specified field\n * @param entries Array of entries to sort\n * @param key Sorting field name\n * @returns Sorted array of entries\n */\n private sortEntries(entries: T[], key: string): T[] {\n return [...entries].sort((a, b) => {\n const aValue = a[key];\n const bValue = b[key];\n\n // Handle undefined or null values\n if (aValue == null && bValue == null) return 0;\n if (aValue == null) return 1; // null/undefined at the end\n if (bValue == null) return -1; // null/undefined at the end\n\n // Number type sorting\n if (typeof aValue === \"number\" && typeof bValue === \"number\") {\n return aValue - bValue;\n }\n\n // String type sorting\n if (typeof aValue === \"string\" && typeof bValue === \"string\") {\n return aValue.localeCompare(bValue);\n }\n\n // Date type sorting (if string format date)\n if (typeof aValue === \"string\" && typeof bValue === \"string\") {\n const aDate = new Date(aValue);\n const bDate = new Date(bValue);\n if (!isNaN(aDate.getTime()) && !isNaN(bDate.getTime())) {\n return aDate.getTime() - bDate.getTime();\n }\n }\n\n // Convert other types to string for comparison\n return String(aValue).localeCompare(String(bValue));\n });\n }\n\n /**\n * Add data\n */\n async add(data: T): Promise<T> {\n await this.ensureInitialized();\n\n // Write to both local and remote simultaneously\n const promises: Promise<T>[] = [];\n\n if (this.localAdapter) {\n const localResult = await this.executeLocal(() =>\n this.localAdapter!.add(this.storeName, data, this.idKey),\n );\n if (localResult) {\n promises.push(Promise.resolve(localResult));\n\n // Check local storage entries limit\n await this.enforceMaxLocalEntries();\n }\n }\n\n if (this.remoteAdapter) {\n promises.push(this.remoteAdapter.add(this.storeName, data, this.idKey));\n }\n\n await Promise.all(promises);\n this.triggerChange();\n return data;\n }\n\n /**\n * Update data\n */\n async update(id: string, data: Partial<T>): Promise<T> {\n await this.ensureInitialized();\n\n const promises: Promise<T>[] = [];\n\n if (this.localAdapter) {\n const localResult = await this.executeLocal(() =>\n this.localAdapter!.update<T>(this.storeName, id, data, this.idKey),\n );\n if (localResult) {\n promises.push(Promise.resolve(localResult));\n }\n }\n\n if (this.remoteAdapter) {\n promises.push(\n this.remoteAdapter.update<T>(this.storeName, id, data, this.idKey),\n );\n }\n\n const results = await Promise.all(promises);\n this.triggerChange();\n // Return first non-null result\n const firstResult = results.find(\n (result) => result !== null && result !== undefined,\n );\n return firstResult as T;\n }\n\n /**\n * Delete data\n */\n async delete(id: string): Promise<void> {\n await this.ensureInitialized();\n\n const promises: Promise<void | null>[] = [];\n\n if (this.localAdapter) {\n promises.push(\n this.executeLocal(() =>\n this.localAdapter!.delete(this.storeName, id, this.idKey),\n ),\n );\n }\n\n if (this.remoteAdapter) {\n promises.push(this.remoteAdapter.delete(this.storeName, id, this.idKey));\n }\n\n await Promise.all(promises);\n this.triggerChange();\n }\n\n /**\n * Get data by ID\n */\n async getData(id: string): Promise<T | null> {\n await this.ensureInitialized();\n\n // Prioritize getting from local\n if (this.localAdapter) {\n try {\n const localResult = await this.executeLocal(() =>\n this.localAdapter!.getData(this.storeName, id, this.idKey),\n );\n if (localResult) {\n return localResult as T;\n }\n } catch (error) {\n if (!this.notThrowLocalError) {\n throw error;\n }\n }\n }\n\n // Get from remote\n if (this.remoteAdapter) {\n return await this.remoteAdapter.getData(this.storeName, id, this.idKey);\n }\n\n return null;\n }\n\n /**\n * Get list data\n */\n async getList(options: QueryOptions<T> = {}): Promise<T[]> {\n await this.ensureInitialized();\n\n const source = options.source;\n\n // If source is specified\n if (source === \"local\" && this.localAdapter) {\n return await this.localAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n }\n\n if (source === \"remote\" && this.remoteAdapter) {\n const result = await this.remoteAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n return result.data;\n }\n\n // Default to getting from remote\n if (this.remoteAdapter) {\n try {\n const result = await this.remoteAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n\n // Cache to local\n if (this.localAdapter && result.data.length > 0) {\n await Promise.all(\n result.data.map((item) =>\n this.executeLocal(() =>\n this.localAdapter!.add(this.storeName, item, this.idKey),\n ),\n ),\n );\n\n // Check local storage entries limit\n await this.enforceMaxLocalEntries();\n }\n\n return result.data;\n } catch (error) {\n // If remote fails, try to get from local\n if (this.localAdapter) {\n return await this.localAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n }\n throw error;\n }\n }\n\n // Only local adapter\n if (this.localAdapter) {\n return await this.localAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n }\n\n return [];\n }\n\n /**\n * Get query object for list data\n */\n async getListQuery(options: QueryOptions<T> = {}): Promise<QueryResult<T[]>> {\n await this.ensureInitialized();\n\n const source = options.source;\n\n // Build query key\n const queryKey = [\"store\", this.storeName, source || \"default\", options];\n\n // Build query function\n const queryFn = async (): Promise<T[]> => {\n return this.getList(options);\n };\n\n // Build initial data fetch function\n const getInitialData = async (): Promise<T[]> => {\n if (this.localAdapter) {\n try {\n return await this.localAdapter.getList<T>(\n this.storeName,\n options,\n this.idKey,\n );\n } catch (error) {\n // If local fetch fails, return empty array\n return [];\n }\n }\n return [];\n };\n\n return {\n queryKey,\n queryFn,\n getInitialData,\n };\n }\n\n /**\n * Clear store\n */\n async clear(): Promise<void> {\n await this.ensureInitialized();\n\n const promises: Promise<void>[] = [];\n\n if (this.localAdapter) {\n await this.executeLocal(() => this.localAdapter!.clear(this.storeName));\n }\n\n if (this.remoteAdapter) {\n promises.push(this.remoteAdapter.clear(this.storeName));\n }\n\n await Promise.all(promises);\n this.triggerChange();\n }\n}\n","import {\n type LocalAdapter,\n type RemoteAdapter,\n type StoreConfig,\n} from './types.js';\nimport { Store } from './store.js';\n\n/**\n * SyncManager configuration options\n */\nexport interface SyncManagerOptions {\n localAdapter?: LocalAdapter;\n remoteAdapter?: RemoteAdapter;\n notThrowLocalError?: boolean;\n}\n\n/**\n * Sync manager\n */\nexport class SyncManager {\n private localAdapter: LocalAdapter | null = null;\n private remoteAdapter: RemoteAdapter | null = null;\n private notThrowLocalError: boolean;\n private stores: Map<string, Store<Record<string, unknown>>> = new Map();\n\n constructor(options: SyncManagerOptions = {}) {\n this.localAdapter = options.localAdapter ?? null;\n this.remoteAdapter = options.remoteAdapter ?? null;\n this.notThrowLocalError = options.notThrowLocalError ?? false;\n }\n\n /**\n * Set local adapter\n */\n setLocalAdapter(adapter: LocalAdapter): void {\n this.localAdapter = adapter;\n }\n\n /**\n * Set remote adapter\n */\n setRemoteAdapter(adapter: RemoteAdapter): void {\n this.remoteAdapter = adapter;\n }\n\n /**\n * Create Store\n */\n createStore<T extends Record<string, unknown>>(\n storeName: string,\n config: StoreConfig = {}\n ): Store<T> {\n // If already exists, return existing store\n const existingStore = this.stores.get(storeName);\n if (existingStore) {\n return existingStore as Store<T>;\n }\n\n const store = new Store<T>(storeName, {\n localAdapter: this.localAdapter,\n remoteAdapter: this.remoteAdapter,\n idKey: config.idKey ?? 'id',\n indexes: config.indexes,\n maxLocalEntries: config.maxLocalEntries,\n onChange: config.onChange,\n notThrowLocalError: this.notThrowLocalError,\n });\n\n this.stores.set(storeName, store as Store<Record<string, unknown>>);\n return store;\n }\n}\n\n/**\n * Create sync manager\n */\nexport function createSyncManager(options: SyncManagerOptions = {}): SyncManager {\n return new SyncManager(options);\n}\n\n"],"mappings":";;;;AAGA,IAAY,sDAAL;AACL;AACA;;;;;;AAMF,IAAY,gDAAL;AACL;AACA;AACA;;;;;;;;;ACsBF,IAAa,QAAb,MAAsD;CACpD,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ,cAAc;CAEtB,YAAY,WAAmB,SAA0B;AACvD,OAAK,YAAY;AACjB,OAAK,eAAe,QAAQ;AAC5B,OAAK,gBAAgB,QAAQ;AAC7B,OAAK,QAAQ,QAAQ;AACrB,OAAK,UAAU,QAAQ;AACvB,OAAK,kBAAkB,QAAQ;AAC/B,OAAK,YAAY,QAAQ;AACzB,OAAK,WAAW,QAAQ;AACxB,OAAK,qBAAqB,QAAQ;;;;;CAMpC,MAAM,OAAsB;AAC1B,MAAI,KAAK,YACP;EAGF,MAAMA,WAAmC,EAAE;AAE3C,MAAI,KAAK,aACP,UAAS,KACP,KAAK,mBACH,KAAK,aAAc,UACjB,KAAK,WACL,KAAK,SACL,KAAK,MACN,CACF,CACF;AAGH,MAAI,KAAK,cACP,UAAS,KACP,KAAK,oBACH,KAAK,cAAe,UAClB,KAAK,WACL,KAAK,SACL,KAAK,MACN,CACF,C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AoC;CAC5C,AAAQ,gBAAsC;CAC9C,AAAQ;CACR,AAAQ,yBAAsD,IAAI,KAAK;CAEvE,YAAY,UAA8B,EAAE,EAAE;AAC5C,OAAK,eAAe,QAAQ,gBAAgB;AAC5C,OAAK,gBAAgB,QAAQ,iBAAiB;AAC9C,OAAK,qBAAqB,QAAQ,sBAAsB;;;;;CAM1D,gBAAgB,SAA6B;AAC3C,OAAK,eAAe;;;;;CAMtB,iBAAiB,SAA8B;AAC7C,OAAK,gBAAgB;;;;;CAMvB,YACE,WACA,SAAsB,EAAE,EACd;EAEV,MAAM,gBAAgB,KAAK,OAAO,IAAI,UAAU;AAChD,MAAI,cACF,QAAO;EAGT,MAAM,QAAQ,IAAI,MAAS,WAAW;GACpC,cAAc,KAAK;GACnB,eAAe,KAAK;GACpB,OAAO,OAAO,SAAS;GACvB,SAAS,OAAO;GAChB,iBAAiB,OAAO;GACxB,UAAU,OAAO;GACjB,oBAAoB,KAAK;GAC1B,CAAC;AAEF,OAAK,OAAO,IAAI,WAAW,MAAwC;AACnE,SAAO;;;;;;AAOX,SAAgB,kBAAkB,UAA8B,EAAE,EAAe;AAC/E,QAAO,IAAI,YAAY,QAAQ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@omnistreamai/data-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.mts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"import": "./dist/index.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsdown",
|
|
15
|
+
"test": "vitest run",
|
|
16
|
+
"type-check": "tsc --noEmit"
|
|
17
|
+
}
|
|
18
|
+
}
|