tinybase 4.6.0-beta.0 → 4.6.0-beta.2
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/checkpoints.js +1 -1
- package/lib/checkpoints.js.gz +0 -0
- package/lib/cjs/checkpoints.cjs +1 -1
- package/lib/cjs/checkpoints.cjs.gz +0 -0
- package/lib/cjs/persisters/persister-electric-sql.cjs +1 -0
- package/lib/cjs/persisters/persister-electric-sql.cjs.gz +0 -0
- package/lib/cjs/persisters/persister-partykit-server.cjs +1 -1
- package/lib/cjs/persisters/persister-partykit-server.cjs.gz +0 -0
- package/lib/cjs/tinybase.cjs +1 -1
- package/lib/cjs/tinybase.cjs.gz +0 -0
- package/lib/cjs/ui-react.cjs +1 -1
- package/lib/cjs/ui-react.cjs.gz +0 -0
- package/lib/cjs-es6/checkpoints.cjs +1 -1
- package/lib/cjs-es6/checkpoints.cjs.gz +0 -0
- package/lib/cjs-es6/persisters/persister-electric-sql.cjs +1 -0
- package/lib/cjs-es6/persisters/persister-electric-sql.cjs.gz +0 -0
- package/lib/cjs-es6/persisters/persister-partykit-server.cjs +1 -1
- package/lib/cjs-es6/persisters/persister-partykit-server.cjs.gz +0 -0
- package/lib/cjs-es6/tinybase.cjs +1 -1
- package/lib/cjs-es6/tinybase.cjs.gz +0 -0
- package/lib/cjs-es6/ui-react.cjs +1 -1
- package/lib/cjs-es6/ui-react.cjs.gz +0 -0
- package/lib/debug/checkpoints.js +8 -0
- package/lib/debug/persisters/persister-electric-sql.js +839 -0
- package/lib/debug/persisters/persister-partykit-server.js +45 -25
- package/lib/debug/tinybase.js +8 -0
- package/lib/debug/ui-react.js +20 -2
- package/lib/es6/checkpoints.js +1 -1
- package/lib/es6/checkpoints.js.gz +0 -0
- package/lib/es6/persisters/persister-electric-sql.js +1 -0
- package/lib/es6/persisters/persister-electric-sql.js.gz +0 -0
- package/lib/es6/persisters/persister-partykit-server.js +1 -1
- package/lib/es6/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/es6/tinybase.js +1 -1
- package/lib/es6/tinybase.js.gz +0 -0
- package/lib/es6/ui-react.js +1 -1
- package/lib/es6/ui-react.js.gz +0 -0
- package/lib/persisters/persister-electric-sql.js +1 -0
- package/lib/persisters/persister-electric-sql.js.gz +0 -0
- package/lib/persisters/persister-partykit-server.js +1 -1
- package/lib/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/types/checkpoints.d.ts +58 -6
- package/lib/types/persisters/persister-automerge.d.ts +1 -1
- package/lib/types/persisters/persister-browser.d.ts +2 -2
- package/lib/types/persisters/persister-cr-sqlite-wasm.d.ts +1 -1
- package/lib/types/persisters/persister-electric-sql.d.ts +164 -0
- package/lib/types/persisters/persister-expo-sqlite-next.d.ts +3 -3
- package/lib/types/persisters/persister-expo-sqlite.d.ts +1 -1
- package/lib/types/persisters/persister-file.d.ts +1 -1
- package/lib/types/persisters/persister-indexed-db.d.ts +1 -1
- package/lib/types/persisters/persister-partykit-client.d.ts +1 -1
- package/lib/types/persisters/persister-partykit-server.d.ts +32 -8
- package/lib/types/persisters/persister-remote.d.ts +1 -1
- package/lib/types/persisters/persister-sqlite-wasm.d.ts +1 -1
- package/lib/types/persisters/persister-sqlite3.d.ts +1 -1
- package/lib/types/persisters/persister-yjs.d.ts +1 -1
- package/lib/types/persisters.d.ts +2 -0
- package/lib/types/ui-react.d.ts +56 -26
- package/lib/types/with-schemas/checkpoints.d.ts +64 -6
- package/lib/types/with-schemas/internal/ui-react.d.ts +5 -0
- package/lib/types/with-schemas/persisters/persister-electric-sql.d.ts +177 -0
- package/lib/types/with-schemas/persisters/persister-expo-sqlite-next.d.ts +2 -2
- package/lib/types/with-schemas/persisters/persister-partykit-server.d.ts +44 -10
- package/lib/types/with-schemas/persisters/persister-sqlite3.d.ts +1 -1
- package/lib/types/with-schemas/persisters.d.ts +2 -0
- package/lib/types/with-schemas/ui-react.d.ts +56 -36
- package/lib/ui-react.js +1 -1
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -1
- package/lib/umd/checkpoints.js.gz +0 -0
- package/lib/umd/persisters/persister-electric-sql.js +1 -0
- package/lib/umd/persisters/persister-electric-sql.js.gz +0 -0
- package/lib/umd/persisters/persister-partykit-server.js +1 -1
- package/lib/umd/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/lib/umd/ui-react.js +1 -1
- package/lib/umd/ui-react.js.gz +0 -0
- package/lib/umd-es6/checkpoints.js +1 -1
- package/lib/umd-es6/checkpoints.js.gz +0 -0
- package/lib/umd-es6/persisters/persister-electric-sql.js +1 -0
- package/lib/umd-es6/persisters/persister-electric-sql.js.gz +0 -0
- package/lib/umd-es6/persisters/persister-partykit-server.js +1 -1
- package/lib/umd-es6/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/umd-es6/tinybase.js +1 -1
- package/lib/umd-es6/tinybase.js.gz +0 -0
- package/lib/umd-es6/ui-react.js +1 -1
- package/lib/umd-es6/ui-react.js.gz +0 -0
- package/package.json +48 -43
- package/readme.md +2 -2
package/lib/types/ui-react.d.ts
CHANGED
|
@@ -204,6 +204,17 @@ export type CheckpointsOrCheckpointsId = Checkpoints | Id;
|
|
|
204
204
|
*/
|
|
205
205
|
export type UndoOrRedoInformation = [boolean, Callback, Id | undefined, string];
|
|
206
206
|
|
|
207
|
+
/**
|
|
208
|
+
* The GetId type describes a function which, when passed a parameter, will
|
|
209
|
+
* return an Id.
|
|
210
|
+
*
|
|
211
|
+
* This type is used in hooks that create callbacks - like the
|
|
212
|
+
* useSetTableCallback hook or useSetRowCallback hook - so that the Id arguments
|
|
213
|
+
* of the object to set can also be dependent on the event or parameter
|
|
214
|
+
* provided (as well as the object itself being set).
|
|
215
|
+
*/
|
|
216
|
+
export type GetId<Parameter> = (parameter: Parameter, store: Store) => Id;
|
|
217
|
+
|
|
207
218
|
/**
|
|
208
219
|
* The useCreateStore hook is used to create a Store within a React application
|
|
209
220
|
* with convenient memoization.
|
|
@@ -2279,13 +2290,15 @@ export function useSetTablesCallback<Parameter>(
|
|
|
2279
2290
|
* The Store to which the callback will make the mutation (indicated by the
|
|
2280
2291
|
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
|
|
2281
2292
|
* dependency for the callback.
|
|
2282
|
-
* @param tableId The Id of the Table in the Store to set
|
|
2293
|
+
* @param tableId The Id of the Table in the Store to set, or a GetId function
|
|
2294
|
+
* that will return it.
|
|
2283
2295
|
* @param getTable A function which returns the Table object that will be used
|
|
2284
2296
|
* to update the Store, based on the parameter the callback will receive (and
|
|
2285
2297
|
* which is most likely a DOM event).
|
|
2286
2298
|
* @param getTableDeps An optional array of dependencies for the `getTable`
|
|
2287
2299
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2288
|
-
* This parameter defaults to an empty array.
|
|
2300
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2301
|
+
* dependencies of a GetId functions if used as the tableId argument.
|
|
2289
2302
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2290
2303
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2291
2304
|
* reference.
|
|
@@ -2332,7 +2345,7 @@ export function useSetTablesCallback<Parameter>(
|
|
|
2332
2345
|
* @category Store hooks
|
|
2333
2346
|
*/
|
|
2334
2347
|
export function useSetTableCallback<Parameter>(
|
|
2335
|
-
tableId: Id
|
|
2348
|
+
tableId: Id | GetId<Parameter>,
|
|
2336
2349
|
getTable: (parameter: Parameter, store: Store) => Table,
|
|
2337
2350
|
getTableDeps?: React.DependencyList,
|
|
2338
2351
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -2364,14 +2377,18 @@ export function useSetTableCallback<Parameter>(
|
|
|
2364
2377
|
* The Store to which the callback will make the mutation (indicated by the
|
|
2365
2378
|
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
|
|
2366
2379
|
* dependency for the callback.
|
|
2367
|
-
* @param tableId The Id of the Table in the Store
|
|
2368
|
-
*
|
|
2380
|
+
* @param tableId The Id of the Table in the Store, or a GetId function that
|
|
2381
|
+
* will return it.
|
|
2382
|
+
* @param rowId The Id of the Row in the Table to set, or a GetId function that
|
|
2383
|
+
* will return it.
|
|
2369
2384
|
* @param getRow A function which returns the Row object that will be used to
|
|
2370
2385
|
* update the Store, based on the parameter the callback will receive (and which
|
|
2371
2386
|
* is most likely a DOM event).
|
|
2372
2387
|
* @param getRowDeps An optional array of dependencies for the `getRow`
|
|
2373
2388
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2374
|
-
* This parameter defaults to an empty array.
|
|
2389
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2390
|
+
* dependencies of any GetId functions if used as the tableId or rowId
|
|
2391
|
+
* arguments.
|
|
2375
2392
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2376
2393
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2377
2394
|
* reference.
|
|
@@ -2419,8 +2436,8 @@ export function useSetTableCallback<Parameter>(
|
|
|
2419
2436
|
* @category Store hooks
|
|
2420
2437
|
*/
|
|
2421
2438
|
export function useSetRowCallback<Parameter>(
|
|
2422
|
-
tableId: Id
|
|
2423
|
-
rowId: Id
|
|
2439
|
+
tableId: Id | GetId<Parameter>,
|
|
2440
|
+
rowId: Id | GetId<Parameter>,
|
|
2424
2441
|
getRow: (parameter: Parameter, store: Store) => Row,
|
|
2425
2442
|
getRowDeps?: React.DependencyList,
|
|
2426
2443
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -2459,13 +2476,15 @@ export function useSetRowCallback<Parameter>(
|
|
|
2459
2476
|
* specify `reuseRowIds` to be `false`, then the Id will be a monotonically
|
|
2460
2477
|
* increasing string representation of an increasing integer, regardless of any
|
|
2461
2478
|
* you may have previously deleted.
|
|
2462
|
-
* @param tableId The Id of the Table in the Store
|
|
2479
|
+
* @param tableId The Id of the Table in the Store, or a GetId function
|
|
2480
|
+
* that will return it.
|
|
2463
2481
|
* @param getRow A function which returns the Row object that will be used to
|
|
2464
2482
|
* update the Store, based on the parameter the callback will receive (and which
|
|
2465
2483
|
* is most likely a DOM event).
|
|
2466
2484
|
* @param getRowDeps An optional array of dependencies for the `getRow`
|
|
2467
2485
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2468
|
-
* This parameter defaults to an empty array.
|
|
2486
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2487
|
+
* dependencies of a GetId functions if used as the tableId argument.
|
|
2469
2488
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2470
2489
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2471
2490
|
* reference.
|
|
@@ -2514,7 +2533,7 @@ export function useSetRowCallback<Parameter>(
|
|
|
2514
2533
|
* @category Store hooks
|
|
2515
2534
|
*/
|
|
2516
2535
|
export function useAddRowCallback<Parameter>(
|
|
2517
|
-
tableId: Id
|
|
2536
|
+
tableId: Id | GetId<Parameter>,
|
|
2518
2537
|
getRow: (parameter: Parameter, store: Store) => Row,
|
|
2519
2538
|
getRowDeps?: React.DependencyList,
|
|
2520
2539
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -2548,14 +2567,18 @@ export function useAddRowCallback<Parameter>(
|
|
|
2548
2567
|
* The Store to which the callback will make the mutation (indicated by the
|
|
2549
2568
|
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
|
|
2550
2569
|
* dependency for the callback.
|
|
2551
|
-
* @param tableId The Id of the Table in the Store
|
|
2552
|
-
*
|
|
2570
|
+
* @param tableId The Id of the Table in the Store, or a GetId function that
|
|
2571
|
+
* will return it.
|
|
2572
|
+
* @param rowId The Id of the Row in the Table to set, or a GetId function that
|
|
2573
|
+
* will return it.
|
|
2553
2574
|
* @param getPartialRow A function which returns the partial Row object that
|
|
2554
2575
|
* will be used to update the Store, based on the parameter the callback will
|
|
2555
2576
|
* receive (and which is most likely a DOM event).
|
|
2556
2577
|
* @param getPartialRowDeps An optional array of dependencies for the `getRow`
|
|
2557
2578
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2558
|
-
* This parameter defaults to an empty array.
|
|
2579
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2580
|
+
* dependencies of any GetId functions if used as the tableId, rowId, or cellId
|
|
2581
|
+
* arguments.
|
|
2559
2582
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2560
2583
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2561
2584
|
* reference.
|
|
@@ -2604,8 +2627,8 @@ export function useAddRowCallback<Parameter>(
|
|
|
2604
2627
|
* @category Store hooks
|
|
2605
2628
|
*/
|
|
2606
2629
|
export function useSetPartialRowCallback<Parameter>(
|
|
2607
|
-
tableId: Id
|
|
2608
|
-
rowId: Id
|
|
2630
|
+
tableId: Id | GetId<Parameter>,
|
|
2631
|
+
rowId: Id | GetId<Parameter>,
|
|
2609
2632
|
getPartialRow: (parameter: Parameter, store: Store) => Row,
|
|
2610
2633
|
getPartialRowDeps?: React.DependencyList,
|
|
2611
2634
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -2637,15 +2660,20 @@ export function useSetPartialRowCallback<Parameter>(
|
|
|
2637
2660
|
* The Store to which the callback will make the mutation (indicated by the
|
|
2638
2661
|
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
|
|
2639
2662
|
* dependency for the callback.
|
|
2640
|
-
* @param tableId The Id of the Table in the Store
|
|
2641
|
-
*
|
|
2642
|
-
* @param
|
|
2663
|
+
* @param tableId The Id of the Table in the Store, or a GetId function that
|
|
2664
|
+
* will return it.
|
|
2665
|
+
* @param rowId The Id of the Row in the Table, or a GetId function that will
|
|
2666
|
+
* return it.
|
|
2667
|
+
* @param cellId The Id of the Cell in the Row to set, or a GetId function that
|
|
2668
|
+
* will return it.
|
|
2643
2669
|
* @param getCell A function which returns the Cell value that will be used to
|
|
2644
2670
|
* update the Store, or a MapCell function to update it, based on the parameter
|
|
2645
2671
|
* the callback will receive (and which is most likely a DOM event).
|
|
2646
2672
|
* @param getCellDeps An optional array of dependencies for the `getCell`
|
|
2647
2673
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2648
|
-
* This parameter defaults to an empty array.
|
|
2674
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2675
|
+
* dependencies of any GetId functions if used as the tableId, rowId, or cellId
|
|
2676
|
+
* arguments.
|
|
2649
2677
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2650
2678
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2651
2679
|
* reference.
|
|
@@ -2731,9 +2759,9 @@ export function useSetPartialRowCallback<Parameter>(
|
|
|
2731
2759
|
* @category Store hooks
|
|
2732
2760
|
*/
|
|
2733
2761
|
export function useSetCellCallback<Parameter>(
|
|
2734
|
-
tableId: Id
|
|
2735
|
-
rowId: Id
|
|
2736
|
-
cellId: Id
|
|
2762
|
+
tableId: Id | GetId<Parameter>,
|
|
2763
|
+
rowId: Id | GetId<Parameter>,
|
|
2764
|
+
cellId: Id | GetId<Parameter>,
|
|
2737
2765
|
getCell: (parameter: Parameter, store: Store) => Cell | MapCell,
|
|
2738
2766
|
getCellDeps?: React.DependencyList,
|
|
2739
2767
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -2933,13 +2961,15 @@ export function useSetPartialValuesCallback<Parameter>(
|
|
|
2933
2961
|
* The Store to which the callback will make the mutation (indicated by the
|
|
2934
2962
|
* hook's `storeOrStoreId` parameter) is always automatically used as a hook
|
|
2935
2963
|
* dependency for the callback.
|
|
2936
|
-
* @param valueId The Id of the Value in the Store to set
|
|
2964
|
+
* @param valueId The Id of the Value in the Store to set, or a GetId function
|
|
2965
|
+
* that will return it.
|
|
2937
2966
|
* @param getValue A function which returns the Value object that will be used
|
|
2938
2967
|
* to update the Store, based on the parameter the callback will receive (and
|
|
2939
2968
|
* which is most likely a DOM event).
|
|
2940
2969
|
* @param getValueDeps An optional array of dependencies for the `getValue`
|
|
2941
2970
|
* function, which, if any change, result in the regeneration of the callback.
|
|
2942
|
-
* This parameter defaults to an empty array.
|
|
2971
|
+
* This parameter defaults to an empty array. Also use this to indicate the
|
|
2972
|
+
* dependencies of a GetId function if used as the valueId argument.
|
|
2943
2973
|
* @param storeOrStoreId The Store to be updated: omit for the default context
|
|
2944
2974
|
* Store, provide an Id for a named context Store, or provide an explicit
|
|
2945
2975
|
* reference.
|
|
@@ -2987,7 +3017,7 @@ export function useSetPartialValuesCallback<Parameter>(
|
|
|
2987
3017
|
* @since v3.0.0
|
|
2988
3018
|
*/
|
|
2989
3019
|
export function useSetValueCallback<Parameter>(
|
|
2990
|
-
valueId: Id
|
|
3020
|
+
valueId: Id | GetId<Parameter>,
|
|
2991
3021
|
getValue: (parameter: Parameter, store: Store) => Value | MapValue,
|
|
2992
3022
|
getValueDeps?: React.DependencyList,
|
|
2993
3023
|
storeOrStoreId?: StoreOrStoreId,
|
|
@@ -864,16 +864,14 @@ export interface Checkpoints<in out Schemas extends OptionalSchemas> {
|
|
|
864
864
|
* ```
|
|
865
865
|
*
|
|
866
866
|
* Obviously this method should be used with caution as it destroys the
|
|
867
|
-
* ability to undo recent changes to the Store (though of course the
|
|
868
|
-
* itself is not reset by this method).
|
|
867
|
+
* ability to undo or redo recent changes to the Store (though of course the
|
|
868
|
+
* Store itself is not reset by this method).
|
|
869
869
|
*
|
|
870
870
|
* This method can be useful when a Store is being loaded via a Persister
|
|
871
871
|
* asynchronously after the Checkpoints object has been attached, and you
|
|
872
872
|
* don't want users to be able to undo the initial load of the data. In this
|
|
873
|
-
* you could call the clear method immediately after the initial load so
|
|
874
|
-
* that is the baseline from which all subsequent changes are tracked.
|
|
875
|
-
*
|
|
876
|
-
* If you are listening to
|
|
873
|
+
* case you could call the clear method immediately after the initial load so
|
|
874
|
+
* that that is the baseline from which all subsequent changes are tracked.
|
|
877
875
|
* @returns A reference to the Checkpoints object.
|
|
878
876
|
* @example
|
|
879
877
|
* This example creates a Store, a Checkpoints object, adds a listener, makes
|
|
@@ -916,6 +914,66 @@ export interface Checkpoints<in out Schemas extends OptionalSchemas> {
|
|
|
916
914
|
*/
|
|
917
915
|
clear(): Checkpoints<Schemas>;
|
|
918
916
|
|
|
917
|
+
/**
|
|
918
|
+
* The clearForward method resets just the 'redo' checkpoints it has been
|
|
919
|
+
* managing.
|
|
920
|
+
*
|
|
921
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
922
|
+
*
|
|
923
|
+
* ```ts override
|
|
924
|
+
* clearForward(): Checkpoints;
|
|
925
|
+
* ```
|
|
926
|
+
*
|
|
927
|
+
* Obviously this method should be used with caution as it destroys the
|
|
928
|
+
* ability to redo recent changes to the Store (though of course the Store
|
|
929
|
+
* itself is not reset by this method).
|
|
930
|
+
*
|
|
931
|
+
* This method can be useful when you want to prohibit a user from redoing
|
|
932
|
+
* changes they have undone. The 'backward' redo stack, and current checkpoint
|
|
933
|
+
* are not affected.
|
|
934
|
+
* @returns A reference to the Checkpoints object.
|
|
935
|
+
* @example
|
|
936
|
+
* This example creates a Store, a Checkpoints object, adds a listener, makes
|
|
937
|
+
* a change and then clears the forward checkpoints.
|
|
938
|
+
*
|
|
939
|
+
* ```js
|
|
940
|
+
* const store = createStore().setTables({pets: {fido: {sold: false}}});
|
|
941
|
+
*
|
|
942
|
+
* const checkpoints = createCheckpoints(store);
|
|
943
|
+
* console.log(checkpoints.getCheckpointIds());
|
|
944
|
+
* // -> [[], '0', []]
|
|
945
|
+
*
|
|
946
|
+
* const listenerId = checkpoints.addCheckpointIdsListener(() => {
|
|
947
|
+
* console.log('checkpoints changed');
|
|
948
|
+
* });
|
|
949
|
+
*
|
|
950
|
+
* store.setCell('pets', 'fido', 'color', 'brown');
|
|
951
|
+
* // -> 'checkpoints changed'
|
|
952
|
+
* checkpoints.addCheckpoint();
|
|
953
|
+
* // -> 'checkpoints changed'
|
|
954
|
+
* store.setCell('pets', 'fido', 'sold', true);
|
|
955
|
+
* // -> 'checkpoints changed'
|
|
956
|
+
* checkpoints.addCheckpoint();
|
|
957
|
+
* // -> 'checkpoints changed'
|
|
958
|
+
* checkpoints.goBackward();
|
|
959
|
+
* // -> 'checkpoints changed'
|
|
960
|
+
*
|
|
961
|
+
* console.log(store.getTables());
|
|
962
|
+
* // -> {pets: {fido: {color: 'brown', sold: false}}}
|
|
963
|
+
* console.log(checkpoints.getCheckpointIds());
|
|
964
|
+
* // -> [['0'], '1', ['2']]
|
|
965
|
+
*
|
|
966
|
+
* checkpoints.clearForward();
|
|
967
|
+
* // -> 'checkpoints changed'
|
|
968
|
+
*
|
|
969
|
+
* console.log(checkpoints.getCheckpointIds());
|
|
970
|
+
* // -> [['0'], '1', []]
|
|
971
|
+
* ```
|
|
972
|
+
* @category Lifecycle
|
|
973
|
+
* @since v4.5.3
|
|
974
|
+
*/
|
|
975
|
+
clearForward(): Checkpoints<Schemas>;
|
|
976
|
+
|
|
919
977
|
/**
|
|
920
978
|
* The destroy method should be called when this Checkpoints object is no
|
|
921
979
|
* longer used.
|
|
@@ -32,6 +32,11 @@ type CheckpointsOrCheckpointsId<Schemas extends OptionalSchemas> =
|
|
|
32
32
|
|
|
33
33
|
type UndoOrRedoInformation = [boolean, Callback, Id | undefined, string];
|
|
34
34
|
|
|
35
|
+
type GetId<Schemas extends OptionalSchemas, Parameter, Id> = (
|
|
36
|
+
parameter: Parameter,
|
|
37
|
+
store: Store<Schemas>,
|
|
38
|
+
) => Id;
|
|
39
|
+
|
|
35
40
|
type ExtraProps = {[propName: string]: any};
|
|
36
41
|
|
|
37
42
|
type TablesProps<Schemas extends OptionalSchemas> = {
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The persister-electric-sql module of the TinyBase project lets you save and
|
|
3
|
+
* load Store data to and from a local ElectricSQL database (in an appropriate
|
|
4
|
+
* environment).
|
|
5
|
+
* @see Persisting Data guide
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module persister-electric-sql
|
|
8
|
+
* @since v4.6.0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {DatabasePersisterConfig, Persister} from '../persisters';
|
|
12
|
+
import {OptionalSchemas, Store} from '../store';
|
|
13
|
+
import {ElectricClient} from 'electric-sql/client/model';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The ElectricSqlPersister interface is a minor extension to the Persister
|
|
17
|
+
* interface.
|
|
18
|
+
*
|
|
19
|
+
* It simply provides an extra getElectricClient method for accessing a
|
|
20
|
+
* reference to the Electric client the Store is being persisted to.
|
|
21
|
+
* @since v4.6.0
|
|
22
|
+
*/
|
|
23
|
+
export interface ElectricSqlPersister<Schemas extends OptionalSchemas>
|
|
24
|
+
extends Persister<Schemas> {
|
|
25
|
+
/**
|
|
26
|
+
* The getElectricClient method returns a reference to the Electric client the
|
|
27
|
+
* Store is being persisted to.
|
|
28
|
+
* @returns A reference to the Electric client.
|
|
29
|
+
* @example
|
|
30
|
+
* This example creates a Persister object against a newly-created Store and
|
|
31
|
+
* then gets the Electric client back out again.
|
|
32
|
+
*
|
|
33
|
+
* ```js yolo
|
|
34
|
+
* const electricClient = await electrify(
|
|
35
|
+
* await ElectricDatabase.init('electric.db', ''),
|
|
36
|
+
* schema,
|
|
37
|
+
* {url: ELECTRIC_URL},
|
|
38
|
+
* );
|
|
39
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
40
|
+
* const persister = createElectricSqlPersister(
|
|
41
|
+
* store,
|
|
42
|
+
* electricClient,
|
|
43
|
+
* 'my_tinybase',
|
|
44
|
+
* );
|
|
45
|
+
*
|
|
46
|
+
* console.log(persister.getElectricClient() == electricClient);
|
|
47
|
+
* // -> true
|
|
48
|
+
*
|
|
49
|
+
* persister.destroy();
|
|
50
|
+
* ```
|
|
51
|
+
* @category Getter
|
|
52
|
+
* @since v4.6.0
|
|
53
|
+
*/
|
|
54
|
+
getElectricClient: () => ElectricClient<any>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The createElectricSqlPersister function creates a Persister object that can
|
|
59
|
+
* persist the Store to a local ElectricSQL database (in an appropriate
|
|
60
|
+
* environment).
|
|
61
|
+
*
|
|
62
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
63
|
+
*
|
|
64
|
+
* ```ts override
|
|
65
|
+
* createElectricSqlPersister(
|
|
66
|
+
* store: Store,
|
|
67
|
+
* electricClient: ElectricClient<any>,
|
|
68
|
+
* configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
69
|
+
* onSqlCommand?: (sql: string, args?: any[]) => void,
|
|
70
|
+
* onIgnoredError?: (error: any) => void,
|
|
71
|
+
* ): ElectricSqlPersister;
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* As well as providing a reference to the Store to persist, you must provide a
|
|
75
|
+
* `electricClient` parameter which identifies the Electric client.
|
|
76
|
+
*
|
|
77
|
+
* A database Persister uses one of two modes: either a JSON serialization of
|
|
78
|
+
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
79
|
+
* mapping of Table Ids to database table names and vice-versa).
|
|
80
|
+
*
|
|
81
|
+
* The third argument is a DatabasePersisterConfig object that configures which
|
|
82
|
+
* of those modes to use, and settings for each. If the third argument is simply
|
|
83
|
+
* a string, it is used as the `storeTableName` property of the JSON
|
|
84
|
+
* serialization.
|
|
85
|
+
*
|
|
86
|
+
* See the documentation for the DpcJson and DpcTabular types for more
|
|
87
|
+
* information on how both of those modes can be configured.
|
|
88
|
+
* @param store The Store to persist.
|
|
89
|
+
* @param electricClient The Electric client that was returned from `await
|
|
90
|
+
* electrify(...)`.
|
|
91
|
+
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
92
|
+
* persistence mode (or a string to set the `storeTableName` property of the
|
|
93
|
+
* JSON serialization).
|
|
94
|
+
* @param onSqlCommand An optional handler called every time the Persister
|
|
95
|
+
* executes a SQL command or query. This is suitable for logging persistence
|
|
96
|
+
* behavior in a development environment.
|
|
97
|
+
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
98
|
+
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
99
|
+
* debugging persistence issues in a development environment.
|
|
100
|
+
* @returns A reference to the new ElectricSqlPersister object.
|
|
101
|
+
* @example
|
|
102
|
+
* This example creates a ElectricSqlPersister object and persists the Store to
|
|
103
|
+
* a local ElectricSql database as a JSON serialization into the `my_tinybase`
|
|
104
|
+
* table. It makes a change to the database directly and then reloads it back
|
|
105
|
+
* into the Store.
|
|
106
|
+
*
|
|
107
|
+
* ```js yolo
|
|
108
|
+
* const electricClient = await electrify(
|
|
109
|
+
* await ElectricDatabase.init('electric.db', ''),
|
|
110
|
+
* schema,
|
|
111
|
+
* {url: ELECTRIC_URL},
|
|
112
|
+
* );
|
|
113
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
114
|
+
* const persister = createElectricSqlPersister(
|
|
115
|
+
* store,
|
|
116
|
+
* electricClient,
|
|
117
|
+
* 'my_tinybase',
|
|
118
|
+
* );
|
|
119
|
+
*
|
|
120
|
+
* await persister.save();
|
|
121
|
+
* // Store will be saved to the database.
|
|
122
|
+
*
|
|
123
|
+
* console.log(
|
|
124
|
+
* await electricClient.db.raw({sql: 'SELECT * FROM my_tinybase;'}),
|
|
125
|
+
* );
|
|
126
|
+
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
127
|
+
*
|
|
128
|
+
* await electricClient.db.raw({
|
|
129
|
+
* sql:
|
|
130
|
+
* 'UPDATE my_tinybase SET store = ' +
|
|
131
|
+
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
132
|
+
* });
|
|
133
|
+
* await persister.load();
|
|
134
|
+
* console.log(store.getTables());
|
|
135
|
+
* // -> {pets: {felix: {species: 'cat'}}}
|
|
136
|
+
*
|
|
137
|
+
* persister.destroy();
|
|
138
|
+
* ```
|
|
139
|
+
* @example
|
|
140
|
+
* This example creates a ElectricSqlPersister object and persists the Store to
|
|
141
|
+
* a local ElectricSql database with tabular mapping.
|
|
142
|
+
*
|
|
143
|
+
* ```js yolo
|
|
144
|
+
* const electricClient = await electrify(
|
|
145
|
+
* await ElectricDatabase.init('electric.db', ''),
|
|
146
|
+
* schema,
|
|
147
|
+
* {url: ELECTRIC_URL},
|
|
148
|
+
* );
|
|
149
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
150
|
+
* const persister = createElectricSqlPersister(store, electricClient, {
|
|
151
|
+
* mode: 'tabular',
|
|
152
|
+
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
153
|
+
* });
|
|
154
|
+
*
|
|
155
|
+
* await persister.save();
|
|
156
|
+
* console.log(await electricClient.db.raw({sql: 'SELECT * FROM pets;'}));
|
|
157
|
+
* // -> [{_id: 'fido', species: 'dog'}]
|
|
158
|
+
*
|
|
159
|
+
* await electricClient.db.raw({
|
|
160
|
+
* sql: `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
|
|
161
|
+
* });
|
|
162
|
+
* await persister.load();
|
|
163
|
+
* console.log(store.getTables());
|
|
164
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
165
|
+
*
|
|
166
|
+
* persister.destroy();
|
|
167
|
+
* ```
|
|
168
|
+
* @category Creation
|
|
169
|
+
* @since v4.6.0
|
|
170
|
+
*/
|
|
171
|
+
export function createElectricSqlPersister<Schemas extends OptionalSchemas>(
|
|
172
|
+
store: Store<Schemas>,
|
|
173
|
+
electricClient: ElectricClient<any>,
|
|
174
|
+
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
175
|
+
onSqlCommand?: (sql: string, args?: any[]) => void,
|
|
176
|
+
onIgnoredError?: (error: any) => void,
|
|
177
|
+
): ElectricSqlPersister<Schemas>;
|
|
@@ -100,10 +100,10 @@ export interface ExpoSqliteNextPersister<Schemas extends OptionalSchemas>
|
|
|
100
100
|
* JSON serialization).
|
|
101
101
|
* @param onSqlCommand An optional handler called every time the Persister
|
|
102
102
|
* executes a SQL command or query. This is suitable for logging persistence
|
|
103
|
-
* behavior in a development environment
|
|
103
|
+
* behavior in a development environment.
|
|
104
104
|
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
105
105
|
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
106
|
-
* debugging persistence issues in a development environment
|
|
106
|
+
* debugging persistence issues in a development environment.
|
|
107
107
|
* @returns A reference to the new ExpoSqliteNextPersister object.
|
|
108
108
|
* @example
|
|
109
109
|
* This example creates a ExpoSqliteNextPersister object and persists the Store
|
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
NoValuesSchema,
|
|
26
26
|
OptionalSchemas,
|
|
27
27
|
Tables,
|
|
28
|
+
TransactionChanges,
|
|
28
29
|
Value,
|
|
29
30
|
ValueOrUndefined,
|
|
30
31
|
Values,
|
|
@@ -251,7 +252,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
251
252
|
tableId: Id,
|
|
252
253
|
initialSave: boolean,
|
|
253
254
|
requestOrConnection: Request | Connection,
|
|
254
|
-
): boolean
|
|
255
|
+
): Promise<boolean>;
|
|
255
256
|
/**
|
|
256
257
|
* The canDelTable method lets you allow or disallow deletions of a Table
|
|
257
258
|
* stored on the server, as sent from a client.
|
|
@@ -283,7 +284,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
283
284
|
* @category Sanitization
|
|
284
285
|
* @since v4.3.12
|
|
285
286
|
*/
|
|
286
|
-
canDelTable(tableId: Id, connection: Connection): boolean
|
|
287
|
+
canDelTable(tableId: Id, connection: Connection): Promise<boolean>;
|
|
287
288
|
/**
|
|
288
289
|
* The canSetRow method lets you allow or disallow any changes to a Row stored
|
|
289
290
|
* on the server, as sent from a client.
|
|
@@ -326,7 +327,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
326
327
|
rowId: Id,
|
|
327
328
|
initialSave: boolean,
|
|
328
329
|
requestOrConnection: Request | Connection,
|
|
329
|
-
): boolean
|
|
330
|
+
): Promise<boolean>;
|
|
330
331
|
/**
|
|
331
332
|
* The canDelRow method lets you allow or disallow deletions of a Row stored
|
|
332
333
|
* on the server, as sent from a client.
|
|
@@ -359,7 +360,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
359
360
|
* @category Sanitization
|
|
360
361
|
* @since v4.3.12
|
|
361
362
|
*/
|
|
362
|
-
canDelRow(tableId: Id, rowId: Id, connection: Connection): boolean
|
|
363
|
+
canDelRow(tableId: Id, rowId: Id, connection: Connection): Promise<boolean>;
|
|
363
364
|
/**
|
|
364
365
|
* The canSetCell method lets you allow or disallow any changes to a Cell
|
|
365
366
|
* stored on the server, as sent from a client.
|
|
@@ -375,7 +376,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
375
376
|
* initialSave: boolean,
|
|
376
377
|
* requestOrConnection: Request | Connection,
|
|
377
378
|
* oldCell: CellOrUndefined,
|
|
378
|
-
* ): boolean
|
|
379
|
+
* ): Promise<boolean>;
|
|
379
380
|
* ```
|
|
380
381
|
*
|
|
381
382
|
* This is one of the functions use to sanitize the data that is being sent
|
|
@@ -422,7 +423,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
422
423
|
initialSave: boolean,
|
|
423
424
|
requestOrConnection: Request | Connection,
|
|
424
425
|
oldCell: CellOrUndefined<NoTablesSchema, Id, Id>,
|
|
425
|
-
): boolean
|
|
426
|
+
): Promise<boolean>;
|
|
426
427
|
/**
|
|
427
428
|
* The canDelCell method lets you allow or disallow deletions of a Cell stored
|
|
428
429
|
* on the server, as sent from a client.
|
|
@@ -460,7 +461,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
460
461
|
rowId: Id,
|
|
461
462
|
cellId: Id,
|
|
462
463
|
connection: Connection,
|
|
463
|
-
): boolean
|
|
464
|
+
): Promise<boolean>;
|
|
464
465
|
/**
|
|
465
466
|
* The canSetValue method lets you allow or disallow any changes to a Value
|
|
466
467
|
* stored on the server, as sent from a client.
|
|
@@ -474,7 +475,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
474
475
|
* initialSave: boolean,
|
|
475
476
|
* requestOrConnection: Request | Connection,
|
|
476
477
|
* oldValue: ValueOrUndefined,
|
|
477
|
-
* ): boolean
|
|
478
|
+
* ): Promise<boolean>;
|
|
478
479
|
* ```
|
|
479
480
|
*
|
|
480
481
|
* This is one of the functions use to sanitize the data that is being sent
|
|
@@ -519,7 +520,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
519
520
|
initialSave: boolean,
|
|
520
521
|
requestOrConnection: Request | Connection,
|
|
521
522
|
oldValue: ValueOrUndefined<NoValuesSchema, Id>,
|
|
522
|
-
): boolean
|
|
523
|
+
): Promise<boolean>;
|
|
523
524
|
/**
|
|
524
525
|
* The canDelValue method lets you allow or disallow deletions of a Value
|
|
525
526
|
* stored on the server, as sent from a client.
|
|
@@ -551,7 +552,7 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
551
552
|
* @category Sanitization
|
|
552
553
|
* @since v4.3.12
|
|
553
554
|
*/
|
|
554
|
-
canDelValue(valueId: Id, connection: Connection): boolean
|
|
555
|
+
canDelValue(valueId: Id, connection: Connection): Promise<boolean>;
|
|
555
556
|
}
|
|
556
557
|
|
|
557
558
|
/**
|
|
@@ -611,3 +612,36 @@ export function loadStoreFromStorage<Schemas extends OptionalSchemas>(
|
|
|
611
612
|
storage: Storage,
|
|
612
613
|
storagePrefix?: string,
|
|
613
614
|
): Promise<[Tables<Schemas[0]>, Values<Schemas[1]>]>;
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* The broadcastTransactionChanges function allows you to broadcast Store
|
|
618
|
+
* changes to all the client connections of a TinyBasePartyKitServer.
|
|
619
|
+
*
|
|
620
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
621
|
+
*
|
|
622
|
+
* ```ts override
|
|
623
|
+
* broadcastTransactionChanges(
|
|
624
|
+
* server: TinyBasePartyKitServer,
|
|
625
|
+
* transactionChanges: TransactionChanges,
|
|
626
|
+
* without?: string[],
|
|
627
|
+
* ): Promise<void>;
|
|
628
|
+
* ```
|
|
629
|
+
*
|
|
630
|
+
* This is intended for specialist applications that require the ability to
|
|
631
|
+
* update clients of a TinyBasePartyKitServer in their own custom ways.
|
|
632
|
+
*
|
|
633
|
+
* The function is asynchronous, so you should use the `await` keyword or handle
|
|
634
|
+
* its completion as a promise.
|
|
635
|
+
* @param server A reference to the TinyBasePartyKitServer object.
|
|
636
|
+
* @param transactionChanges The Store changes to broadcast to the server's
|
|
637
|
+
* clients.
|
|
638
|
+
* @param without An optional array of client connection Ids to exclude from the
|
|
639
|
+
* broadcast.
|
|
640
|
+
* @category Connection
|
|
641
|
+
* @since v4.5.1
|
|
642
|
+
*/
|
|
643
|
+
export function broadcastTransactionChanges<Schemas extends OptionalSchemas>(
|
|
644
|
+
server: TinyBasePartyKitServer,
|
|
645
|
+
transactionChanges: TransactionChanges<Schemas>,
|
|
646
|
+
without?: string[],
|
|
647
|
+
): Promise<void>;
|
|
@@ -164,4 +164,4 @@ export function createSqlite3Persister<Schemas extends OptionalSchemas>(
|
|
|
164
164
|
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
165
165
|
onSqlCommand?: (sql: string, args?: any[]) => void,
|
|
166
166
|
onIgnoredError?: (error: any) => void,
|
|
167
|
-
):
|
|
167
|
+
): Sqlite3Persister<Schemas>;
|
|
@@ -15,11 +15,13 @@
|
|
|
15
15
|
* |persister-indexed-db|createIndexedDbPersister|Browser IndexedDB|
|
|
16
16
|
* |persister-remote|createRemotePersister|Remote server|
|
|
17
17
|
* |persister-file|createFilePersister|Local file (where possible)|
|
|
18
|
+
* |persister-partykit-client|createPartyKitPersister|PartyKit with the persister-partykit-server module|
|
|
18
19
|
* |persister-sqlite3|createSqlite3Persister|SQLite in Node, via [sqlite3](https://github.com/TryGhost/node-sqlite3)|
|
|
19
20
|
* |persister-sqlite-wasm|createSqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|
|
|
20
21
|
* |persister-cr-sqlite-wasm|createCrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|
|
|
21
22
|
* |persister-expo-sqlite|createExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|
|
|
22
23
|
* |persister-expo-sqlite-next|createExpoSqliteNextPersister|SQLite in React Native, via [expo-sqlite/next](https://github.com/expo/expo/tree/main/packages/expo-sqlite/next)|
|
|
24
|
+
* |persister-electric-sql|createElectricSqlPersister|Electric SQL, via [electric-sql](https://github.com/electric-sql/electric)|
|
|
23
25
|
* |persister-yjs|createYjsPersister|Yjs CRDTs, via [yjs](https://github.com/yjs/yjs)|
|
|
24
26
|
* |persister-automerge|createSqliteWasmPersister|Automerge CRDTs, via [automerge-repo](https://github.com/automerge/automerge-repo)|
|
|
25
27
|
*
|