tinybase 4.0.0-beta.0 → 4.0.0-beta.1
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/lib/cjs/persister-browser.cjs +1 -0
- package/lib/cjs/persister-browser.cjs.gz +0 -0
- package/lib/cjs/persister-file.cjs +1 -0
- package/lib/cjs/persister-file.cjs.gz +0 -0
- package/lib/cjs/persister-remote.cjs +1 -0
- package/lib/cjs/persister-remote.cjs.gz +0 -0
- package/lib/cjs/persister-yjs.cjs +1 -1
- package/lib/cjs/persister-yjs.cjs.gz +0 -0
- package/lib/cjs/persisters.cjs +1 -1
- package/lib/cjs/persisters.cjs.gz +0 -0
- package/lib/cjs/store.cjs +1 -1
- package/lib/cjs/store.cjs.gz +0 -0
- package/lib/cjs/tinybase.cjs +1 -1
- package/lib/cjs/tinybase.cjs.gz +0 -0
- package/lib/cjs-es6/persister-browser.cjs +1 -0
- package/lib/cjs-es6/persister-browser.cjs.gz +0 -0
- package/lib/cjs-es6/persister-file.cjs +1 -0
- package/lib/cjs-es6/persister-file.cjs.gz +0 -0
- package/lib/cjs-es6/persister-remote.cjs +1 -0
- package/lib/cjs-es6/persister-remote.cjs.gz +0 -0
- package/lib/cjs-es6/persister-yjs.cjs +1 -1
- package/lib/cjs-es6/persister-yjs.cjs.gz +0 -0
- package/lib/cjs-es6/persisters.cjs +1 -1
- package/lib/cjs-es6/persisters.cjs.gz +0 -0
- package/lib/cjs-es6/store.cjs +1 -1
- package/lib/cjs-es6/store.cjs.gz +0 -0
- package/lib/cjs-es6/tinybase.cjs +1 -1
- package/lib/cjs-es6/tinybase.cjs.gz +0 -0
- package/lib/debug/persister-browser.js +148 -0
- package/lib/debug/persister-file.js +140 -0
- package/lib/debug/persister-remote.js +156 -0
- package/lib/debug/persister-yjs.js +30 -21
- package/lib/debug/persisters.js +24 -117
- package/lib/debug/store.js +36 -21
- package/lib/debug/tinybase.js +59 -135
- package/lib/es6/persister-browser.js +1 -0
- package/lib/es6/persister-browser.js.gz +0 -0
- package/lib/es6/persister-file.js +1 -0
- package/lib/es6/persister-file.js.gz +0 -0
- package/lib/es6/persister-remote.js +1 -0
- package/lib/es6/persister-remote.js.gz +0 -0
- package/lib/es6/persister-yjs.js +1 -1
- package/lib/es6/persister-yjs.js.gz +0 -0
- package/lib/es6/persisters.js +1 -1
- package/lib/es6/persisters.js.gz +0 -0
- package/lib/es6/store.js +1 -1
- package/lib/es6/store.js.gz +0 -0
- package/lib/es6/tinybase.js +1 -1
- package/lib/es6/tinybase.js.gz +0 -0
- package/lib/persister-browser.js +1 -0
- package/lib/persister-browser.js.gz +0 -0
- package/lib/persister-file.js +1 -0
- package/lib/persister-file.js.gz +0 -0
- package/lib/persister-remote.js +1 -0
- package/lib/persister-remote.js.gz +0 -0
- package/lib/persister-yjs.js +1 -1
- package/lib/persister-yjs.js.gz +0 -0
- package/lib/persisters.js +1 -1
- package/lib/persisters.js.gz +0 -0
- package/lib/store.js +1 -1
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/types/persister-browser.d.ts +85 -0
- package/lib/types/persister-file.d.ts +41 -0
- package/lib/types/persister-remote.d.ts +60 -0
- package/lib/types/persisters.d.ts +40 -173
- package/lib/types/store.d.ts +117 -11
- package/lib/types/with-schemas/persister-browser.d.ts +103 -0
- package/lib/types/with-schemas/persister-file.d.ts +50 -0
- package/lib/types/with-schemas/persister-remote.d.ts +71 -0
- package/lib/types/with-schemas/persisters.d.ts +49 -214
- package/lib/types/with-schemas/store.d.ts +136 -11
- package/lib/umd/persister-browser.js +1 -0
- package/lib/umd/persister-browser.js.gz +0 -0
- package/lib/umd/persister-file.js +1 -0
- package/lib/umd/persister-file.js.gz +0 -0
- package/lib/umd/persister-remote.js +1 -0
- package/lib/umd/persister-remote.js.gz +0 -0
- package/lib/umd/persister-yjs.js +1 -1
- package/lib/umd/persister-yjs.js.gz +0 -0
- package/lib/umd/persisters.js +1 -1
- package/lib/umd/persisters.js.gz +0 -0
- package/lib/umd/store.js +1 -1
- package/lib/umd/store.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/lib/umd-es6/persister-browser.js +1 -0
- package/lib/umd-es6/persister-browser.js.gz +0 -0
- package/lib/umd-es6/persister-file.js +1 -0
- package/lib/umd-es6/persister-file.js.gz +0 -0
- package/lib/umd-es6/persister-remote.js +1 -0
- package/lib/umd-es6/persister-remote.js.gz +0 -0
- package/lib/umd-es6/persister-yjs.js +1 -1
- package/lib/umd-es6/persister-yjs.js.gz +0 -0
- package/lib/umd-es6/persisters.js +1 -1
- package/lib/umd-es6/persisters.js.gz +0 -0
- package/lib/umd-es6/store.js +1 -1
- package/lib/umd-es6/store.js.gz +0 -0
- package/lib/umd-es6/tinybase.js +1 -1
- package/lib/umd-es6/tinybase.js.gz +0 -0
- package/package.json +9 -9
- package/readme.md +2 -2
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The persister-remote module of the TinyBase project lets you save and load
|
|
3
|
+
* Store data to and from a remote server.
|
|
4
|
+
*
|
|
5
|
+
* @see Persisting Data guide
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module persister-remote
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {Persister} from './persisters';
|
|
11
|
+
import {Store} from './store';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The createRemotePersister function creates a Persister object that can
|
|
15
|
+
* persist the Store to a remote server.
|
|
16
|
+
*
|
|
17
|
+
* As well as providing a reference to the Store to persist, you must provide
|
|
18
|
+
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
19
|
+
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
20
|
+
* the `POST` method (to send the Store JSON to save) respectively.
|
|
21
|
+
*
|
|
22
|
+
* For when you choose to enable automatic loading for the Persister (with the
|
|
23
|
+
* startAutoLoad method), it will poll the loadUrl for changes. The
|
|
24
|
+
* `autoLoadIntervalSeconds` method is used to indicate how often to do this.
|
|
25
|
+
*
|
|
26
|
+
* @param store The Store to persist.
|
|
27
|
+
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
28
|
+
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
29
|
+
* @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
|
|
30
|
+
* automatically loading changes from the server.
|
|
31
|
+
* @returns A reference to the new Persister object.
|
|
32
|
+
* @example
|
|
33
|
+
* This example creates a Persister object and persists the Store to a remote
|
|
34
|
+
* server.
|
|
35
|
+
*
|
|
36
|
+
* ```js yolo
|
|
37
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
38
|
+
* const persister = createRemotePersister(
|
|
39
|
+
* store,
|
|
40
|
+
* 'https://example.com/load',
|
|
41
|
+
* 'https://example.com/save',
|
|
42
|
+
* 5,
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* await persister.save();
|
|
46
|
+
* // Store JSON will be sent to server in a POST request.
|
|
47
|
+
*
|
|
48
|
+
* await persister.load();
|
|
49
|
+
* // Store JSON will be fetched from server with a GET request.
|
|
50
|
+
*
|
|
51
|
+
* persister.destroy();
|
|
52
|
+
* ```
|
|
53
|
+
* @category Creation
|
|
54
|
+
*/
|
|
55
|
+
export function createRemotePersister(
|
|
56
|
+
store: Store,
|
|
57
|
+
loadUrl: string,
|
|
58
|
+
saveUrl: string,
|
|
59
|
+
autoLoadIntervalSeconds: number,
|
|
60
|
+
): Persister;
|
|
@@ -3,21 +3,21 @@
|
|
|
3
3
|
* saving and loading Store data, to and from different destinations, or
|
|
4
4
|
* underlying storage types.
|
|
5
5
|
*
|
|
6
|
-
* Several entry points are provided
|
|
7
|
-
* object that can load and save a Store:
|
|
8
|
-
*
|
|
9
|
-
* - The createSessionPersister function
|
|
10
|
-
* browser's session storage.
|
|
11
|
-
* - The createLocalPersister function
|
|
12
|
-
* browser's local storage.
|
|
13
|
-
* - The createRemotePersister function
|
|
14
|
-
* server.
|
|
15
|
-
* - The createFilePersister function
|
|
16
|
-
* (in an appropriate environment).
|
|
6
|
+
* Several entry points are provided (in separately installed modules), each of
|
|
7
|
+
* which returns a new Persister object that can load and save a Store:
|
|
8
|
+
*
|
|
9
|
+
* - The createSessionPersister function (in the persister-browser module)
|
|
10
|
+
* returns a Persister that uses the browser's session storage.
|
|
11
|
+
* - The createLocalPersister function (in the persister-browser module) returns
|
|
12
|
+
* a Persister that uses the browser's local storage.
|
|
13
|
+
* - The createRemotePersister function (in the persister-remote module) returns
|
|
14
|
+
* a Persister that uses a remote server.
|
|
15
|
+
* - The createFilePersister function (in the persister-file module) returns a
|
|
16
|
+
* Persister that uses a local file (in an appropriate environment).
|
|
17
17
|
*
|
|
18
18
|
* Since persistence requirements can be different for every app, the
|
|
19
|
-
* createCustomPersister function can also be used to easily
|
|
20
|
-
* customized way to save and load Store data.
|
|
19
|
+
* createCustomPersister function in this module can also be used to easily
|
|
20
|
+
* create a fully customized way to save and load Store data.
|
|
21
21
|
*
|
|
22
22
|
* @see Persisting Data guide
|
|
23
23
|
* @see Countries demo
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
30
|
import {Store, Tables, Values} from './store.d';
|
|
31
|
-
import {Callback} from './common.d';
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
33
|
* The PersisterStats type describes the number of times a Persister object has
|
|
@@ -50,6 +49,12 @@ export type PersisterStats = {
|
|
|
50
49
|
saves?: number;
|
|
51
50
|
};
|
|
52
51
|
|
|
52
|
+
/**
|
|
53
|
+
* A PersisterListener is a callback that lets a Persister inform the Store that
|
|
54
|
+
* a change has happened to the underlying data.
|
|
55
|
+
*/
|
|
56
|
+
export type PersisterListener = (content?: [Tables, Values]) => void;
|
|
57
|
+
|
|
53
58
|
/**
|
|
54
59
|
* A Persister object lets you save and load Store data to and from different
|
|
55
60
|
* locations, or underlying storage types.
|
|
@@ -461,12 +466,12 @@ export interface Persister {
|
|
|
461
466
|
* const persister = createSessionPersister(store, 'pets');
|
|
462
467
|
* await persister.startAutoSave();
|
|
463
468
|
*
|
|
464
|
-
* console.log(store.getListenerStats().
|
|
469
|
+
* console.log(store.getListenerStats().transaction);
|
|
465
470
|
* // -> 1
|
|
466
471
|
*
|
|
467
472
|
* persister.destroy();
|
|
468
473
|
*
|
|
469
|
-
* console.log(store.getListenerStats().
|
|
474
|
+
* console.log(store.getListenerStats().transaction);
|
|
470
475
|
* // -> 0
|
|
471
476
|
* ```
|
|
472
477
|
* @category Lifecycle
|
|
@@ -520,151 +525,6 @@ export interface Persister {
|
|
|
520
525
|
//
|
|
521
526
|
}
|
|
522
527
|
|
|
523
|
-
/**
|
|
524
|
-
* The createSessionPersister function creates a Persister object that can
|
|
525
|
-
* persist the Store to the browser's session storage.
|
|
526
|
-
*
|
|
527
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
528
|
-
* `storageName` parameter which is unique to your application. This is the key
|
|
529
|
-
* that the browser uses to identify the storage location.
|
|
530
|
-
*
|
|
531
|
-
* @param store The Store to persist.
|
|
532
|
-
* @param storageName The unique key to identify the storage location.
|
|
533
|
-
* @returns A reference to the new Persister object.
|
|
534
|
-
* @example
|
|
535
|
-
* This example creates a Persister object and persists the Store to the
|
|
536
|
-
* browser's session storage.
|
|
537
|
-
*
|
|
538
|
-
* ```js
|
|
539
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
540
|
-
* const persister = createSessionPersister(store, 'pets');
|
|
541
|
-
*
|
|
542
|
-
* await persister.save();
|
|
543
|
-
* console.log(sessionStorage.getItem('pets'));
|
|
544
|
-
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
545
|
-
*
|
|
546
|
-
* persister.destroy();
|
|
547
|
-
* sessionStorage.clear();
|
|
548
|
-
* ```
|
|
549
|
-
* @category Creation
|
|
550
|
-
*/
|
|
551
|
-
export function createSessionPersister(
|
|
552
|
-
store: Store,
|
|
553
|
-
storageName: string,
|
|
554
|
-
): Persister;
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* The createLocalPersister function creates a Persister object that can
|
|
558
|
-
* persist the Store to the browser's local storage.
|
|
559
|
-
*
|
|
560
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
561
|
-
* `storageName` parameter which is unique to your application. This is the key
|
|
562
|
-
* that the browser uses to identify the storage location.
|
|
563
|
-
*
|
|
564
|
-
* @param store The Store to persist.
|
|
565
|
-
* @param storageName The unique key to identify the storage location.
|
|
566
|
-
* @returns A reference to the new Persister object.
|
|
567
|
-
* @example
|
|
568
|
-
* This example creates a Persister object and persists the Store to the
|
|
569
|
-
* browser's local storage.
|
|
570
|
-
*
|
|
571
|
-
* ```js
|
|
572
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
573
|
-
* const persister = createLocalPersister(store, 'pets');
|
|
574
|
-
*
|
|
575
|
-
* await persister.save();
|
|
576
|
-
* console.log(localStorage.getItem('pets'));
|
|
577
|
-
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
578
|
-
*
|
|
579
|
-
* persister.destroy();
|
|
580
|
-
* localStorage.clear();
|
|
581
|
-
* ```
|
|
582
|
-
* @category Creation
|
|
583
|
-
*/
|
|
584
|
-
export function createLocalPersister(
|
|
585
|
-
store: Store,
|
|
586
|
-
storageName: string,
|
|
587
|
-
): Persister;
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* The createRemotePersister function creates a Persister object that can
|
|
591
|
-
* persist the Store to a remote server.
|
|
592
|
-
*
|
|
593
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
594
|
-
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
595
|
-
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
596
|
-
* the `POST` method (to send the Store JSON to save) respectively.
|
|
597
|
-
*
|
|
598
|
-
* For when you choose to enable automatic loading for the Persister (with the
|
|
599
|
-
* startAutoLoad method), it will poll the loadUrl for changes. The
|
|
600
|
-
* `autoLoadIntervalSeconds` method is used to indicate how often to do this.
|
|
601
|
-
*
|
|
602
|
-
* @param store The Store to persist.
|
|
603
|
-
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
604
|
-
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
605
|
-
* @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
|
|
606
|
-
* automatically loading changes from the server.
|
|
607
|
-
* @returns A reference to the new Persister object.
|
|
608
|
-
* @example
|
|
609
|
-
* This example creates a Persister object and persists the Store to a remote
|
|
610
|
-
* server.
|
|
611
|
-
*
|
|
612
|
-
* ```js yolo
|
|
613
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
614
|
-
* const persister = createRemotePersister(
|
|
615
|
-
* store,
|
|
616
|
-
* 'https://example.com/load',
|
|
617
|
-
* 'https://example.com/save',
|
|
618
|
-
* 5,
|
|
619
|
-
* );
|
|
620
|
-
*
|
|
621
|
-
* await persister.save();
|
|
622
|
-
* // Store JSON will be sent to server in a POST request.
|
|
623
|
-
*
|
|
624
|
-
* await persister.load();
|
|
625
|
-
* // Store JSON will be fetched from server with a GET request.
|
|
626
|
-
*
|
|
627
|
-
* persister.destroy();
|
|
628
|
-
* ```
|
|
629
|
-
* @category Creation
|
|
630
|
-
*/
|
|
631
|
-
export function createRemotePersister(
|
|
632
|
-
store: Store,
|
|
633
|
-
loadUrl: string,
|
|
634
|
-
saveUrl: string,
|
|
635
|
-
autoLoadIntervalSeconds: number,
|
|
636
|
-
): Persister;
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
* The createFilePersister function creates a Persister object that can persist
|
|
640
|
-
* the Store to a local file (in an appropriate environment).
|
|
641
|
-
*
|
|
642
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
643
|
-
* `filePath` parameter which identifies the file to persist it to.
|
|
644
|
-
*
|
|
645
|
-
* @param store The Store to persist.
|
|
646
|
-
* @param filePath The location of the local file to persist the Store to.
|
|
647
|
-
* @returns A reference to the new Persister object.
|
|
648
|
-
* @example
|
|
649
|
-
* This example creates a Persister object and persists the Store to a local
|
|
650
|
-
* file.
|
|
651
|
-
*
|
|
652
|
-
* ```js yolo
|
|
653
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
654
|
-
* const persister = createFilePersister(store, '/app/persisted.json');
|
|
655
|
-
*
|
|
656
|
-
* await persister.save();
|
|
657
|
-
* // Store JSON will be saved to the file.
|
|
658
|
-
*
|
|
659
|
-
* await persister.load();
|
|
660
|
-
* // Store JSON will be loaded from the file.
|
|
661
|
-
*
|
|
662
|
-
* persister.destroy();
|
|
663
|
-
* ```
|
|
664
|
-
* @category Creation
|
|
665
|
-
*/
|
|
666
|
-
export function createFilePersister(store: Store, filePath: string): Persister;
|
|
667
|
-
|
|
668
528
|
/**
|
|
669
529
|
* The createCustomPersister function creates a Persister object that you can
|
|
670
530
|
* configure to persist the Store in any way you wish.
|
|
@@ -678,18 +538,25 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
678
538
|
* covers. See those implementations for ideas on how to implement your own
|
|
679
539
|
* Persister types.
|
|
680
540
|
*
|
|
541
|
+
* This API changed in v4.0.0. Any custom persisters created on previous
|
|
542
|
+
* versions should be upgraded. Most notably, the `setPersisted` function
|
|
543
|
+
* parameter is provided with a `getContent` function to get the content from
|
|
544
|
+
* the Store itself, rather than being passed pre-serialized JSON.
|
|
545
|
+
* `addPersisterListener` has been renamed `addPersisterListener`, and
|
|
546
|
+
* `addPersisterListener` has been renamed `delPersisterListener`.
|
|
547
|
+
*
|
|
681
548
|
* @param store The Store to persist.
|
|
682
|
-
* @param getPersisted An asynchronous function which will fetch
|
|
683
|
-
* persistence layer (or `null` or `undefined` if not present).
|
|
684
|
-
* @param setPersisted An asynchronous function which will send
|
|
549
|
+
* @param getPersisted An asynchronous function which will fetch content from
|
|
550
|
+
* the persistence layer (or `null` or `undefined` if not present).
|
|
551
|
+
* @param setPersisted An asynchronous function which will send content to the
|
|
685
552
|
* persistence layer.
|
|
686
|
-
* @param
|
|
553
|
+
* @param addPersisterListener A function that will register a `listener`
|
|
687
554
|
* listener on underlying changes to the persistence layer. You can return a
|
|
688
|
-
* listening handle that will be provided again when `
|
|
555
|
+
* listening handle that will be provided again when `delPersisterListener`
|
|
689
556
|
* is called.
|
|
690
|
-
* @param
|
|
557
|
+
* @param delPersisterListener A function that will unregister the listener
|
|
691
558
|
* from the underlying changes to the persistence layer. It receives whatever
|
|
692
|
-
* was returned from your `
|
|
559
|
+
* was returned from your `addPersisterListener` implementation.
|
|
693
560
|
* @returns A reference to the new Persister object.
|
|
694
561
|
* @example
|
|
695
562
|
* This example creates a custom Persister object and persists the Store to a
|
|
@@ -703,8 +570,8 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
703
570
|
* const persister = createCustomPersister(
|
|
704
571
|
* store,
|
|
705
572
|
* async () => storeJson,
|
|
706
|
-
* async (
|
|
707
|
-
* (
|
|
573
|
+
* async (getContent) => (storeJson = JSON.stringify(getContent())),
|
|
574
|
+
* (listener) => setInterval(listener, 1000),
|
|
708
575
|
* (interval) => clearInterval(interval),
|
|
709
576
|
* );
|
|
710
577
|
*
|
|
@@ -725,7 +592,7 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
725
592
|
export function createCustomPersister<ListeningHandle>(
|
|
726
593
|
store: Store,
|
|
727
594
|
getPersisted: () => Promise<string | null | undefined>,
|
|
728
|
-
setPersisted: (
|
|
729
|
-
|
|
730
|
-
|
|
595
|
+
setPersisted: (getContent: () => [Tables, Values]) => Promise<void>,
|
|
596
|
+
addPersisterListener: (listener: PersisterListener) => ListeningHandle,
|
|
597
|
+
delPersisterListener: (listeningHandle: ListeningHandle) => void,
|
|
731
598
|
): Persister;
|
package/lib/types/store.d.ts
CHANGED
|
@@ -451,17 +451,30 @@ export type DoRollback = (
|
|
|
451
451
|
* of `cellsTouched` and `valuesTouched` in the listener will be `false` because
|
|
452
452
|
* all changes have been reverted.
|
|
453
453
|
*
|
|
454
|
+
* Since v4.0, the listener also receives the list of (valid and invalid) Cell
|
|
455
|
+
* and Value changes made during the transaction.
|
|
456
|
+
*
|
|
454
457
|
* @param store A reference to the Store that is completing a transaction.
|
|
455
458
|
* @param cellsTouched Whether Cell values have been touched during the
|
|
456
459
|
* transaction.
|
|
457
460
|
* @param valuesTouched Whether Values have been touched during the transaction,
|
|
458
461
|
* since v3.0.0.
|
|
462
|
+
* @param changedCells Any Cells that were changed during the transaction, since
|
|
463
|
+
* v4.0.0.
|
|
464
|
+
* @param invalidCells Any invalid attempts to change Cells, since v4.0.0.
|
|
465
|
+
* @param changedValues Any Values that were changed during the transaction,
|
|
466
|
+
* since v4.0.0.
|
|
467
|
+
* @param invalidValues Any invalid attempts to change Values, since v4.0.0.
|
|
459
468
|
* @category Listener
|
|
460
469
|
*/
|
|
461
470
|
export type TransactionListener = (
|
|
462
471
|
store: Store,
|
|
463
472
|
cellsTouched: boolean,
|
|
464
473
|
valuesTouched: boolean,
|
|
474
|
+
changedCells: ChangedCells,
|
|
475
|
+
invalidCells: InvalidCells,
|
|
476
|
+
changedValues: ChangedValues,
|
|
477
|
+
invalidValues: InvalidValues,
|
|
465
478
|
) => void;
|
|
466
479
|
|
|
467
480
|
/**
|
|
@@ -1221,16 +1234,49 @@ export type StoreListenerStats = {
|
|
|
1221
1234
|
export interface Store {
|
|
1222
1235
|
//
|
|
1223
1236
|
/**
|
|
1224
|
-
* The
|
|
1225
|
-
* the Store.
|
|
1237
|
+
* The getContent method returns a Tables object and a Values object in an
|
|
1238
|
+
* array, representing the entire content of the Store.
|
|
1239
|
+
*
|
|
1240
|
+
* Note that this returns a copy of, rather than a reference to the underlying
|
|
1241
|
+
* data, so changes made to the returned objects are not made to the Store
|
|
1242
|
+
* itself.
|
|
1243
|
+
*
|
|
1244
|
+
* @returns An array of a Tables object and a Values object.
|
|
1245
|
+
* @example
|
|
1246
|
+
* This example retrieves the content of a Store.
|
|
1247
|
+
*
|
|
1248
|
+
* ```js
|
|
1249
|
+
* const store = createStore()
|
|
1250
|
+
* .setTables({pets: {fido: {species: 'dog'}}})
|
|
1251
|
+
* .setValues({open: true, employees: 3});
|
|
1252
|
+
* console.log(store.getContent());
|
|
1253
|
+
* // -> [{pets: {fido: {species: 'dog'}}}, {open: true, employees: 3}]
|
|
1254
|
+
* ```
|
|
1255
|
+
* @example
|
|
1256
|
+
* This example retrieves the Tables and Values of an empty Store, returning
|
|
1257
|
+
* empty objects.
|
|
1258
|
+
*
|
|
1259
|
+
* ```js
|
|
1260
|
+
* const store = createStore();
|
|
1261
|
+
* console.log(store.getContent());
|
|
1262
|
+
* // -> [{}, {}]
|
|
1263
|
+
* ```
|
|
1264
|
+
* @category Getter
|
|
1265
|
+
* @since v4.0.0
|
|
1266
|
+
*/
|
|
1267
|
+
getContent(): [Tables, Values];
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* The getTables method returns a Tables object containing the entire tabular
|
|
1271
|
+
* data of the Store.
|
|
1226
1272
|
*
|
|
1227
1273
|
* Note that this returns a copy of, rather than a reference to the underlying
|
|
1228
1274
|
* data, so changes made to the returned object are not made to the Store
|
|
1229
1275
|
* itself.
|
|
1230
1276
|
*
|
|
1231
|
-
* @returns A Tables object containing the
|
|
1277
|
+
* @returns A Tables object containing the tabular data of the Store.
|
|
1232
1278
|
* @example
|
|
1233
|
-
* This example retrieves the data in a Store.
|
|
1279
|
+
* This example retrieves the tabular data in a Store.
|
|
1234
1280
|
*
|
|
1235
1281
|
* ```js
|
|
1236
1282
|
* const store = createStore().setTables({
|
|
@@ -1569,9 +1615,9 @@ export interface Store {
|
|
|
1569
1615
|
* data, so changes made to the returned object are not made to the Store
|
|
1570
1616
|
* itself.
|
|
1571
1617
|
*
|
|
1572
|
-
* @returns An object containing the
|
|
1618
|
+
* @returns An object containing the set of keyed Values in the Store.
|
|
1573
1619
|
* @example
|
|
1574
|
-
* This example retrieves the keyed Values
|
|
1620
|
+
* This example retrieves the set of keyed Values in the Store.
|
|
1575
1621
|
*
|
|
1576
1622
|
* ```js
|
|
1577
1623
|
* const store = createStore().setValues({open: true, employees: 3});
|
|
@@ -1579,7 +1625,7 @@ export interface Store {
|
|
|
1579
1625
|
* // -> {open: true, employees: 3}
|
|
1580
1626
|
* ```
|
|
1581
1627
|
* @example
|
|
1582
|
-
* This example retrieves
|
|
1628
|
+
* This example retrieves Values from a Store that has none, returning an
|
|
1583
1629
|
* empty object.
|
|
1584
1630
|
*
|
|
1585
1631
|
* ```js
|
|
@@ -1964,7 +2010,67 @@ export interface Store {
|
|
|
1964
2010
|
getSchemaJson(): Json;
|
|
1965
2011
|
|
|
1966
2012
|
/**
|
|
1967
|
-
* The
|
|
2013
|
+
* The setContent method takes an object and sets the entire data of the
|
|
2014
|
+
* Store.
|
|
2015
|
+
*
|
|
2016
|
+
* This method will cause listeners to be called for any Table, Row, Cell,
|
|
2017
|
+
* Value, or Id changes resulting from it.
|
|
2018
|
+
*
|
|
2019
|
+
* Any part of the provided objects that are invalid (either according to the
|
|
2020
|
+
* Tables or Values type, or because it does not match a TablesSchema or
|
|
2021
|
+
* ValuesSchema associated with the Store), will be ignored silently.
|
|
2022
|
+
*
|
|
2023
|
+
* Assuming that at least some of the provided Tables object or Values object
|
|
2024
|
+
* is valid, any data that was already present in that part of the Store will
|
|
2025
|
+
* be completely overwritten. If either object is completely invalid, no
|
|
2026
|
+
* change will be made to the corresponding part of the Store.
|
|
2027
|
+
*
|
|
2028
|
+
* The method returns a reference to the Store so that subsequent operations
|
|
2029
|
+
* can be chained in a fluent style.
|
|
2030
|
+
*
|
|
2031
|
+
* @param content An array containing the tabular and keyed-value data of the
|
|
2032
|
+
* Store to be set.
|
|
2033
|
+
* @example
|
|
2034
|
+
* This example sets the data of a Store.
|
|
2035
|
+
*
|
|
2036
|
+
* ```js
|
|
2037
|
+
* const store = createStore().setContent([
|
|
2038
|
+
* {pets: {fido: {species: 'dog'}}},
|
|
2039
|
+
* {open: true, employees: 3},
|
|
2040
|
+
* ]);
|
|
2041
|
+
* console.log(store.getTables());
|
|
2042
|
+
* // -> {pets: {fido: {species: 'dog'}}}
|
|
2043
|
+
* console.log(store.getValues());
|
|
2044
|
+
* // -> {open: true, employees: 3}
|
|
2045
|
+
* ```
|
|
2046
|
+
* @example
|
|
2047
|
+
* This example attempts to set the data of an existing Store with partly
|
|
2048
|
+
* invalid, and then completely invalid objects.
|
|
2049
|
+
*
|
|
2050
|
+
* ```js
|
|
2051
|
+
* const store = createStore().setContent([
|
|
2052
|
+
* {pets: {fido: {species: 'dog'}}},
|
|
2053
|
+
* {open: true, employees: 3},
|
|
2054
|
+
* ]);
|
|
2055
|
+
*
|
|
2056
|
+
* store.setContent([{pets: {felix: {species: 'cat', bug: []}}}, '']);
|
|
2057
|
+
* console.log(store.getTables());
|
|
2058
|
+
* // -> {pets: {felix: {species: 'cat'}}}
|
|
2059
|
+
* console.log(store.getValues());
|
|
2060
|
+
* // -> {open: true, employees: 3}
|
|
2061
|
+
*
|
|
2062
|
+
* store.setContent([{meaning: 42}]);
|
|
2063
|
+
* console.log(store.getTables());
|
|
2064
|
+
* // -> {pets: {felix: {species: 'cat'}}}
|
|
2065
|
+
* ```
|
|
2066
|
+
* @category Setter
|
|
2067
|
+
* @since 4.0.0
|
|
2068
|
+
*/
|
|
2069
|
+
setContent([tables, values]: [Tables, Values]): Store;
|
|
2070
|
+
|
|
2071
|
+
/**
|
|
2072
|
+
* The setTables method takes an object and sets the entire tabular data of
|
|
2073
|
+
* the Store.
|
|
1968
2074
|
*
|
|
1969
2075
|
* This method will cause listeners to be called for any Table, Row, Cell, or
|
|
1970
2076
|
* Id changes resulting from it.
|
|
@@ -1982,7 +2088,7 @@ export interface Store {
|
|
|
1982
2088
|
*
|
|
1983
2089
|
* @param tables The data of the Store to be set.
|
|
1984
2090
|
* @example
|
|
1985
|
-
* This example sets the data of a Store.
|
|
2091
|
+
* This example sets the tabular data of a Store.
|
|
1986
2092
|
*
|
|
1987
2093
|
* ```js
|
|
1988
2094
|
* const store = createStore().setTables({
|
|
@@ -1993,8 +2099,8 @@ export interface Store {
|
|
|
1993
2099
|
* // -> {pets: {fido: {species: 'dog'}}, species: {dog: {price: 5}}}
|
|
1994
2100
|
* ```
|
|
1995
2101
|
* @example
|
|
1996
|
-
* This example attempts to set the data of an existing Store with
|
|
1997
|
-
* invalid, and then completely invalid, Tables objects.
|
|
2102
|
+
* This example attempts to set the tabular data of an existing Store with
|
|
2103
|
+
* partly invalid, and then completely invalid, Tables objects.
|
|
1998
2104
|
*
|
|
1999
2105
|
* ```js
|
|
2000
2106
|
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The persister-browser module of the TinyBase project lets you save and load
|
|
3
|
+
* Store data to and from browser storage.
|
|
4
|
+
*
|
|
5
|
+
* Two entry points are provided, each of which returns a new Persister object
|
|
6
|
+
* that can load and save a Store:
|
|
7
|
+
*
|
|
8
|
+
* - The createSessionPersister function returns a Persister that uses the
|
|
9
|
+
* browser's session storage.
|
|
10
|
+
* - The createLocalPersister function returns a Persister that uses the
|
|
11
|
+
* browser's local storage.
|
|
12
|
+
*
|
|
13
|
+
* @see Persisting Data guide
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
* @module persister-browser
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {OptionalSchemas, Store} from './store';
|
|
19
|
+
import {Persister} from './persisters';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The createSessionPersister function creates a Persister object that can
|
|
23
|
+
* persist the Store to the browser's session storage.
|
|
24
|
+
*
|
|
25
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
26
|
+
*
|
|
27
|
+
* ```ts override
|
|
28
|
+
* createSessionPersister(
|
|
29
|
+
* store: Store,
|
|
30
|
+
* storageName: string,
|
|
31
|
+
* ): Persister;
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* As well as providing a reference to the Store to persist, you must provide a
|
|
35
|
+
* `storageName` parameter which is unique to your application. This is the key
|
|
36
|
+
* that the browser uses to identify the storage location.
|
|
37
|
+
*
|
|
38
|
+
* @param store The Store to persist.
|
|
39
|
+
* @param storageName The unique key to identify the storage location.
|
|
40
|
+
* @returns A reference to the new Persister object.
|
|
41
|
+
* @example
|
|
42
|
+
* This example creates a Persister object and persists the Store to the
|
|
43
|
+
* browser's session storage.
|
|
44
|
+
*
|
|
45
|
+
* ```js
|
|
46
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
47
|
+
* const persister = createSessionPersister(store, 'pets');
|
|
48
|
+
*
|
|
49
|
+
* await persister.save();
|
|
50
|
+
* console.log(sessionStorage.getItem('pets'));
|
|
51
|
+
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
52
|
+
*
|
|
53
|
+
* persister.destroy();
|
|
54
|
+
* sessionStorage.clear();
|
|
55
|
+
* ```
|
|
56
|
+
* @category Creation
|
|
57
|
+
*/
|
|
58
|
+
export function createSessionPersister<Schemas extends OptionalSchemas>(
|
|
59
|
+
store: Store<Schemas>,
|
|
60
|
+
storageName: string,
|
|
61
|
+
): Persister<Schemas>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The createLocalPersister function creates a Persister object that can
|
|
65
|
+
* persist the Store to the browser's local storage.
|
|
66
|
+
*
|
|
67
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
68
|
+
*
|
|
69
|
+
* ```ts override
|
|
70
|
+
* createLocalPersister(
|
|
71
|
+
* store: Store,
|
|
72
|
+
* storageName: string,
|
|
73
|
+
* ): Persister;
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* As well as providing a reference to the Store to persist, you must provide a
|
|
77
|
+
* `storageName` parameter which is unique to your application. This is the key
|
|
78
|
+
* that the browser uses to identify the storage location.
|
|
79
|
+
*
|
|
80
|
+
* @param store The Store to persist.
|
|
81
|
+
* @param storageName The unique key to identify the storage location.
|
|
82
|
+
* @returns A reference to the new Persister object.
|
|
83
|
+
* @example
|
|
84
|
+
* This example creates a Persister object and persists the Store to the
|
|
85
|
+
* browser's local storage.
|
|
86
|
+
*
|
|
87
|
+
* ```js
|
|
88
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
89
|
+
* const persister = createLocalPersister(store, 'pets');
|
|
90
|
+
*
|
|
91
|
+
* await persister.save();
|
|
92
|
+
* console.log(localStorage.getItem('pets'));
|
|
93
|
+
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
94
|
+
*
|
|
95
|
+
* persister.destroy();
|
|
96
|
+
* localStorage.clear();
|
|
97
|
+
* ```
|
|
98
|
+
* @category Creation
|
|
99
|
+
*/
|
|
100
|
+
export function createLocalPersister<Schemas extends OptionalSchemas>(
|
|
101
|
+
store: Store<Schemas>,
|
|
102
|
+
storageName: string,
|
|
103
|
+
): Persister<Schemas>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The persister-file module of the TinyBase project lets you save and load
|
|
3
|
+
* Store data to and from a local file system (in an appropriate environment).
|
|
4
|
+
*
|
|
5
|
+
* @see Persisting Data guide
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module persister-file
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {OptionalSchemas, Store} from './store';
|
|
11
|
+
import {Persister} from './persisters';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The createFilePersister function creates a Persister object that can persist
|
|
15
|
+
* the Store to a local file (in an appropriate environment).
|
|
16
|
+
*
|
|
17
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
18
|
+
*
|
|
19
|
+
* ```ts override
|
|
20
|
+
* createFilePersister(store: Store, filePath: string): Persister;
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* As well as providing a reference to the Store to persist, you must provide
|
|
24
|
+
* `filePath` parameter which identifies the file to persist it to.
|
|
25
|
+
*
|
|
26
|
+
* @param store The Store to persist.
|
|
27
|
+
* @param filePath The location of the local file to persist the Store to.
|
|
28
|
+
* @returns A reference to the new Persister object.
|
|
29
|
+
* @example
|
|
30
|
+
* This example creates a Persister object and persists the Store to a local
|
|
31
|
+
* file.
|
|
32
|
+
*
|
|
33
|
+
* ```js yolo
|
|
34
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
35
|
+
* const persister = createFilePersister(store, '/app/persisted.json');
|
|
36
|
+
*
|
|
37
|
+
* await persister.save();
|
|
38
|
+
* // Store JSON will be saved to the file.
|
|
39
|
+
*
|
|
40
|
+
* await persister.load();
|
|
41
|
+
* // Store JSON will be loaded from the file.
|
|
42
|
+
*
|
|
43
|
+
* persister.destroy();
|
|
44
|
+
* ```
|
|
45
|
+
* @category Creation
|
|
46
|
+
*/
|
|
47
|
+
export function createFilePersister<Schemas extends OptionalSchemas>(
|
|
48
|
+
store: Store<Schemas>,
|
|
49
|
+
filePath: string,
|
|
50
|
+
): Persister<Schemas>;
|