tinybase 3.1.0-beta.4 → 3.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.
Files changed (85) hide show
  1. package/lib/debug/ui-react.js +2 -2
  2. package/lib/{checkpoints.d.ts → types/checkpoints.d.ts} +13 -13
  3. package/lib/{debug → types}/indexes.d.ts +10 -16
  4. package/lib/{debug → types}/metrics.d.ts +49 -50
  5. package/lib/{debug → types}/persisters.d.ts +26 -30
  6. package/lib/{cjs-es6 → types}/queries.d.ts +474 -309
  7. package/lib/{debug → types}/relationships.d.ts +11 -17
  8. package/lib/{debug → types}/store.d.ts +180 -1038
  9. package/lib/types/tinybase.d.ts +14 -0
  10. package/lib/{debug → types}/tools.d.ts +12 -12
  11. package/lib/{debug → types}/ui-react.d.ts +11 -5
  12. package/lib/{cjs-es6 → types/with-schemas}/checkpoints.d.ts +93 -7
  13. package/lib/{cjs → types/with-schemas}/indexes.d.ts +136 -18
  14. package/lib/types/with-schemas/internal/queries.d.ts +15 -0
  15. package/lib/types/with-schemas/internal/store.d.ts +101 -0
  16. package/lib/types/with-schemas/internal/ui-react.d.ts +776 -0
  17. package/lib/{cjs → types/with-schemas}/metrics.d.ts +115 -50
  18. package/lib/{cjs → types/with-schemas}/persisters.d.ts +102 -4
  19. package/lib/{debug → types/with-schemas}/queries.d.ts +865 -346
  20. package/lib/{cjs → types/with-schemas}/relationships.d.ts +151 -20
  21. package/lib/{cjs-es6 → types/with-schemas}/store.d.ts +1082 -801
  22. package/lib/types/with-schemas/tinybase.d.ts +14 -0
  23. package/lib/{cjs-es6 → types/with-schemas}/tools.d.ts +21 -10
  24. package/lib/{cjs-es6 → types/with-schemas}/ui-react.d.ts +1871 -1314
  25. package/package.json +145 -29
  26. package/readme.md +14 -14
  27. package/lib/cjs/checkpoints.d.ts +0 -961
  28. package/lib/cjs/queries.d.ts +0 -3028
  29. package/lib/cjs/store.d.ts +0 -6143
  30. package/lib/cjs/tinybase.d.ts +0 -14
  31. package/lib/cjs/tools.d.ts +0 -536
  32. package/lib/cjs/ui-react.d.ts +0 -10921
  33. package/lib/cjs-es6/indexes.d.ts +0 -974
  34. package/lib/cjs-es6/metrics.d.ts +0 -829
  35. package/lib/cjs-es6/persisters.d.ts +0 -733
  36. package/lib/cjs-es6/relationships.d.ts +0 -1209
  37. package/lib/cjs-es6/tinybase.d.ts +0 -14
  38. package/lib/common.d.ts +0 -115
  39. package/lib/debug/checkpoints.d.ts +0 -961
  40. package/lib/debug/common.d.ts +0 -115
  41. package/lib/debug/tinybase.d.ts +0 -14
  42. package/lib/es6/checkpoints.d.ts +0 -961
  43. package/lib/es6/common.d.ts +0 -115
  44. package/lib/es6/indexes.d.ts +0 -974
  45. package/lib/es6/metrics.d.ts +0 -829
  46. package/lib/es6/persisters.d.ts +0 -733
  47. package/lib/es6/queries.d.ts +0 -3028
  48. package/lib/es6/relationships.d.ts +0 -1209
  49. package/lib/es6/store.d.ts +0 -6143
  50. package/lib/es6/tinybase.d.ts +0 -14
  51. package/lib/es6/tools.d.ts +0 -536
  52. package/lib/es6/ui-react.d.ts +0 -10921
  53. package/lib/indexes.d.ts +0 -974
  54. package/lib/metrics.d.ts +0 -829
  55. package/lib/persisters.d.ts +0 -733
  56. package/lib/queries.d.ts +0 -3028
  57. package/lib/relationships.d.ts +0 -1209
  58. package/lib/store.d.ts +0 -6143
  59. package/lib/tinybase.d.ts +0 -14
  60. package/lib/tools.d.ts +0 -536
  61. package/lib/ui-react.d.ts +0 -10921
  62. package/lib/umd/checkpoints.d.ts +0 -961
  63. package/lib/umd/common.d.ts +0 -115
  64. package/lib/umd/indexes.d.ts +0 -974
  65. package/lib/umd/metrics.d.ts +0 -829
  66. package/lib/umd/persisters.d.ts +0 -733
  67. package/lib/umd/queries.d.ts +0 -3028
  68. package/lib/umd/relationships.d.ts +0 -1209
  69. package/lib/umd/store.d.ts +0 -6143
  70. package/lib/umd/tinybase.d.ts +0 -14
  71. package/lib/umd/tools.d.ts +0 -536
  72. package/lib/umd/ui-react.d.ts +0 -10921
  73. package/lib/umd-es6/checkpoints.d.ts +0 -961
  74. package/lib/umd-es6/common.d.ts +0 -115
  75. package/lib/umd-es6/indexes.d.ts +0 -974
  76. package/lib/umd-es6/metrics.d.ts +0 -829
  77. package/lib/umd-es6/persisters.d.ts +0 -733
  78. package/lib/umd-es6/queries.d.ts +0 -3028
  79. package/lib/umd-es6/relationships.d.ts +0 -1209
  80. package/lib/umd-es6/store.d.ts +0 -6143
  81. package/lib/umd-es6/tinybase.d.ts +0 -14
  82. package/lib/umd-es6/tools.d.ts +0 -536
  83. package/lib/umd-es6/ui-react.d.ts +0 -10921
  84. /package/lib/{cjs-es6 → types}/common.d.ts +0 -0
  85. /package/lib/{cjs → types/with-schemas}/common.d.ts +0 -0
@@ -1,733 +0,0 @@
1
- /**
2
- * The persisters module of the TinyBase project provides a simple framework for
3
- * saving and loading Store data, to and from different destinations, or
4
- * underlying storage types.
5
- *
6
- * Several entry points are provided, each of which returns a new Persister
7
- * object that can load and save a Store:
8
- *
9
- * - The createSessionPersister function returns a Persister that uses the
10
- * browser's session storage.
11
- * - The createLocalPersister function returns a Persister that uses the
12
- * browser's local storage.
13
- * - The createRemotePersister function returns a Persister that uses a remote
14
- * server.
15
- * - The createFilePersister function returns a Persister that uses a local file
16
- * (in an appropriate environment).
17
- *
18
- * Since persistence requirements can be different for every app, the
19
- * createCustomPersister function can also be used to easily create a fully
20
- * customized way to save and load Store data.
21
- *
22
- * @see Persisting Data guide
23
- * @see Countries demo
24
- * @see Todo App demos
25
- * @see Drawing demo
26
- * @packageDocumentation
27
- * @module persisters
28
- */
29
-
30
- import {NoSchemas, OptionalSchemas, Store, Tables, Values} from './store.d';
31
- import {Callback} from './common.d';
32
-
33
- /**
34
- * The PersisterStats type describes the number of times a Persister object has
35
- * loaded or saved data.
36
- *
37
- * A PersisterStats object is returned from the getStats method, and is only
38
- * populated in a debug build.
39
- *
40
- * @category Development
41
- */
42
- export type PersisterStats = {
43
- /**
44
- * The number of times data has been loaded.
45
- */
46
- loads?: number;
47
- /**
48
- * The number of times data has been saved.
49
- */
50
- saves?: number;
51
- };
52
-
53
- /**
54
- * A Persister object lets you save and load Store data to and from different
55
- * locations, or underlying storage types.
56
- *
57
- * This is useful for preserving Store data between browser sessions or reloads,
58
- * saving or loading browser state to or from a server, or saving Store data to
59
- * disk in a environment with filesystem access.
60
- *
61
- * Creating a Persister depends on the choice of underlying storage where the
62
- * data is to be stored. Options include the createSessionPersister function,
63
- * the createLocalPersister, the createRemotePersister function, and the
64
- * createFilePersister function. The createCustomPersister function can also be
65
- * used to easily create a fully customized way to save and load Store data.
66
- *
67
- * A Persister lets you explicit save or load data, with the save method and the
68
- * load method respectively. These methods are both asynchronous (since the
69
- * underlying data storage may also be) and return promises. As a result you
70
- * should use the `await` keyword to call them in a way that guarantees
71
- * subsequent execution order.
72
- *
73
- * When you don't want to deal with explicit persistence operations, a Persister
74
- * object also provides automatic saving and loading. Automatic saving listens
75
- * for changes to the Store and persists the data immediately. Automatic loading
76
- * listens (or polls) for changes to the persisted data and reflects those
77
- * changes in the Store.
78
- *
79
- * You can start automatic saving or loading with the startAutoSave method and
80
- * startAutoLoad method. Both are asynchronous since they will do an immediate
81
- * save and load before starting to listen for subsequent changes. You can stop
82
- * the behavior with the stopAutoSave method and stopAutoLoad method (which are
83
- * synchronous).
84
- *
85
- * You may often want to have both automatic saving and loading of a Store so
86
- * that changes are constantly synchronized (allowing basic state preservation
87
- * between browser tabs, for example). The framework has some basic provisions
88
- * to prevent race conditions - for example it will not attempt to save data if
89
- * it is currently loading it and vice-versa.
90
- *
91
- * Be aware, however, that the default implementations do not provide complex
92
- * synchronization heuristics and you should comprehensively test your
93
- * persistence strategy to understand the opportunity for data loss (in the case
94
- * of trying to save data to a server under poor network conditions, for
95
- * example).
96
- *
97
- * @example
98
- * This example creates a Store, persists it to the browser's session storage as
99
- * a JSON string, changes the persisted data, updates the Store from it, and
100
- * finally destroys the Persister again.
101
- *
102
- * ```js
103
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
104
- * const persister = createSessionPersister(store, 'pets');
105
- *
106
- * await persister.save();
107
- * console.log(sessionStorage.getItem('pets'));
108
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
109
- *
110
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
111
- * await persister.load();
112
- * console.log(store.getTables());
113
- * // -> {pets: {toto: {species: 'dog'}}}
114
- *
115
- * persister.destroy();
116
- * sessionStorage.clear();
117
- * ```
118
- * @example
119
- * This example creates a Store, and automatically saves and loads it to the
120
- * browser's session storage as a JSON string. Changes to the Store data, or the
121
- * persisted data (implicitly firing a StorageEvent), are reflected accordingly.
122
- *
123
- * ```js
124
- * const store = createStore();
125
- * const persister = createSessionPersister(store, 'pets');
126
- *
127
- * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
128
- * await persister.startAutoSave();
129
- *
130
- * store.setTables({pets: {felix: {species: 'cat'}}});
131
- * // ...
132
- * console.log(sessionStorage.getItem('pets'));
133
- * // -> '[{"pets":{"felix":{"species":"cat"}}},{}]'
134
- *
135
- * // In another browser tab:
136
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
137
- * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
138
- *
139
- * // ...
140
- * console.log(store.getTables());
141
- * // -> {pets: {toto: {species: 'dog'}}}
142
- *
143
- * persister.destroy();
144
- * sessionStorage.clear();
145
- * ```
146
- * @category Persister
147
- */
148
- export interface Persister<Schemas extends OptionalSchemas = NoSchemas> {
149
- /**
150
- * The load method gets persisted data from storage, and loads it into the
151
- * Store with which the Persister is associated, once.
152
- *
153
- * The optional parameter allows you to specify what the initial Tables object
154
- * for the Store will be if there is nothing currently persisted. Using this
155
- * instead of the `initialTables` parameter in the regular createStore
156
- * function allows you to easily instantiate a Store whether it's loading from
157
- * previously persisted storage or being run for the first time.
158
- *
159
- * This method is asynchronous because the persisted data may be on a remote
160
- * machine or a filesystem. Even for those storage types that are synchronous
161
- * (like browser storage) it is still recommended that you `await` calls to
162
- * this method or handle the return type natively as a Promise.
163
- *
164
- * @param initialTables An optional Tables object used when the underlying
165
- * storage has not previously been populated.
166
- * @param initialValues An optional Values object used when the underlying
167
- * storage has not previously been populated, since v3.0.0.
168
- * @returns A Promise containing a reference to the Persister object.
169
- * @example
170
- * This example creates an empty Store, and loads data into it from the
171
- * browser's session storage, which for the purposes of this example has been
172
- * previously populated.
173
- *
174
- * ```js
175
- * sessionStorage.setItem('pets', '[{"pets":{"fido":{"species":"dog"}}},{}]');
176
- *
177
- * const store = createStore();
178
- * const persister = createSessionPersister(store, 'pets');
179
- *
180
- * await persister.load();
181
- * console.log(store.getTables());
182
- * // -> {pets: {fido: {species: 'dog'}}}
183
- *
184
- * sessionStorage.clear();
185
- * ```
186
- * @example
187
- * This example creates an empty Store, and loads data into it from the
188
- * browser's session storage, which is at first empty, so the optional
189
- * parameter is used. The second time the load method is called, data has
190
- * previously been persisted and instead, that is loaded.
191
- *
192
- * ```js
193
- * const store = createStore();
194
- * const persister = createSessionPersister(store, 'pets');
195
- *
196
- * await persister.load({pets: {fido: {species: 'dog'}}});
197
- * console.log(store.getTables());
198
- * // -> {pets: {fido: {species: 'dog'}}}
199
- *
200
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
201
- * await persister.load({pets: {fido: {species: 'dog'}}});
202
- * console.log(store.getTables());
203
- * // -> {pets: {toto: {species: 'dog'}}}
204
- *
205
- * sessionStorage.clear();
206
- * ```
207
- * @category Load
208
- */
209
- load(
210
- initialTables?: Tables<Schemas[0], true>,
211
- initialValues?: Values,
212
- ): Promise<Persister<Schemas>>;
213
-
214
- /**
215
- * The startAutoLoad method gets persisted data from storage, and loads it
216
- * into the Store with which the Persister is associated, once, and then
217
- * continuously.
218
- *
219
- * The optional parameter allows you to specify what the initial Tables object
220
- * for the Store will be if there is nothing at first persisted. Using this
221
- * instead of the `initialTables` parameter in the regular createStore
222
- * function allows you to easily instantiate a Store whether it's loading from
223
- * previously persisted storage or being run for the first time.
224
- *
225
- * This method first runs a single call to the load method to ensure the data
226
- * is in sync with the persisted storage. It then continues to watch for
227
- * changes to the underlying data (either through events or polling, depending
228
- * on the storage type), automatically loading the data into the Store.
229
- *
230
- * This method is asynchronous because it starts by making a single call to
231
- * the asynchronous load method. Even for those storage types that are
232
- * synchronous (like browser storage) it is still recommended that you `await`
233
- * calls to this method or handle the return type natively as a Promise.
234
- *
235
- * @param initialTables An optional Tables object used when the underlying
236
- * storage has not previously been populated.
237
- * @param initialValues An optional Values object used when the underlying
238
- * storage has not previously been populated, since v3.0.0.
239
- * @returns A Promise containing a reference to the Persister object.
240
- * @example
241
- * This example creates an empty Store, and loads data into it from the
242
- * browser's session storage, which at first is empty (so the `initialTables`
243
- * parameter is used). Subsequent changes to the underlying storage are then
244
- * reflected in the Store (in this case through detection of StorageEvents
245
- * from session storage changes made in another browser tab).
246
- *
247
- * ```js
248
- * const store = createStore();
249
- * const persister = createSessionPersister(store, 'pets');
250
- *
251
- * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
252
- * console.log(store.getTables());
253
- * // -> {pets: {fido: {species: 'dog'}}}
254
- *
255
- * // In another browser tab:
256
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
257
- * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
258
- *
259
- * // ...
260
- * console.log(store.getTables());
261
- * // -> {pets: {toto: {species: 'dog'}}}
262
- *
263
- * persister.destroy();
264
- * sessionStorage.clear();
265
- * ```
266
- * @category Load
267
- */
268
- startAutoLoad(
269
- initialTables?: Tables<Schemas[0], true>,
270
- initialValues?: Values,
271
- ): Promise<Persister<Schemas>>;
272
-
273
- /**
274
- * The stopAutoLoad method stops the automatic loading of data from storage
275
- * previously started with the startAutoLoad method.
276
- *
277
- * If the Persister is not currently set to automatically load, this method
278
- * has no effect.
279
- *
280
- * @returns A reference to the Persister object.
281
- * @example
282
- * This example creates an empty Store, and starts automatically loading data
283
- * into it from the browser's session storage. Once the automatic loading is
284
- * stopped, subsequent changes are not reflected in the Store.
285
- *
286
- * ```js
287
- * const store = createStore();
288
- * const persister = createSessionPersister(store, 'pets');
289
- * await persister.startAutoLoad();
290
- *
291
- * // In another browser tab:
292
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
293
- * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
294
- * // ...
295
- * console.log(store.getTables());
296
- * // -> {pets: {toto: {species: 'dog'}}}
297
- *
298
- * persister.stopAutoLoad();
299
- *
300
- * // In another browser tab:
301
- * sessionStorage.setItem(
302
- * 'pets',
303
- * '[{"pets":{"felix":{"species":"cat"}}},{}]',
304
- * );
305
- * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
306
- * // ...
307
- * console.log(store.getTables());
308
- * // -> {pets: {toto: {species: 'dog'}}}
309
- * // Storage change has not been automatically loaded.
310
- *
311
- * persister.destroy();
312
- * sessionStorage.clear();
313
- * ```
314
- * @category Load
315
- */
316
- stopAutoLoad(): Persister<Schemas>;
317
-
318
- /**
319
- * The save method takes data from the Store with which the Persister is
320
- * associated and persists it into storage, once.
321
- *
322
- * This method is asynchronous because the persisted data may be on a remote
323
- * machine or a filesystem. Even for those storage types that are synchronous
324
- * (like browser storage) it is still recommended that you `await` calls to
325
- * this method or handle the return type natively as a Promise.
326
- *
327
- * @returns A Promise containing a reference to the Persister object.
328
- * @example
329
- * This example creates a Store with some data, and saves into the browser's
330
- * session storage.
331
- *
332
- * ```js
333
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
334
- * const persister = createSessionPersister(store, 'pets');
335
- *
336
- * await persister.save();
337
- * console.log(sessionStorage.getItem('pets'));
338
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
339
- *
340
- * persister.destroy();
341
- * sessionStorage.clear();
342
- * ```
343
- * @category Save
344
- */
345
- save(): Promise<Persister<Schemas>>;
346
-
347
- /**
348
- * The save method takes data from the Store with which the Persister is
349
- * associated and persists it into storage, once, and then continuously.
350
- *
351
- * This method first runs a single call to the save method to ensure the data
352
- * is in sync with the persisted storage. It then continues to watch for
353
- * changes to the Store, automatically saving the data to storage.
354
- *
355
- * This method is asynchronous because it starts by making a single call to
356
- * the asynchronous save method. Even for those storage types that are
357
- * synchronous (like browser storage) it is still recommended that you `await`
358
- * calls to this method or handle the return type natively as a Promise.
359
- *
360
- * @returns A Promise containing a reference to the Persister object.
361
- * @example
362
- * This example creates a Store with some data, and saves into the browser's
363
- * session storage. Subsequent changes to the Store are then automatically
364
- * saved to the underlying storage.
365
- *
366
- * ```js
367
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
368
- * const persister = createSessionPersister(store, 'pets');
369
- *
370
- * await persister.startAutoSave();
371
- * console.log(sessionStorage.getItem('pets'));
372
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
373
- *
374
- * store.setTables({pets: {toto: {species: 'dog'}}});
375
- * // ...
376
- * console.log(sessionStorage.getItem('pets'));
377
- * // -> '[{"pets":{"toto":{"species":"dog"}}},{}]'
378
- *
379
- * sessionStorage.clear();
380
- * ```
381
- * @category Save
382
- */
383
- startAutoSave(): Promise<Persister<Schemas>>;
384
-
385
- /**
386
- * The stopAutoSave method stops the automatic save of data to storage
387
- * previously started with the startAutoSave method.
388
- *
389
- * If the Persister is not currently set to automatically save, this method
390
- * has no effect.
391
- *
392
- * @returns A reference to the Persister object.
393
- * @example
394
- * This example creates a Store with some data, and saves into the browser's
395
- * session storage. Subsequent changes to the Store are then automatically
396
- * saved to the underlying storage. Once the automatic saving is
397
- * stopped, subsequent changes are not reflected.
398
- *
399
- * ```js
400
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
401
- * const persister = createSessionPersister(store, 'pets');
402
- * await persister.startAutoSave();
403
- *
404
- * store.setTables({pets: {toto: {species: 'dog'}}});
405
- * // ...
406
- * console.log(sessionStorage.getItem('pets'));
407
- * // -> '[{"pets":{"toto":{"species":"dog"}}},{}]'
408
- *
409
- * persister.stopAutoSave();
410
- *
411
- * store.setTables({pets: {felix: {species: 'cat'}}});
412
- * // ...
413
- * console.log(sessionStorage.getItem('pets'));
414
- * // -> '[{"pets":{"toto":{"species":"dog"}}},{}]'
415
- * // Store change has not been automatically saved.
416
- *
417
- * sessionStorage.clear();
418
- * ```
419
- * @category Save
420
- */
421
- stopAutoSave(): Persister<Schemas>;
422
-
423
- /**
424
- * The getStore method returns a reference to the underlying Store that is
425
- * backing this Persister object.
426
- *
427
- * @returns A reference to the Store.
428
- * @example
429
- * This example creates a Persister object against a newly-created Store and
430
- * then gets its reference in order to update its data.
431
- *
432
- * ```js
433
- * const persister = createSessionPersister(createStore(), 'pets');
434
- * await persister.startAutoSave();
435
- *
436
- * persister.getStore().setTables({pets: {fido: {species: 'dog'}}});
437
- * // ...
438
- * console.log(sessionStorage.getItem('pets'));
439
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
440
- *
441
- * sessionStorage.clear();
442
- * ```
443
- * @category Getter
444
- */
445
- getStore(): Store<Schemas>;
446
-
447
- /**
448
- * The destroy method should be called when this Persister object is no longer
449
- * used.
450
- *
451
- * This guarantees that all of the listeners that the object registered with
452
- * the underlying Store and storage are removed and it can be correctly
453
- * garbage collected. It is equivalent to running the stopAutoLoad method and
454
- * the stopAutoSave method in succession.
455
- *
456
- * @example
457
- * This example creates a Store, associates a Persister object with it (that
458
- * registers a TablesListener with the underlying Store), and then destroys it
459
- * again, removing the listener.
460
- *
461
- * ```js
462
- * const store = createStore();
463
- * const persister = createSessionPersister(store, 'pets');
464
- * await persister.startAutoSave();
465
- *
466
- * console.log(store.getListenerStats().tables);
467
- * // -> 1
468
- *
469
- * persister.destroy();
470
- *
471
- * console.log(store.getListenerStats().tables);
472
- * // -> 0
473
- * ```
474
- * @category Lifecycle
475
- */
476
- destroy(): Persister<Schemas>;
477
-
478
- /**
479
- * The getStats method provides a set of statistics about the Persister, and
480
- * is used for debugging purposes.
481
- *
482
- * The PersisterStats object contains a count of the number of times the
483
- * Persister has loaded and saved data.
484
- *
485
- * The statistics are only populated in a debug build: production builds
486
- * return an empty object. The method is intended to be used during
487
- * development to ensure your persistence layer is acting as expected, for
488
- * example.
489
- *
490
- * @returns A PersisterStats object containing Persister load and save
491
- * statistics.
492
- * @example
493
- * This example gets the load and save statistics of a Persister object.
494
- * Remember that the startAutoLoad method invokes an explicit load when it
495
- * starts, and the startAutoSave method invokes an explicit save when it
496
- * starts - so those numbers are included in addition to the loads and saves
497
- * invoked by changes to the Store and to the underlying storage.
498
- *
499
- * ```js
500
- * const store = createStore();
501
- * const persister = createSessionPersister(store, 'pets');
502
- *
503
- * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
504
- * await persister.startAutoSave();
505
- *
506
- * store.setTables({pets: {felix: {species: 'cat'}}});
507
- * // ...
508
- *
509
- * sessionStorage.setItem('pets', '[{"pets":{"toto":{"species":"dog"}}},{}]');
510
- * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
511
- * // ...
512
- *
513
- * console.log(persister.getStats());
514
- * // -> {loads: 2, saves: 2}
515
- *
516
- * persister.destroy();
517
- * sessionStorage.clear();
518
- * ```
519
- * @category Development
520
- */
521
- getStats(): PersisterStats;
522
- }
523
-
524
- /**
525
- * The createSessionPersister function creates a Persister object that can
526
- * persist the Store to the browser's session storage.
527
- *
528
- * As well as providing a reference to the Store to persist, you must provide a
529
- * `storageName` parameter which is unique to your application. This is the key
530
- * that the browser uses to identify the storage location.
531
- *
532
- * @param store The Store to persist.
533
- * @param storageName The unique key to identify the storage location.
534
- * @returns A reference to the new Persister object.
535
- * @example
536
- * This example creates a Persister object and persists the Store to the
537
- * browser's session storage.
538
- *
539
- * ```js
540
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
541
- * const persister = createSessionPersister(store, 'pets');
542
- *
543
- * await persister.save();
544
- * console.log(sessionStorage.getItem('pets'));
545
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
546
- *
547
- * persister.destroy();
548
- * sessionStorage.clear();
549
- * ```
550
- * @category Creation
551
- */
552
- export function createSessionPersister<Schemas extends OptionalSchemas>(
553
- store: Store<Schemas>,
554
- storageName: string,
555
- ): Persister<Schemas>;
556
-
557
- /**
558
- * The createLocalPersister function creates a Persister object that can
559
- * persist the Store to the browser's local storage.
560
- *
561
- * As well as providing a reference to the Store to persist, you must provide a
562
- * `storageName` parameter which is unique to your application. This is the key
563
- * that the browser uses to identify the storage location.
564
- *
565
- * @param store The Store to persist.
566
- * @param storageName The unique key to identify the storage location.
567
- * @returns A reference to the new Persister object.
568
- * @example
569
- * This example creates a Persister object and persists the Store to the
570
- * browser's local storage.
571
- *
572
- * ```js
573
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
574
- * const persister = createLocalPersister(store, 'pets');
575
- *
576
- * await persister.save();
577
- * console.log(localStorage.getItem('pets'));
578
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
579
- *
580
- * persister.destroy();
581
- * localStorage.clear();
582
- * ```
583
- * @category Creation
584
- */
585
- export function createLocalPersister<Schemas extends OptionalSchemas>(
586
- store: Store<Schemas>,
587
- storageName: string,
588
- ): Persister<Schemas>;
589
-
590
- /**
591
- * The createRemotePersister function creates a Persister object that can
592
- * persist the Store to a remote server.
593
- *
594
- * As well as providing a reference to the Store to persist, you must provide
595
- * `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
596
- * server that support the `GET` method (to fetch the Store JSON to load) and
597
- * the `POST` method (to send the Store JSON to save) respectively.
598
- *
599
- * For when you choose to enable automatic loading for the Persister (with the
600
- * startAutoLoad method), it will poll the loadUrl for changes. The
601
- * `autoLoadIntervalSeconds` method is used to indicate how often to do this.
602
- *
603
- * @param store The Store to persist.
604
- * @param loadUrl The endpoint that supports a `GET` method to load JSON.
605
- * @param saveUrl The endpoint that supports a `POST` method to save JSON.
606
- * @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
607
- * automatically loading changes from the server.
608
- * @returns A reference to the new Persister object.
609
- * @example
610
- * This example creates a Persister object and persists the Store to a remote
611
- * server.
612
- *
613
- * ```js yolo
614
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
615
- * const persister = createRemotePersister(
616
- * store,
617
- * 'https://example.com/load',
618
- * 'https://example.com/save',
619
- * 5,
620
- * );
621
- *
622
- * await persister.save();
623
- * // Store JSON will be sent to server in a POST request.
624
- *
625
- * await persister.load();
626
- * // Store JSON will be fetched from server with a GET request.
627
- *
628
- * persister.destroy();
629
- * ```
630
- * @category Creation
631
- */
632
- export function createRemotePersister<Schemas extends OptionalSchemas>(
633
- store: Store<Schemas>,
634
- loadUrl: string,
635
- saveUrl: string,
636
- autoLoadIntervalSeconds: number,
637
- ): Persister<Schemas>;
638
-
639
- /**
640
- * The createFilePersister function creates a Persister object that can persist
641
- * the Store to a local file (in an appropriate environment).
642
- *
643
- * As well as providing a reference to the Store to persist, you must provide
644
- * `filePath` parameter which identifies the file to persist it to.
645
- *
646
- * @param store The Store to persist.
647
- * @param filePath The location of the local file to persist the Store to.
648
- * @returns A reference to the new Persister object.
649
- * @example
650
- * This example creates a Persister object and persists the Store to a local
651
- * file.
652
- *
653
- * ```js yolo
654
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
655
- * const persister = createFilePersister(store, '/app/persisted.json');
656
- *
657
- * await persister.save();
658
- * // Store JSON will be saved to the file.
659
- *
660
- * await persister.load();
661
- * // Store JSON will be loaded from the file.
662
- *
663
- * persister.destroy();
664
- * ```
665
- * @category Creation
666
- */
667
- export function createFilePersister<Schemas extends OptionalSchemas>(
668
- store: Store<Schemas>,
669
- filePath: string,
670
- ): Persister<Schemas>;
671
-
672
- /**
673
- * The createCustomPersister function creates a Persister object that you can
674
- * configure to persist the Store in any way you wish.
675
- *
676
- * As well as providing a reference to the Store to persist, you must provide
677
- * functions that handle how to fetch, write, and listen to, the persistence
678
- * layer.
679
- *
680
- * The other creation functions (such as the createSessionPersister function and
681
- * createFilePersister function, for example) all use this function under the
682
- * covers. See those implementations for ideas on how to implement your own
683
- * Persister types.
684
- *
685
- * @param store The Store to persist.
686
- * @param getPersisted An asynchronous function which will fetch JSON from the
687
- * persistence layer (or `null` or `undefined` if not present).
688
- * @param setPersisted An asynchronous function which will send JSON to the
689
- * persistence layer.
690
- * @param startListeningToPersisted A function that will register a `didChange`
691
- * listener on underlying changes to the persistence layer.
692
- * @param stopListeningToPersisted A function that will unregister the listener
693
- * from the underlying changes to the persistence layer.
694
- * @returns A reference to the new Persister object.
695
- * @example
696
- * This example creates a custom Persister object and persists the Store to a
697
- * local string called `storeJson` and which would automatically load by polling
698
- * for changes every second.
699
- *
700
- * ```js
701
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
702
- * let storeJson;
703
- * let interval;
704
- *
705
- * const persister = createCustomPersister(
706
- * store,
707
- * async () => storeJson,
708
- * async (json) => (storeJson = json),
709
- * (didChange) => (interval = setInterval(didChange, 1000)),
710
- * () => clearInterval(interval),
711
- * );
712
- *
713
- * await persister.save();
714
- * console.log(storeJson);
715
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
716
- *
717
- * storeJson = '[{"pets":{"fido":{"species":"dog","color":"brown"}}},{}]';
718
- * await persister.load();
719
- *
720
- * console.log(store.getTables());
721
- * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
722
- *
723
- * persister.destroy();
724
- * ```
725
- * @category Creation
726
- */
727
- export function createCustomPersister<Schemas extends OptionalSchemas>(
728
- store: Store<Schemas>,
729
- getPersisted: () => Promise<string | null | undefined>,
730
- setPersisted: (json: string) => Promise<void>,
731
- startListeningToPersisted: (didChange: Callback) => void,
732
- stopListeningToPersisted: Callback,
733
- ): Persister<Schemas>;