tinybase 4.5.0-beta.0 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/persisters/persister-expo-sqlite-next.cjs +1 -0
- package/lib/cjs/persisters/persister-expo-sqlite-next.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/ui-react.cjs +1 -1
- package/lib/cjs/ui-react.cjs.gz +0 -0
- package/lib/cjs-es6/persisters/persister-expo-sqlite-next.cjs +1 -0
- package/lib/cjs-es6/persisters/persister-expo-sqlite-next.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/ui-react.cjs +1 -1
- package/lib/cjs-es6/ui-react.cjs.gz +0 -0
- package/lib/debug/checkpoints.js +8 -8
- package/lib/debug/indexes.js +8 -8
- package/lib/debug/metrics.js +8 -8
- package/lib/debug/persisters/persister-automerge.js +23 -23
- package/lib/debug/persisters/persister-expo-sqlite-next.js +835 -0
- package/lib/debug/persisters/persister-partykit-server.js +35 -21
- package/lib/debug/persisters/persister-yjs.js +23 -23
- package/lib/debug/queries.js +10 -10
- package/lib/debug/relationships.js +6 -6
- package/lib/debug/store.js +10 -10
- package/lib/debug/tinybase.js +24 -24
- package/lib/debug/tools.js +2 -2
- package/lib/debug/ui-react-dom.js +12 -12
- package/lib/debug/ui-react.js +56 -22
- package/lib/es6/persisters/persister-expo-sqlite-next.js +1 -0
- package/lib/es6/persisters/persister-expo-sqlite-next.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/ui-react.js +1 -1
- package/lib/es6/ui-react.js.gz +0 -0
- package/lib/persisters/persister-expo-sqlite-next.js +1 -0
- package/lib/persisters/persister-expo-sqlite-next.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/types/metrics.d.ts +2 -2
- package/lib/types/persisters/persister-expo-sqlite-next.d.ts +168 -0
- package/lib/types/persisters/persister-expo-sqlite.d.ts +8 -0
- package/lib/types/persisters/persister-partykit-server.d.ts +58 -2
- package/lib/types/persisters/persister-remote.d.ts +6 -1
- package/lib/types/persisters.d.ts +1 -0
- package/lib/types/queries.d.ts +2 -45
- package/lib/types/ui-react.d.ts +53 -0
- package/lib/types/with-schemas/metrics.d.ts +2 -2
- package/lib/types/with-schemas/persisters/persister-expo-sqlite-next.d.ts +181 -0
- package/lib/types/with-schemas/persisters/persister-expo-sqlite.d.ts +8 -0
- package/lib/types/with-schemas/persisters/persister-partykit-server.d.ts +61 -0
- package/lib/types/with-schemas/persisters/persister-remote.d.ts +6 -1
- package/lib/types/with-schemas/persisters.d.ts +1 -0
- package/lib/types/with-schemas/queries.d.ts +2 -45
- package/lib/types/with-schemas/store.d.ts +8 -8
- package/lib/types/with-schemas/ui-react.d.ts +59 -0
- package/lib/ui-react.js +1 -1
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/persisters/persister-expo-sqlite-next.js +1 -0
- package/lib/umd/persisters/persister-expo-sqlite-next.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/ui-react.js +1 -1
- package/lib/umd/ui-react.js.gz +0 -0
- package/lib/umd-es6/persisters/persister-expo-sqlite-next.js +1 -0
- package/lib/umd-es6/persisters/persister-expo-sqlite-next.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/ui-react.js +1 -1
- package/lib/umd-es6/ui-react.js.gz +0 -0
- package/package.json +22 -23
- package/readme.md +13 -13
|
@@ -18,8 +18,15 @@
|
|
|
18
18
|
* @since 4.3.0
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
import {
|
|
22
|
-
|
|
21
|
+
import {
|
|
22
|
+
Cell,
|
|
23
|
+
CellOrUndefined,
|
|
24
|
+
Tables,
|
|
25
|
+
Value,
|
|
26
|
+
ValueOrUndefined,
|
|
27
|
+
Values,
|
|
28
|
+
} from '../store';
|
|
29
|
+
import {Connection, Party, Request, Server, Storage} from 'partykit/server';
|
|
23
30
|
import {Id} from '../common';
|
|
24
31
|
|
|
25
32
|
/**
|
|
@@ -517,3 +524,52 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
517
524
|
*/
|
|
518
525
|
canDelValue(valueId: Id, connection: Connection): boolean;
|
|
519
526
|
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* The hasStoreInStorage function returns a boolean indicating whether durable
|
|
530
|
+
* PartyKit storage contains a serialized Store.
|
|
531
|
+
*
|
|
532
|
+
* This is intended for specialist applications that require the ability to
|
|
533
|
+
* inspect or load a TinyBase Store from a server's storage outside of the
|
|
534
|
+
* normal context of a TinyBasePartyKitServer.
|
|
535
|
+
*
|
|
536
|
+
* The function is asynchronous, so you should use the `await` keyword or handle
|
|
537
|
+
* the result as a promise.
|
|
538
|
+
* @param storage A reference to the storage object, as would normally be
|
|
539
|
+
* accessible from the `TinyBasePartyKitServer.party` object.
|
|
540
|
+
* @param storagePrefix An optional prefix used before all the keys in the
|
|
541
|
+
* server's durable storage, to match the equivalent property in the server's
|
|
542
|
+
* TinyBasePartyKitServerConfig.
|
|
543
|
+
* @returns A promised boolean indicating whether a Store is present in the
|
|
544
|
+
* storage.
|
|
545
|
+
* @category Storage
|
|
546
|
+
* @since v4.4.1
|
|
547
|
+
*/
|
|
548
|
+
export function hasStoreInStorage(
|
|
549
|
+
storage: Storage,
|
|
550
|
+
storagePrefix?: string,
|
|
551
|
+
): Promise<boolean>;
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* The loadStoreFromStorage function returns the content of a Store from durable
|
|
555
|
+
* PartyKit storage.
|
|
556
|
+
*
|
|
557
|
+
* This is intended for specialist applications that require the ability to
|
|
558
|
+
* inspect or load a TinyBase Store from a server's storage outside of the
|
|
559
|
+
* normal context of a TinyBasePartyKitServer.
|
|
560
|
+
*
|
|
561
|
+
* The function is asynchronous, so you should use the `await` keyword or handle
|
|
562
|
+
* the result as a promise.
|
|
563
|
+
* @param storage A reference to the storage object, as would normally be
|
|
564
|
+
* accessible from the `TinyBasePartyKitServer.party` object.
|
|
565
|
+
* @param storagePrefix An optional prefix used before all the keys in the
|
|
566
|
+
* server's durable storage, to match the equivalent property in the server's
|
|
567
|
+
* TinyBasePartyKitServerConfig.
|
|
568
|
+
* @returns A promised array of a Tables object and a Values object.
|
|
569
|
+
* @category Storage
|
|
570
|
+
* @since v4.4.1
|
|
571
|
+
*/
|
|
572
|
+
export function loadStoreFromStorage(
|
|
573
|
+
storage: Storage,
|
|
574
|
+
storagePrefix?: string,
|
|
575
|
+
): Promise<[Tables, Values]>;
|
|
@@ -55,8 +55,13 @@ export interface RemotePersister extends Persister {
|
|
|
55
55
|
* the `POST` method (to send the Store JSON to save) respectively.
|
|
56
56
|
*
|
|
57
57
|
* For when you choose to enable automatic loading for the Persister (with the
|
|
58
|
-
* startAutoLoad method), it will poll the loadUrl for changes
|
|
58
|
+
* startAutoLoad method), it will poll the loadUrl for changes, using the
|
|
59
|
+
* `ETag` HTTP header to identify if things have changed. The
|
|
59
60
|
* `autoLoadIntervalSeconds` method is used to indicate how often to do this.
|
|
61
|
+
*
|
|
62
|
+
* If you are implementing the server portion of this functionality yourself,
|
|
63
|
+
* remember to ensure that the `ETag` header changes every time the server's
|
|
64
|
+
* Store content does - otherwise changes will not be detected by the client.
|
|
60
65
|
* @param store The Store to persist.
|
|
61
66
|
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
62
67
|
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* |persister-sqlite-wasm|createSqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|
|
|
20
20
|
* |persister-cr-sqlite-wasm|createCrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|
|
|
21
21
|
* |persister-expo-sqlite|createExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|
|
|
22
|
+
* |persister-expo-sqlite-next|createExpoSqliteNextPersister|SQLite in React Native, via [expo-sqlite/next](https://github.com/expo/expo/tree/main/packages/expo-sqlite/next)|
|
|
22
23
|
* |persister-yjs|createYjsPersister|Yjs CRDTs, via [yjs](https://github.com/yjs/yjs)|
|
|
23
24
|
* |persister-automerge|createSqliteWasmPersister|Automerge CRDTs, via [automerge-repo](https://github.com/automerge/automerge-repo)|
|
|
24
25
|
*
|
package/lib/types/queries.d.ts
CHANGED
|
@@ -1313,49 +1313,6 @@ export type Where = {
|
|
|
1313
1313
|
* ```js
|
|
1314
1314
|
* const store = createStore()
|
|
1315
1315
|
* .setTable('pets', {
|
|
1316
|
-
* fido: {species: 'dog', color: 'brown', owner: 'alice'},
|
|
1317
|
-
* felix: {species: 'cat', color: 'black', owner: 'bob'},
|
|
1318
|
-
* cujo: {species: 'dog', color: 'black', owner: 'bob'},
|
|
1319
|
-
* lowly: {species: 'worm', color: 'brown', owner: 'alice'},
|
|
1320
|
-
* carnaby: {species: 'parrot', color: 'black', owner: 'bob'},
|
|
1321
|
-
* polly: {species: 'parrot', color: 'red', owner: 'alice'},
|
|
1322
|
-
* })
|
|
1323
|
-
* .setTable('species', {
|
|
1324
|
-
* dog: {price: 5, legs: 4},
|
|
1325
|
-
* cat: {price: 4, legs: 4},
|
|
1326
|
-
* parrot: {price: 3, legs: 2},
|
|
1327
|
-
* worm: {price: 1, legs: 0},
|
|
1328
|
-
* });
|
|
1329
|
-
*
|
|
1330
|
-
* const queries = createQueries(store);
|
|
1331
|
-
* queries.setQueryDefinition('query', 'pets', ({select, join, group}) => {
|
|
1332
|
-
* select('pets', 'color'); // group by
|
|
1333
|
-
* select('pets', 'owner'); // group by
|
|
1334
|
-
* select('species', 'price'); // grouped
|
|
1335
|
-
* select('species', 'legs'); // grouped
|
|
1336
|
-
* // from pets
|
|
1337
|
-
* join('species', 'species');
|
|
1338
|
-
* group('price', 'avg').as('avgPrice');
|
|
1339
|
-
* group('legs', 'sum').as('sumLegs');
|
|
1340
|
-
* });
|
|
1341
|
-
*
|
|
1342
|
-
* queries.forEachResultRow('query', (rowId) => {
|
|
1343
|
-
* console.log({[rowId]: queries.getResultRow('query', rowId)});
|
|
1344
|
-
* });
|
|
1345
|
-
* // -> {0: {color: 'brown', owner: 'alice', avgPrice: 3, sumLegs: 4}}
|
|
1346
|
-
* // -> {1: {color: 'black', owner: 'bob', avgPrice: 4, sumLegs: 10}}
|
|
1347
|
-
* // -> {2: {color: 'red', owner: 'alice', avgPrice: 3, sumLegs: 2}}
|
|
1348
|
-
* ```
|
|
1349
|
-
* @example
|
|
1350
|
-
* This example shows a query that calculates the a custom aggregate of one
|
|
1351
|
-
* Cell's values, grouped by another. Note how `aggregateAdd`,
|
|
1352
|
-
* `aggregateRemove`, and `aggregateReplace` parameters are provided to make the
|
|
1353
|
-
* custom aggregation more efficient as individual values are added, removed, or
|
|
1354
|
-
* replaced during the lifecycle of the Table.
|
|
1355
|
-
*
|
|
1356
|
-
* ```js
|
|
1357
|
-
* const store = createStore()
|
|
1358
|
-
* .setTable('pets', {
|
|
1359
1316
|
* fido: {species: 'dog', owner: 'alice'},
|
|
1360
1317
|
* felix: {species: 'cat', owner: 'bob'},
|
|
1361
1318
|
* cujo: {species: 'dog', owner: 'bob'},
|
|
@@ -1385,8 +1342,8 @@ export type Where = {
|
|
|
1385
1342
|
* remove == current
|
|
1386
1343
|
* ? undefined
|
|
1387
1344
|
* : add > 2
|
|
1388
|
-
*
|
|
1389
|
-
*
|
|
1345
|
+
* ? Math.min(current, add)
|
|
1346
|
+
* : current,
|
|
1390
1347
|
* ).as('lowestPriceOver2');
|
|
1391
1348
|
* });
|
|
1392
1349
|
*
|
package/lib/types/ui-react.d.ts
CHANGED
|
@@ -427,6 +427,59 @@ export function useStoreOrStoreById(
|
|
|
427
427
|
storeOrStoreId?: StoreOrStoreId,
|
|
428
428
|
): Store | undefined;
|
|
429
429
|
|
|
430
|
+
/**
|
|
431
|
+
* The useProvideStore hook is used to add a Store object by Id to a Provider
|
|
432
|
+
* component, but imperatively from a component within it.
|
|
433
|
+
*
|
|
434
|
+
* Normally you will register a Store by Id in a context by using the
|
|
435
|
+
* `storesById` prop of the top-level Provider component. This hook, however,
|
|
436
|
+
* lets you dynamically add a new Store to the context, from within a descendent
|
|
437
|
+
* component. This is useful for applications where the set of Stores is not
|
|
438
|
+
* known at the time of the first render of the root Provider.
|
|
439
|
+
*
|
|
440
|
+
* A Store added to the Provider context in this way will be available to other
|
|
441
|
+
* components within the context (using the useStore hook and so on). If you use
|
|
442
|
+
* the same Id as an existing Store registration, the new one will take priority
|
|
443
|
+
* over one provided by the `storesById` prop.
|
|
444
|
+
*
|
|
445
|
+
* Note that other components that consume a Store registered like this should
|
|
446
|
+
* defend against it being undefined at first. On the first render, the other
|
|
447
|
+
* component will likely not yet have completed the registration. In the example
|
|
448
|
+
* below, we use the null-safe `useStore('petStore')?.getTableIds()` to do this.
|
|
449
|
+
* @param storeId The Id of the Store object to be registered with the Provider.
|
|
450
|
+
* @param store The Store object to be registered.
|
|
451
|
+
* @example
|
|
452
|
+
* This example creates a Provider context. A child component registers a Store
|
|
453
|
+
* into it which is then consumable by a peer child component.
|
|
454
|
+
* ```jsx
|
|
455
|
+
* const App = () => (
|
|
456
|
+
* <Provider>
|
|
457
|
+
* <RegisterStore />
|
|
458
|
+
* <ConsumeStore />
|
|
459
|
+
* </Provider>
|
|
460
|
+
* );
|
|
461
|
+
* const RegisterStore = () => {
|
|
462
|
+
* const store = useCreateStore(() =>
|
|
463
|
+
* createStore().setCell('pets', 'fido', 'color', 'brown'),
|
|
464
|
+
* );
|
|
465
|
+
* useProvideStore('petStore', store);
|
|
466
|
+
* return null;
|
|
467
|
+
* };
|
|
468
|
+
* const ConsumeStore = () => (
|
|
469
|
+
* <span>{JSON.stringify(useStore('petStore')?.getTableIds())}</span>
|
|
470
|
+
* );
|
|
471
|
+
*
|
|
472
|
+
* const app = document.createElement('div');
|
|
473
|
+
* const root = ReactDOMClient.createRoot(app);
|
|
474
|
+
* root.render(<App />); // !act
|
|
475
|
+
* console.log(app.innerHTML);
|
|
476
|
+
* // -> '<span>["pets"]</span>'
|
|
477
|
+
* ```
|
|
478
|
+
* @category Store hooks
|
|
479
|
+
* @since v4.4.2
|
|
480
|
+
*/
|
|
481
|
+
export function useProvideStore(storeId: Id, store: Store): void;
|
|
482
|
+
|
|
430
483
|
/**
|
|
431
484
|
* The useHasTables hook returns a boolean indicating whether any Table objects
|
|
432
485
|
* exist in the Store, and registers a listener so that any changes to that
|
|
@@ -413,8 +413,8 @@ export interface Metrics<in out Schemas extends OptionalSchemas> {
|
|
|
413
413
|
* remove == metric
|
|
414
414
|
* ? undefined
|
|
415
415
|
* : add > 2
|
|
416
|
-
*
|
|
417
|
-
*
|
|
416
|
+
* ? Math.min(metric, add)
|
|
417
|
+
* : metric,
|
|
418
418
|
* );
|
|
419
419
|
*
|
|
420
420
|
* console.log(metrics.getMetric('lowestPriceOver2'));
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The persister-expo-sqlite-next module of the TinyBase project lets you save
|
|
3
|
+
* and load Store data to and from a local Expo-SQLite 'next' database (in an
|
|
4
|
+
* appropriate React Native environment).
|
|
5
|
+
*
|
|
6
|
+
* This module provides a Persister for the modern version of Expo's
|
|
7
|
+
* [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite)
|
|
8
|
+
* library, designated 'next' as of November 2023. This API should be used if
|
|
9
|
+
* you are installing the `expo-sqlite/next` module. In the future we expect
|
|
10
|
+
* this module to become the default.
|
|
11
|
+
*
|
|
12
|
+
* Note that TinyBase support for the legacy version of Expo-SQLite
|
|
13
|
+
* (`expo-sqlite`) is still available in the persister-expo-sqlite module.
|
|
14
|
+
*
|
|
15
|
+
* Note that this Persister is currently experimental as Expo themselves iterate
|
|
16
|
+
* on the underlying database library API.
|
|
17
|
+
* @see Persisting Data guide
|
|
18
|
+
* @packageDocumentation
|
|
19
|
+
* @module persister-expo-sqlite-next
|
|
20
|
+
* @since v4.5.0
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {DatabasePersisterConfig, Persister} from '../persisters';
|
|
24
|
+
import {OptionalSchemas, Store} from '../store';
|
|
25
|
+
import {Database} from 'expo-sqlite/next';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The ExpoSqliteNextPersister interface is a minor extension to the Persister
|
|
29
|
+
* interface.
|
|
30
|
+
*
|
|
31
|
+
* It simply provides an extra getDb method for accessing a reference to the
|
|
32
|
+
* database instance the Store is being persisted to.
|
|
33
|
+
* @since v4.5.0
|
|
34
|
+
*/
|
|
35
|
+
export interface ExpoSqliteNextPersister<Schemas extends OptionalSchemas>
|
|
36
|
+
extends Persister<Schemas> {
|
|
37
|
+
/**
|
|
38
|
+
* The getDb method returns a reference to the database instance the Store is
|
|
39
|
+
* being persisted to.
|
|
40
|
+
* @returns A reference to the database instance.
|
|
41
|
+
* @example
|
|
42
|
+
* This example creates a Persister object against a newly-created Store and
|
|
43
|
+
* then gets the database instance back out again.
|
|
44
|
+
*
|
|
45
|
+
* ```js yolo
|
|
46
|
+
* const db = SQLite.openDatabaseSync('my.db');
|
|
47
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
48
|
+
* const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
|
|
49
|
+
*
|
|
50
|
+
* console.log(persister.getDb() == db);
|
|
51
|
+
* // -> true
|
|
52
|
+
*
|
|
53
|
+
* persister.destroy();
|
|
54
|
+
* ```
|
|
55
|
+
* @category Getter
|
|
56
|
+
* @since v4.5.0
|
|
57
|
+
*/
|
|
58
|
+
getDb: () => Database;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The createExpoSqliteNextPersister function creates a Persister object that
|
|
63
|
+
* can persist the Store to a local Expo-SQLite database (in an appropriate
|
|
64
|
+
* React Native environment).
|
|
65
|
+
*
|
|
66
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
67
|
+
*
|
|
68
|
+
* ```ts override
|
|
69
|
+
* createExpoSqliteNextPersister(
|
|
70
|
+
* store: Store,
|
|
71
|
+
* db: Database,
|
|
72
|
+
* configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
73
|
+
* onSqlCommand?: (sql: string, args?: any[]) => void,
|
|
74
|
+
* onIgnoredError?: (error: any) => void,
|
|
75
|
+
* ): ExpoSqliteNextPersister;
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* Note that this Persister is currently experimental as Expo themselves iterate
|
|
79
|
+
* on the underlying database library API.
|
|
80
|
+
*
|
|
81
|
+
* As well as providing a reference to the Store to persist, you must provide a
|
|
82
|
+
* `db` parameter which identifies the database instance.
|
|
83
|
+
*
|
|
84
|
+
* A database Persister uses one of two modes: either a JSON serialization of
|
|
85
|
+
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
86
|
+
* mapping of Table Ids to database table names and vice-versa).
|
|
87
|
+
*
|
|
88
|
+
* The third argument is a DatabasePersisterConfig object that configures which
|
|
89
|
+
* of those modes to use, and settings for each. If the third argument is simply
|
|
90
|
+
* a string, it is used as the `storeTableName` property of the JSON
|
|
91
|
+
* serialization.
|
|
92
|
+
*
|
|
93
|
+
* See the documentation for the DpcJson and DpcTabular types for more
|
|
94
|
+
* information on how both of those modes can be configured.
|
|
95
|
+
* @param store The Store to persist.
|
|
96
|
+
* @param db The database instance that was returned from
|
|
97
|
+
* `SQLite.openDatabase(...)`.
|
|
98
|
+
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
99
|
+
* persistence mode (or a string to set the `storeTableName` property of the
|
|
100
|
+
* JSON serialization).
|
|
101
|
+
* @param onSqlCommand An optional handler called every time the Persister
|
|
102
|
+
* executes a SQL command or query. This is suitable for logging persistence
|
|
103
|
+
* behavior in a development environment, since v4.0.4.
|
|
104
|
+
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
105
|
+
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
106
|
+
* debugging persistence issues in a development environment, since v4.0.4.
|
|
107
|
+
* @returns A reference to the new ExpoSqliteNextPersister object.
|
|
108
|
+
* @example
|
|
109
|
+
* This example creates a ExpoSqliteNextPersister object and persists the Store
|
|
110
|
+
* to a local SQLite database as a JSON serialization into the `my_tinybase`
|
|
111
|
+
* table. It makes a change to the database directly and then reloads it back
|
|
112
|
+
* into the Store.
|
|
113
|
+
*
|
|
114
|
+
* ```js yolo
|
|
115
|
+
* const db = SQLite.openDatabaseSync('my.db');
|
|
116
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
117
|
+
* const persister = createExpoSqliteNextPersister(store, db, 'my_tinybase');
|
|
118
|
+
*
|
|
119
|
+
* await persister.save();
|
|
120
|
+
* // Store will be saved to the database.
|
|
121
|
+
*
|
|
122
|
+
* console.log(
|
|
123
|
+
* await new Promise((resolve) =>
|
|
124
|
+
* db.allAsync('SELECT * FROM my_tinybase;').then(resolve),
|
|
125
|
+
* ),
|
|
126
|
+
* );
|
|
127
|
+
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
128
|
+
*
|
|
129
|
+
* await new Promise((resolve) =>
|
|
130
|
+
* db.allAsync(
|
|
131
|
+
* 'UPDATE my_tinybase SET store = ' +
|
|
132
|
+
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
133
|
+
* ),
|
|
134
|
+
* );
|
|
135
|
+
* await persister.load();
|
|
136
|
+
* console.log(store.getTables());
|
|
137
|
+
* // -> {pets: {felix: {species: 'cat'}}}
|
|
138
|
+
*
|
|
139
|
+
* persister.destroy();
|
|
140
|
+
* ```
|
|
141
|
+
* @example
|
|
142
|
+
* This example creates a ExpoSqliteNextPersister object and persists the Store
|
|
143
|
+
* to a local SQLite database with tabular mapping.
|
|
144
|
+
*
|
|
145
|
+
* ```js yolo
|
|
146
|
+
* const db = SQLite.openDatabaseSync('my.db');
|
|
147
|
+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
148
|
+
* const persister = createExpoSqliteNextPersister(store, db, {
|
|
149
|
+
* mode: 'tabular',
|
|
150
|
+
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
151
|
+
* });
|
|
152
|
+
*
|
|
153
|
+
* await persister.save();
|
|
154
|
+
* console.log(
|
|
155
|
+
* await new Promise((resolve) =>
|
|
156
|
+
* db.allAsync('SELECT * FROM pets;').then(resolve),
|
|
157
|
+
* ),
|
|
158
|
+
* );
|
|
159
|
+
* // -> [{_id: 'fido', species: 'dog'}]
|
|
160
|
+
*
|
|
161
|
+
* await new Promise((resolve) =>
|
|
162
|
+
* db
|
|
163
|
+
* .allAsync(`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`)
|
|
164
|
+
* .then(resolve),
|
|
165
|
+
* );
|
|
166
|
+
* await persister.load();
|
|
167
|
+
* console.log(store.getTables());
|
|
168
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
169
|
+
*
|
|
170
|
+
* persister.destroy();
|
|
171
|
+
* ```
|
|
172
|
+
* @category Creation
|
|
173
|
+
* @since v4.5.0
|
|
174
|
+
*/
|
|
175
|
+
export function createExpoSqliteNextPersister<Schemas extends OptionalSchemas>(
|
|
176
|
+
store: Store<Schemas>,
|
|
177
|
+
db: Database,
|
|
178
|
+
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
179
|
+
onSqlCommand?: (sql: string, args?: any[]) => void,
|
|
180
|
+
onIgnoredError?: (error: any) => void,
|
|
181
|
+
): ExpoSqliteNextPersister<Schemas>;
|
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
* load Store data to and from a local Expo-SQLite database (in an appropriate
|
|
4
4
|
* React Native environment).
|
|
5
5
|
*
|
|
6
|
+
* This module provides a Persister for the legacy version of Expo's
|
|
7
|
+
* [SQLite](https://docs.expo.dev/versions/latest/sdk/sqlite) library. This API
|
|
8
|
+
* should be used if you are installing the `expo-sqlite` module.
|
|
9
|
+
*
|
|
10
|
+
* Note that TinyBase support for the modern version of Expo-SQLite
|
|
11
|
+
* (`expo-sqlite-next`) is currently available in the persister-expo-sqlite-next
|
|
12
|
+
* module.
|
|
13
|
+
*
|
|
6
14
|
* Note that this Persister is currently experimental as Expo themselves iterate
|
|
7
15
|
* on the underlying database library API.
|
|
8
16
|
* @see Persisting Data guide
|
|
@@ -23,8 +23,11 @@ import {
|
|
|
23
23
|
CellOrUndefined,
|
|
24
24
|
NoTablesSchema,
|
|
25
25
|
NoValuesSchema,
|
|
26
|
+
OptionalSchemas,
|
|
27
|
+
Tables,
|
|
26
28
|
Value,
|
|
27
29
|
ValueOrUndefined,
|
|
30
|
+
Values,
|
|
28
31
|
} from '../store';
|
|
29
32
|
import {Connection, Party, Request, Server} from 'partykit/server';
|
|
30
33
|
import {Id} from '../common';
|
|
@@ -550,3 +553,61 @@ export class TinyBasePartyKitServer implements Server {
|
|
|
550
553
|
*/
|
|
551
554
|
canDelValue(valueId: Id, connection: Connection): boolean;
|
|
552
555
|
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* The hasStoreInStorage function returns a boolean indicating whether durable
|
|
559
|
+
* PartyKit storage contains a serialized Store.
|
|
560
|
+
*
|
|
561
|
+
* This is intended for specialist applications that require the ability to
|
|
562
|
+
* inspect or load a TinyBase Store from a server's storage outside of the
|
|
563
|
+
* normal context of a TinyBasePartyKitServer.
|
|
564
|
+
*
|
|
565
|
+
* The function is asynchronous, so you should use the `await` keyword or handle
|
|
566
|
+
* the result as a promise.
|
|
567
|
+
* @param storage A reference to the storage object, as would normally be
|
|
568
|
+
* accessible from the `TinyBasePartyKitServer.party` object.
|
|
569
|
+
* @param storagePrefix An optional prefix used before all the keys in the
|
|
570
|
+
* server's durable storage, to match the equivalent property in the server's
|
|
571
|
+
* TinyBasePartyKitServerConfig.
|
|
572
|
+
* @returns A promised boolean indicating whether a Store is present in the
|
|
573
|
+
* storage.
|
|
574
|
+
* @category Storage
|
|
575
|
+
* @since v4.4.1
|
|
576
|
+
*/
|
|
577
|
+
export function hasStoreInStorage(
|
|
578
|
+
storage: Storage,
|
|
579
|
+
storagePrefix?: string,
|
|
580
|
+
): Promise<boolean>;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* The loadStoreFromStorage function returns the content of a Store from durable
|
|
584
|
+
* PartyKit storage.
|
|
585
|
+
*
|
|
586
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
587
|
+
*
|
|
588
|
+
* ```ts override
|
|
589
|
+
* loadStoreFromStorage(
|
|
590
|
+
* storage: Storage,
|
|
591
|
+
* storagePrefix?: string,
|
|
592
|
+
* ): Promise<[Tables, Values]>;
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* This is intended for specialist applications that require the ability to
|
|
596
|
+
* inspect or load a TinyBase Store from a server's storage outside of the
|
|
597
|
+
* normal context of a TinyBasePartyKitServer.
|
|
598
|
+
*
|
|
599
|
+
* The function is asynchronous, so you should use the `await` keyword or handle
|
|
600
|
+
* the result as a promise.
|
|
601
|
+
* @param storage A reference to the storage object, as would normally be
|
|
602
|
+
* accessible from the `TinyBasePartyKitServer.party` object.
|
|
603
|
+
* @param storagePrefix An optional prefix used before all the keys in the
|
|
604
|
+
* server's durable storage, to match the equivalent property in the server's
|
|
605
|
+
* TinyBasePartyKitServerConfig.
|
|
606
|
+
* @returns A promised array of a Tables object and a Values object.
|
|
607
|
+
* @category Storage
|
|
608
|
+
* @since v4.4.1
|
|
609
|
+
*/
|
|
610
|
+
export function loadStoreFromStorage<Schemas extends OptionalSchemas>(
|
|
611
|
+
storage: Storage,
|
|
612
|
+
storagePrefix?: string,
|
|
613
|
+
): Promise<[Tables<Schemas[0]>, Values<Schemas[1]>]>;
|
|
@@ -68,8 +68,13 @@ export interface RemotePersister<Schemas extends OptionalSchemas>
|
|
|
68
68
|
* the `POST` method (to send the Store JSON to save) respectively.
|
|
69
69
|
*
|
|
70
70
|
* For when you choose to enable automatic loading for the Persister (with the
|
|
71
|
-
* startAutoLoad method), it will poll the loadUrl for changes
|
|
71
|
+
* startAutoLoad method), it will poll the loadUrl for changes, using the
|
|
72
|
+
* `ETag` HTTP header to identify if things have changed. The
|
|
72
73
|
* `autoLoadIntervalSeconds` method is used to indicate how often to do this.
|
|
74
|
+
*
|
|
75
|
+
* If you are implementing the server portion of this functionality yourself,
|
|
76
|
+
* remember to ensure that the `ETag` header changes every time the server's
|
|
77
|
+
* Store content does - otherwise changes will not be detected by the client.
|
|
73
78
|
* @param store The Store to persist.
|
|
74
79
|
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
75
80
|
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* |persister-sqlite-wasm|createSqliteWasmPersister|SQLite in a browser, via [sqlite-wasm](https://github.com/tomayac/sqlite-wasm)|
|
|
20
20
|
* |persister-cr-sqlite-wasm|createCrSqliteWasmPersister|SQLite CRDTs, via [cr-sqlite-wasm](https://github.com/vlcn-io/cr-sqlite)|
|
|
21
21
|
* |persister-expo-sqlite|createExpoSqlitePersister|SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite)|
|
|
22
|
+
* |persister-expo-sqlite-next|createExpoSqliteNextPersister|SQLite in React Native, via [expo-sqlite/next](https://github.com/expo/expo/tree/main/packages/expo-sqlite/next)|
|
|
22
23
|
* |persister-yjs|createYjsPersister|Yjs CRDTs, via [yjs](https://github.com/yjs/yjs)|
|
|
23
24
|
* |persister-automerge|createSqliteWasmPersister|Automerge CRDTs, via [automerge-repo](https://github.com/automerge/automerge-repo)|
|
|
24
25
|
*
|
|
@@ -1500,49 +1500,6 @@ export type Where<
|
|
|
1500
1500
|
* ```js
|
|
1501
1501
|
* const store = createStore()
|
|
1502
1502
|
* .setTable('pets', {
|
|
1503
|
-
* fido: {species: 'dog', color: 'brown', owner: 'alice'},
|
|
1504
|
-
* felix: {species: 'cat', color: 'black', owner: 'bob'},
|
|
1505
|
-
* cujo: {species: 'dog', color: 'black', owner: 'bob'},
|
|
1506
|
-
* lowly: {species: 'worm', color: 'brown', owner: 'alice'},
|
|
1507
|
-
* carnaby: {species: 'parrot', color: 'black', owner: 'bob'},
|
|
1508
|
-
* polly: {species: 'parrot', color: 'red', owner: 'alice'},
|
|
1509
|
-
* })
|
|
1510
|
-
* .setTable('species', {
|
|
1511
|
-
* dog: {price: 5, legs: 4},
|
|
1512
|
-
* cat: {price: 4, legs: 4},
|
|
1513
|
-
* parrot: {price: 3, legs: 2},
|
|
1514
|
-
* worm: {price: 1, legs: 0},
|
|
1515
|
-
* });
|
|
1516
|
-
*
|
|
1517
|
-
* const queries = createQueries(store);
|
|
1518
|
-
* queries.setQueryDefinition('query', 'pets', ({select, join, group}) => {
|
|
1519
|
-
* select('pets', 'color'); // group by
|
|
1520
|
-
* select('pets', 'owner'); // group by
|
|
1521
|
-
* select('species', 'price'); // grouped
|
|
1522
|
-
* select('species', 'legs'); // grouped
|
|
1523
|
-
* // from pets
|
|
1524
|
-
* join('species', 'species');
|
|
1525
|
-
* group('price', 'avg').as('avgPrice');
|
|
1526
|
-
* group('legs', 'sum').as('sumLegs');
|
|
1527
|
-
* });
|
|
1528
|
-
*
|
|
1529
|
-
* queries.forEachResultRow('query', (rowId) => {
|
|
1530
|
-
* console.log({[rowId]: queries.getResultRow('query', rowId)});
|
|
1531
|
-
* });
|
|
1532
|
-
* // -> {0: {color: 'brown', owner: 'alice', avgPrice: 3, sumLegs: 4}}
|
|
1533
|
-
* // -> {1: {color: 'black', owner: 'bob', avgPrice: 4, sumLegs: 10}}
|
|
1534
|
-
* // -> {2: {color: 'red', owner: 'alice', avgPrice: 3, sumLegs: 2}}
|
|
1535
|
-
* ```
|
|
1536
|
-
* @example
|
|
1537
|
-
* This example shows a query that calculates the a custom aggregate of one
|
|
1538
|
-
* Cell's values, grouped by another. Note how `aggregateAdd`,
|
|
1539
|
-
* `aggregateRemove`, and `aggregateReplace` parameters are provided to make the
|
|
1540
|
-
* custom aggregation more efficient as individual values are added, removed, or
|
|
1541
|
-
* replaced during the lifecycle of the Table.
|
|
1542
|
-
*
|
|
1543
|
-
* ```js
|
|
1544
|
-
* const store = createStore()
|
|
1545
|
-
* .setTable('pets', {
|
|
1546
1503
|
* fido: {species: 'dog', owner: 'alice'},
|
|
1547
1504
|
* felix: {species: 'cat', owner: 'bob'},
|
|
1548
1505
|
* cujo: {species: 'dog', owner: 'bob'},
|
|
@@ -1572,8 +1529,8 @@ export type Where<
|
|
|
1572
1529
|
* remove == current
|
|
1573
1530
|
* ? undefined
|
|
1574
1531
|
* : add > 2
|
|
1575
|
-
*
|
|
1576
|
-
*
|
|
1532
|
+
* ? Math.min(current, add)
|
|
1533
|
+
* : current,
|
|
1577
1534
|
* ).as('lowestPriceOver2');
|
|
1578
1535
|
* });
|
|
1579
1536
|
*
|
|
@@ -304,10 +304,10 @@ export type Cell<
|
|
|
304
304
|
> = CellType extends 'string'
|
|
305
305
|
? string
|
|
306
306
|
: CellType extends 'number'
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
307
|
+
? number
|
|
308
|
+
: CellType extends 'boolean'
|
|
309
|
+
? boolean
|
|
310
|
+
: string | number | boolean;
|
|
311
311
|
|
|
312
312
|
/**
|
|
313
313
|
* The CellOrUndefined type is a data structure representing the data in a
|
|
@@ -400,10 +400,10 @@ export type Value<
|
|
|
400
400
|
> = ValueType extends 'string'
|
|
401
401
|
? string
|
|
402
402
|
: ValueType extends 'number'
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
403
|
+
? number
|
|
404
|
+
: ValueType extends 'boolean'
|
|
405
|
+
? boolean
|
|
406
|
+
: string | number | boolean;
|
|
407
407
|
|
|
408
408
|
/**
|
|
409
409
|
* The ValueOrUndefined type is a data structure representing the data in a
|