tinybase 6.1.0-beta.1 → 6.1.0-beta.3
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/@types/common/index.d.ts +4 -4
- package/@types/common/with-schemas/index.d.ts +4 -4
- package/@types/mergeable-store/index.d.ts +45 -20
- package/@types/mergeable-store/with-schemas/index.d.ts +49 -21
- package/@types/persisters/index.d.ts +1 -1
- package/@types/persisters/with-schemas/index.d.ts +1 -1
- package/index.js +19 -11
- package/indexes/index.js +15 -3
- package/indexes/with-schemas/index.js +15 -3
- package/mergeable-store/index.js +4 -8
- package/mergeable-store/with-schemas/index.js +4 -8
- package/min/index.js +1 -1
- package/min/index.js.gz +0 -0
- package/min/indexes/index.js +1 -1
- package/min/indexes/index.js.gz +0 -0
- package/min/indexes/with-schemas/index.js +1 -1
- package/min/indexes/with-schemas/index.js.gz +0 -0
- package/min/mergeable-store/index.js +1 -1
- package/min/mergeable-store/index.js.gz +0 -0
- package/min/mergeable-store/with-schemas/index.js +1 -1
- package/min/mergeable-store/with-schemas/index.js.gz +0 -0
- package/min/with-schemas/index.js +1 -1
- package/min/with-schemas/index.js.gz +0 -0
- package/package.json +9 -9
- package/readme.md +2 -2
- package/with-schemas/index.js +19 -11
- package/@types/_internal/queries/index.d.cts +0 -0
- package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
- package/@types/_internal/store/index.d.cts +0 -3
- package/@types/_internal/store/with-schemas/index.d.cts +0 -106
- package/@types/_internal/ui-react/index.d.cts +0 -0
- package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
- package/@types/checkpoints/index.d.cts +0 -1059
- package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
- package/@types/common/index.d.cts +0 -158
- package/@types/common/with-schemas/index.d.cts +0 -158
- package/@types/index.d.cts +0 -17
- package/@types/indexes/index.d.cts +0 -1064
- package/@types/indexes/with-schemas/index.d.cts +0 -1210
- package/@types/mergeable-store/index.d.cts +0 -1139
- package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
- package/@types/metrics/index.d.cts +0 -917
- package/@types/metrics/with-schemas/index.d.cts +0 -1004
- package/@types/persisters/index.d.cts +0 -1877
- package/@types/persisters/persister-automerge/index.d.cts +0 -165
- package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
- package/@types/persisters/persister-browser/index.d.cts +0 -185
- package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
- package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
- package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
- package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
- package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
- package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
- package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
- package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
- package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
- package/@types/persisters/persister-file/index.d.cts +0 -94
- package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
- package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
- package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
- package/@types/persisters/persister-libsql/index.d.cts +0 -158
- package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
- package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
- package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
- package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
- package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
- package/@types/persisters/persister-pglite/index.d.cts +0 -177
- package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
- package/@types/persisters/persister-postgres/index.d.cts +0 -166
- package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
- package/@types/persisters/persister-powersync/index.d.cts +0 -174
- package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
- package/@types/persisters/persister-remote/index.d.cts +0 -117
- package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
- package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
- package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
- package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
- package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
- package/@types/persisters/persister-yjs/index.d.cts +0 -161
- package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
- package/@types/persisters/with-schemas/index.d.cts +0 -2054
- package/@types/queries/index.d.cts +0 -3695
- package/@types/queries/with-schemas/index.d.cts +0 -4016
- package/@types/relationships/index.d.cts +0 -1320
- package/@types/relationships/with-schemas/index.d.cts +0 -1474
- package/@types/store/index.d.cts +0 -7598
- package/@types/store/with-schemas/index.d.cts +0 -9278
- package/@types/synchronizers/index.d.cts +0 -485
- package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
- package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
- package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
- package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
- package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
- package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
- package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
- package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
- package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
- package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
- package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
- package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
- package/@types/synchronizers/with-schemas/index.d.cts +0 -503
- package/@types/ui-react/index.d.cts +0 -16640
- package/@types/ui-react/with-schemas/index.d.cts +0 -17281
- package/@types/ui-react-dom/index.d.cts +0 -1862
- package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
- package/@types/ui-react-inspector/index.d.cts +0 -79
- package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
- package/@types/with-schemas/index.d.cts +0 -17
|
@@ -1,133 +0,0 @@
|
|
|
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
|
-
* @see Persistence guides
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
* @module persister-remote
|
|
7
|
-
* @since v1.0.0
|
|
8
|
-
*/
|
|
9
|
-
import type {
|
|
10
|
-
OptionalSchemas,
|
|
11
|
-
Store,
|
|
12
|
-
} from '../../../store/with-schemas/index.d.cts';
|
|
13
|
-
import type {Persister} from '../../with-schemas/index.d.cts';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The RemotePersister interface represents a Persister that lets you save and
|
|
17
|
-
* load Store data to and from a remote server.
|
|
18
|
-
*
|
|
19
|
-
* You should use the createRemotePersister function to create a RemotePersister
|
|
20
|
-
* object.
|
|
21
|
-
*
|
|
22
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
23
|
-
* extra getUrls method for accessing the URLs the Store is being persisted to.
|
|
24
|
-
* @category Persister
|
|
25
|
-
* @since v4.3.14
|
|
26
|
-
*/
|
|
27
|
-
export interface RemotePersister<Schemas extends OptionalSchemas>
|
|
28
|
-
extends Persister<Schemas> {
|
|
29
|
-
/**
|
|
30
|
-
* The getUrls method returns the URLs the Store is being persisted to.
|
|
31
|
-
* @returns The load and save URLs as a two-item array.
|
|
32
|
-
* @example
|
|
33
|
-
* This example creates a RemotePersister object against a newly-created Store
|
|
34
|
-
* and then gets the URLs back out again.
|
|
35
|
-
*
|
|
36
|
-
* ```js
|
|
37
|
-
* import {createStore} from 'tinybase';
|
|
38
|
-
* import {createRemotePersister} from 'tinybase/persisters/persister-remote';
|
|
39
|
-
*
|
|
40
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
41
|
-
* const persister = createRemotePersister(
|
|
42
|
-
* store,
|
|
43
|
-
* 'https://example.com/load',
|
|
44
|
-
* 'https://example.com/save',
|
|
45
|
-
* 5,
|
|
46
|
-
* );
|
|
47
|
-
*
|
|
48
|
-
* console.log(persister.getUrls());
|
|
49
|
-
* // -> ['https://example.com/load', 'https://example.com/save']
|
|
50
|
-
*
|
|
51
|
-
* persister.destroy();
|
|
52
|
-
* ```
|
|
53
|
-
* @category Getter
|
|
54
|
-
* @since v4.3.14
|
|
55
|
-
*/
|
|
56
|
-
getUrls(): [string, string];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The createRemotePersister function creates a RemotePersister object that can
|
|
61
|
-
* persist the Store to a remote server.
|
|
62
|
-
*
|
|
63
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
64
|
-
*
|
|
65
|
-
* ```ts override
|
|
66
|
-
* createRemotePersister(
|
|
67
|
-
* store: Store,
|
|
68
|
-
* loadUrl: string,
|
|
69
|
-
* saveUrl: string,
|
|
70
|
-
* autoLoadIntervalSeconds?: number,
|
|
71
|
-
* onIgnoredError?: (error: any) => void,
|
|
72
|
-
* ): RemotePersister;
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* A RemotePersister only supports regular Store objects, and cannot be used to
|
|
76
|
-
* persist the metadata of a MergeableStore.
|
|
77
|
-
*
|
|
78
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
79
|
-
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
80
|
-
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
81
|
-
* the `POST` method (to send the Store JSON to save) respectively.
|
|
82
|
-
*
|
|
83
|
-
* For when you choose to enable automatic loading for the Persister (with the
|
|
84
|
-
* startAutoLoad method), it will poll the loadUrl for changes, using the `ETag`
|
|
85
|
-
* HTTP header to identify if things have changed. The `autoLoadIntervalSeconds`
|
|
86
|
-
* method is used to indicate how often to do this.
|
|
87
|
-
*
|
|
88
|
-
* If you are implementing the server portion of this functionality yourself,
|
|
89
|
-
* remember to ensure that the `ETag` header changes every time the server's
|
|
90
|
-
* Store content does - otherwise changes will not be detected by the client.
|
|
91
|
-
* @param store The Store to persist.
|
|
92
|
-
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
93
|
-
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
94
|
-
* @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
|
|
95
|
-
* automatically loading changes from the server, defaulting to 5.
|
|
96
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
97
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
98
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
99
|
-
* @returns A reference to the new RemotePersister object.
|
|
100
|
-
* @example
|
|
101
|
-
* This example creates a RemotePersister object and persists the Store to a
|
|
102
|
-
* remote server.
|
|
103
|
-
*
|
|
104
|
-
* ```js yolo
|
|
105
|
-
* import {createStore} from 'tinybase';
|
|
106
|
-
* import {createRemotePersister} from 'tinybase/persisters/persister-remote';
|
|
107
|
-
*
|
|
108
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
109
|
-
* const persister = createRemotePersister(
|
|
110
|
-
* store,
|
|
111
|
-
* 'https://example.com/load',
|
|
112
|
-
* 'https://example.com/save',
|
|
113
|
-
* 5,
|
|
114
|
-
* );
|
|
115
|
-
*
|
|
116
|
-
* await persister.save();
|
|
117
|
-
* // Store JSON will be sent to server in a POST request.
|
|
118
|
-
*
|
|
119
|
-
* await persister.load();
|
|
120
|
-
* // Store JSON will be fetched from server with a GET request.
|
|
121
|
-
*
|
|
122
|
-
* persister.destroy();
|
|
123
|
-
* ```
|
|
124
|
-
* @category Creation
|
|
125
|
-
* @since v1.0.0
|
|
126
|
-
*/
|
|
127
|
-
export function createRemotePersister<Schemas extends OptionalSchemas>(
|
|
128
|
-
store: Store<Schemas>,
|
|
129
|
-
loadUrl: string,
|
|
130
|
-
saveUrl: string,
|
|
131
|
-
autoLoadIntervalSeconds?: number,
|
|
132
|
-
onIgnoredError?: (error: any) => void,
|
|
133
|
-
): RemotePersister<Schemas>;
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-sqlite-wasm module of the TinyBase project lets you save and
|
|
3
|
-
* load Store data to and from a local SQLite database (in an appropriate
|
|
4
|
-
* environment).
|
|
5
|
-
* @see Database Persistence guide
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
* @module persister-sqlite-wasm
|
|
8
|
-
* @since v4.0.0
|
|
9
|
-
*/
|
|
10
|
-
import type {MergeableStore} from '../../mergeable-store/index.d.cts';
|
|
11
|
-
import type {Store} from '../../store/index.d.cts';
|
|
12
|
-
import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The SqliteWasmPersister interface represents a Persister that lets you save
|
|
16
|
-
* and load Store data to and from a local SQLite database.
|
|
17
|
-
*
|
|
18
|
-
* You should use the createSqliteWasmPersister function to create a
|
|
19
|
-
* SqliteWasmPersister object.
|
|
20
|
-
*
|
|
21
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
22
|
-
* extra getDb method for accessing a reference to the database instance the
|
|
23
|
-
* Store is being persisted to.
|
|
24
|
-
* @category Persister
|
|
25
|
-
* @since v4.3.14
|
|
26
|
-
*/
|
|
27
|
-
export interface SqliteWasmPersister
|
|
28
|
-
extends Persister<Persists.StoreOrMergeableStore> {
|
|
29
|
-
/**
|
|
30
|
-
* The getDb method returns a reference to the database instance the Store is
|
|
31
|
-
* being persisted to.
|
|
32
|
-
* @returns A reference to the database instance.
|
|
33
|
-
* @example
|
|
34
|
-
* This example creates a Persister object against a newly-created Store and
|
|
35
|
-
* then gets the database instance back out again.
|
|
36
|
-
*
|
|
37
|
-
* ```js
|
|
38
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
39
|
-
* import {createStore} from 'tinybase';
|
|
40
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
41
|
-
*
|
|
42
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
43
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
44
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
45
|
-
* const persister = createSqliteWasmPersister(
|
|
46
|
-
* store,
|
|
47
|
-
* sqlite3,
|
|
48
|
-
* db,
|
|
49
|
-
* 'my_tinybase',
|
|
50
|
-
* );
|
|
51
|
-
*
|
|
52
|
-
* console.log(persister.getDb() == db);
|
|
53
|
-
* // -> true
|
|
54
|
-
*
|
|
55
|
-
* persister.destroy();
|
|
56
|
-
* ```
|
|
57
|
-
* @category Getter
|
|
58
|
-
* @since v4.3.14
|
|
59
|
-
*/
|
|
60
|
-
getDb(): any;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The createSqliteWasmPersister function creates a SqliteWasmPersister object
|
|
65
|
-
* that can persist the Store to a local SQLite database.
|
|
66
|
-
*
|
|
67
|
-
* A SqliteWasmPersister supports regular Store objects, and can also be used to
|
|
68
|
-
* persist the metadata of a MergeableStore when using the JSON serialization
|
|
69
|
-
* mode, as described below.
|
|
70
|
-
*
|
|
71
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
72
|
-
* `sqlite3` and `db` parameters which identify the WASM module and database
|
|
73
|
-
* instance respectively.
|
|
74
|
-
*
|
|
75
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
76
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
77
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
78
|
-
*
|
|
79
|
-
* The fourth argument is a DatabasePersisterConfig object that configures which
|
|
80
|
-
* of those modes to use, and settings for each. If the fourth argument is
|
|
81
|
-
* simply a string, it is used as the `storeTableName` property of the JSON
|
|
82
|
-
* serialization.
|
|
83
|
-
*
|
|
84
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
85
|
-
* information on how both of those modes can be configured.
|
|
86
|
-
* @param store The Store or MergeableStore to persist.
|
|
87
|
-
* @param sqlite3 The WASM module that was returned from `sqlite3InitModule`.
|
|
88
|
-
* @param db The database instance that was returned from `new
|
|
89
|
-
* sqlite3.oo1.DB(...)`.
|
|
90
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
91
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
92
|
-
* JSON serialization).
|
|
93
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
94
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
95
|
-
* behavior in a development environment, since v4.0.4.
|
|
96
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
97
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
98
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
99
|
-
* @returns A reference to the new SqliteWasmPersister object.
|
|
100
|
-
* @example
|
|
101
|
-
* This example creates a SqliteWasmPersister object and persists the Store to a
|
|
102
|
-
* local SQLite database as a JSON serialization into the `my_tinybase` table.
|
|
103
|
-
* It makes a change to the database directly and then reloads it back into the
|
|
104
|
-
* Store.
|
|
105
|
-
*
|
|
106
|
-
* ```js
|
|
107
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
108
|
-
* import {createStore} from 'tinybase';
|
|
109
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
110
|
-
*
|
|
111
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
112
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
113
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
114
|
-
* const persister = createSqliteWasmPersister(
|
|
115
|
-
* store,
|
|
116
|
-
* sqlite3,
|
|
117
|
-
* db,
|
|
118
|
-
* 'my_tinybase',
|
|
119
|
-
* );
|
|
120
|
-
*
|
|
121
|
-
* await persister.save();
|
|
122
|
-
* // Store will be saved to the database.
|
|
123
|
-
*
|
|
124
|
-
* console.log(db.exec('SELECT * FROM my_tinybase;', {rowMode: 'object'}));
|
|
125
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
126
|
-
*
|
|
127
|
-
* db.exec(
|
|
128
|
-
* 'UPDATE my_tinybase SET store = ' +
|
|
129
|
-
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
130
|
-
* );
|
|
131
|
-
* await persister.load();
|
|
132
|
-
* console.log(store.getTables());
|
|
133
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
134
|
-
*
|
|
135
|
-
* persister.destroy();
|
|
136
|
-
* ```
|
|
137
|
-
* @example
|
|
138
|
-
* This example creates a SqliteWasmPersister object and persists the Store to a
|
|
139
|
-
* local SQLite database with tabular mapping.
|
|
140
|
-
*
|
|
141
|
-
* ```js
|
|
142
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
143
|
-
* import {createStore} from 'tinybase';
|
|
144
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
145
|
-
*
|
|
146
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
147
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
148
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
149
|
-
* const persister = createSqliteWasmPersister(store, sqlite3, db, {
|
|
150
|
-
* mode: 'tabular',
|
|
151
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
152
|
-
* });
|
|
153
|
-
*
|
|
154
|
-
* await persister.save();
|
|
155
|
-
* console.log(db.exec('SELECT * FROM pets;', {rowMode: 'object'}));
|
|
156
|
-
* // -> [{_id: 'fido', species: 'dog'}]
|
|
157
|
-
*
|
|
158
|
-
* db.exec(`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`);
|
|
159
|
-
* await persister.load();
|
|
160
|
-
* console.log(store.getTables());
|
|
161
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
162
|
-
*
|
|
163
|
-
* persister.destroy();
|
|
164
|
-
* ```
|
|
165
|
-
* @category Creation
|
|
166
|
-
* @since v4.0.0
|
|
167
|
-
*/
|
|
168
|
-
export function createSqliteWasmPersister(
|
|
169
|
-
store: Store | MergeableStore,
|
|
170
|
-
sqlite3: any,
|
|
171
|
-
db: any,
|
|
172
|
-
configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
173
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
174
|
-
onIgnoredError?: (error: any) => void,
|
|
175
|
-
): SqliteWasmPersister;
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-sqlite-wasm module of the TinyBase project lets you save and
|
|
3
|
-
* load Store data to and from a local SQLite database (in an appropriate
|
|
4
|
-
* environment).
|
|
5
|
-
* @see Database Persistence guide
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
* @module persister-sqlite-wasm
|
|
8
|
-
* @since v4.0.0
|
|
9
|
-
*/
|
|
10
|
-
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
|
|
11
|
-
import type {
|
|
12
|
-
OptionalSchemas,
|
|
13
|
-
Store,
|
|
14
|
-
} from '../../../store/with-schemas/index.d.cts';
|
|
15
|
-
import type {
|
|
16
|
-
DatabasePersisterConfig,
|
|
17
|
-
Persister,
|
|
18
|
-
Persists,
|
|
19
|
-
} from '../../with-schemas/index.d.cts';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The SqliteWasmPersister interface represents a Persister that lets you save
|
|
23
|
-
* and load Store data to and from a local SQLite database.
|
|
24
|
-
*
|
|
25
|
-
* You should use the createSqliteWasmPersister function to create a
|
|
26
|
-
* SqliteWasmPersister object.
|
|
27
|
-
*
|
|
28
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
29
|
-
* extra getDb method for accessing a reference to the database instance the
|
|
30
|
-
* Store is being persisted to.
|
|
31
|
-
* @category Persister
|
|
32
|
-
* @since v4.3.14
|
|
33
|
-
*/
|
|
34
|
-
export interface SqliteWasmPersister<Schemas extends OptionalSchemas>
|
|
35
|
-
extends Persister<Schemas, Persists.StoreOrMergeableStore> {
|
|
36
|
-
/**
|
|
37
|
-
* The getDb method returns a reference to the database instance the Store is
|
|
38
|
-
* being persisted to.
|
|
39
|
-
* @returns A reference to the database instance.
|
|
40
|
-
* @example
|
|
41
|
-
* This example creates a Persister object against a newly-created Store and
|
|
42
|
-
* then gets the database instance back out again.
|
|
43
|
-
*
|
|
44
|
-
* ```js
|
|
45
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
46
|
-
* import {createStore} from 'tinybase';
|
|
47
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
48
|
-
*
|
|
49
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
50
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
51
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
52
|
-
* const persister = createSqliteWasmPersister(
|
|
53
|
-
* store,
|
|
54
|
-
* sqlite3,
|
|
55
|
-
* db,
|
|
56
|
-
* 'my_tinybase',
|
|
57
|
-
* );
|
|
58
|
-
*
|
|
59
|
-
* console.log(persister.getDb() == db);
|
|
60
|
-
* // -> true
|
|
61
|
-
*
|
|
62
|
-
* persister.destroy();
|
|
63
|
-
* ```
|
|
64
|
-
* @category Getter
|
|
65
|
-
* @since v4.3.14
|
|
66
|
-
*/
|
|
67
|
-
getDb(): any;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The createSqliteWasmPersister function creates a SqliteWasmPersister object
|
|
72
|
-
* that can persist the Store to a local SQLite database.
|
|
73
|
-
*
|
|
74
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
75
|
-
*
|
|
76
|
-
* ```ts override
|
|
77
|
-
* createSqliteWasmPersister(
|
|
78
|
-
* store: Store | MergeableStore,
|
|
79
|
-
* sqlite3: any,
|
|
80
|
-
* db: any,
|
|
81
|
-
* configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
82
|
-
* onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
83
|
-
* onIgnoredError?: (error: any) => void,
|
|
84
|
-
* ): SqliteWasmPersister;
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* A SqliteWasmPersister supports regular Store objects, and can also be used to
|
|
88
|
-
* persist the metadata of a MergeableStore when using the JSON serialization
|
|
89
|
-
* mode, as described below.
|
|
90
|
-
*
|
|
91
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
92
|
-
* `sqlite3` and `db` parameters which identify the WASM module and database
|
|
93
|
-
* instance respectively.
|
|
94
|
-
*
|
|
95
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
96
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
97
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
98
|
-
*
|
|
99
|
-
* The fourth argument is a DatabasePersisterConfig object that configures which
|
|
100
|
-
* of those modes to use, and settings for each. If the fourth argument is
|
|
101
|
-
* simply a string, it is used as the `storeTableName` property of the JSON
|
|
102
|
-
* serialization.
|
|
103
|
-
*
|
|
104
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
105
|
-
* information on how both of those modes can be configured.
|
|
106
|
-
* @param store The Store or MergeableStore to persist.
|
|
107
|
-
* @param sqlite3 The WASM module that was returned from `sqlite3InitModule`.
|
|
108
|
-
* @param db The database instance that was returned from `new
|
|
109
|
-
* sqlite3.oo1.DB(...)`.
|
|
110
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
111
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
112
|
-
* JSON serialization).
|
|
113
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
114
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
115
|
-
* behavior in a development environment, since v4.0.4.
|
|
116
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
117
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
118
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
119
|
-
* @returns A reference to the new SqliteWasmPersister object.
|
|
120
|
-
* @example
|
|
121
|
-
* This example creates a SqliteWasmPersister object and persists the Store to a
|
|
122
|
-
* local SQLite database as a JSON serialization into the `my_tinybase` table.
|
|
123
|
-
* It makes a change to the database directly and then reloads it back into the
|
|
124
|
-
* Store.
|
|
125
|
-
*
|
|
126
|
-
* ```js
|
|
127
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
128
|
-
* import {createStore} from 'tinybase';
|
|
129
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
130
|
-
*
|
|
131
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
132
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
133
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
134
|
-
* const persister = createSqliteWasmPersister(
|
|
135
|
-
* store,
|
|
136
|
-
* sqlite3,
|
|
137
|
-
* db,
|
|
138
|
-
* 'my_tinybase',
|
|
139
|
-
* );
|
|
140
|
-
*
|
|
141
|
-
* await persister.save();
|
|
142
|
-
* // Store will be saved to the database.
|
|
143
|
-
*
|
|
144
|
-
* console.log(db.exec('SELECT * FROM my_tinybase;', {rowMode: 'object'}));
|
|
145
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
146
|
-
*
|
|
147
|
-
* db.exec(
|
|
148
|
-
* 'UPDATE my_tinybase SET store = ' +
|
|
149
|
-
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
150
|
-
* );
|
|
151
|
-
* await persister.load();
|
|
152
|
-
* console.log(store.getTables());
|
|
153
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
154
|
-
*
|
|
155
|
-
* persister.destroy();
|
|
156
|
-
* ```
|
|
157
|
-
* @example
|
|
158
|
-
* This example creates a SqliteWasmPersister object and persists the Store to a
|
|
159
|
-
* local SQLite database with tabular mapping.
|
|
160
|
-
*
|
|
161
|
-
* ```js
|
|
162
|
-
* import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
163
|
-
* import {createStore} from 'tinybase';
|
|
164
|
-
* import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
|
|
165
|
-
*
|
|
166
|
-
* const sqlite3 = await sqlite3InitModule();
|
|
167
|
-
* const db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
168
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
169
|
-
* const persister = createSqliteWasmPersister(store, sqlite3, db, {
|
|
170
|
-
* mode: 'tabular',
|
|
171
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
172
|
-
* });
|
|
173
|
-
*
|
|
174
|
-
* await persister.save();
|
|
175
|
-
* console.log(db.exec('SELECT * FROM pets;', {rowMode: 'object'}));
|
|
176
|
-
* // -> [{_id: 'fido', species: 'dog'}]
|
|
177
|
-
*
|
|
178
|
-
* db.exec(`INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`);
|
|
179
|
-
* await persister.load();
|
|
180
|
-
* console.log(store.getTables());
|
|
181
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
182
|
-
*
|
|
183
|
-
* persister.destroy();
|
|
184
|
-
* ```
|
|
185
|
-
* @category Creation
|
|
186
|
-
* @since v4.0.0
|
|
187
|
-
*/
|
|
188
|
-
export function createSqliteWasmPersister<Schemas extends OptionalSchemas>(
|
|
189
|
-
store: Store<Schemas> | MergeableStore<Schemas>,
|
|
190
|
-
sqlite3: any,
|
|
191
|
-
db: any,
|
|
192
|
-
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
193
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
194
|
-
onIgnoredError?: (error: any) => void,
|
|
195
|
-
): SqliteWasmPersister<Schemas>;
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-sqlite3 module of the TinyBase project lets you save and load
|
|
3
|
-
* Store data to and from a local SQLite database (in an appropriate
|
|
4
|
-
* environment).
|
|
5
|
-
* @see Database Persistence guide
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
* @module persister-sqlite3
|
|
8
|
-
* @since v4.0.0
|
|
9
|
-
*/
|
|
10
|
-
import type {Database} from 'sqlite3';
|
|
11
|
-
import type {MergeableStore} from '../../mergeable-store/index.d.cts';
|
|
12
|
-
import type {Store} from '../../store/index.d.cts';
|
|
13
|
-
import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The Sqlite3Persister interface represents a Persister that lets you save and
|
|
17
|
-
* load Store data to and from a local SQLite database.
|
|
18
|
-
*
|
|
19
|
-
* You should use the createSqlite3Persister function to create a
|
|
20
|
-
* Sqlite3Persister object.
|
|
21
|
-
*
|
|
22
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
23
|
-
* extra getDb method for accessing a reference to the database instance the
|
|
24
|
-
* Store is being persisted to.
|
|
25
|
-
* @category Persister
|
|
26
|
-
* @since v4.3.14
|
|
27
|
-
*/
|
|
28
|
-
export interface Sqlite3Persister
|
|
29
|
-
extends Persister<Persists.StoreOrMergeableStore> {
|
|
30
|
-
/**
|
|
31
|
-
* The getDb method returns a reference to the database instance the Store is
|
|
32
|
-
* being persisted to.
|
|
33
|
-
* @returns A reference to the database instance.
|
|
34
|
-
* @example
|
|
35
|
-
* This example creates a Persister object against a newly-created Store and
|
|
36
|
-
* then gets the database instance back out again.
|
|
37
|
-
*
|
|
38
|
-
* ```js
|
|
39
|
-
* import {Database} from 'sqlite3';
|
|
40
|
-
* import {createStore} from 'tinybase';
|
|
41
|
-
* import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
|
|
42
|
-
*
|
|
43
|
-
* const db = new Database(':memory:');
|
|
44
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
45
|
-
* const persister = createSqlite3Persister(store, db, 'my_tinybase');
|
|
46
|
-
*
|
|
47
|
-
* console.log(persister.getDb() == db);
|
|
48
|
-
* // -> true
|
|
49
|
-
*
|
|
50
|
-
* persister.destroy();
|
|
51
|
-
* ```
|
|
52
|
-
* @category Getter
|
|
53
|
-
* @since v4.3.14
|
|
54
|
-
*/
|
|
55
|
-
getDb(): Database;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The createSqlite3Persister function creates a Sqlite3Persister object that
|
|
60
|
-
* can persist the Store to a local SQLite database.
|
|
61
|
-
*
|
|
62
|
-
* A Sqlite3Persister supports regular Store objects, and can also be used to
|
|
63
|
-
* persist the metadata of a MergeableStore when using the JSON serialization
|
|
64
|
-
* mode, as described below.
|
|
65
|
-
*
|
|
66
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
67
|
-
* `db` parameter which identifies the database instance.
|
|
68
|
-
*
|
|
69
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
70
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
71
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
72
|
-
*
|
|
73
|
-
* The third argument is a DatabasePersisterConfig object that configures which
|
|
74
|
-
* of those modes to use, and settings for each. If the third argument is simply
|
|
75
|
-
* a string, it is used as the `storeTableName` property of the JSON
|
|
76
|
-
* serialization.
|
|
77
|
-
*
|
|
78
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
79
|
-
* information on how both of those modes can be configured.
|
|
80
|
-
* @param store The Store or MergeableStore to persist.
|
|
81
|
-
* @param db The database instance that was returned from `new
|
|
82
|
-
* sqlite3.Database(...)`.
|
|
83
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
84
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
85
|
-
* JSON serialization).
|
|
86
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
87
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
88
|
-
* behavior in a development environment, since v4.0.4.
|
|
89
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
90
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
91
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
92
|
-
* @returns A reference to the new Sqlite3Persister object.
|
|
93
|
-
* @example
|
|
94
|
-
* This example creates a Sqlite3Persister object and persists the Store to a
|
|
95
|
-
* local SQLite database as a JSON serialization into the `my_tinybase` table.
|
|
96
|
-
* It makes a change to the database directly and then reloads it back into the
|
|
97
|
-
* Store.
|
|
98
|
-
*
|
|
99
|
-
* ```js
|
|
100
|
-
* import {Database} from 'sqlite3';
|
|
101
|
-
* import {createStore} from 'tinybase';
|
|
102
|
-
* import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
|
|
103
|
-
*
|
|
104
|
-
* const db = new Database(':memory:');
|
|
105
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
106
|
-
* const persister = createSqlite3Persister(store, db, 'my_tinybase');
|
|
107
|
-
*
|
|
108
|
-
* await persister.save();
|
|
109
|
-
* // Store will be saved to the database.
|
|
110
|
-
*
|
|
111
|
-
* console.log(
|
|
112
|
-
* await new Promise((resolve) =>
|
|
113
|
-
* db.all('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)),
|
|
114
|
-
* ),
|
|
115
|
-
* );
|
|
116
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
117
|
-
*
|
|
118
|
-
* await new Promise((resolve) =>
|
|
119
|
-
* db.all(
|
|
120
|
-
* 'UPDATE my_tinybase SET store = ' +
|
|
121
|
-
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
122
|
-
* resolve,
|
|
123
|
-
* ),
|
|
124
|
-
* );
|
|
125
|
-
* await persister.load();
|
|
126
|
-
* console.log(store.getTables());
|
|
127
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
128
|
-
*
|
|
129
|
-
* persister.destroy();
|
|
130
|
-
* ```
|
|
131
|
-
* @example
|
|
132
|
-
* This example creates a Sqlite3Persister object and persists the Store to a
|
|
133
|
-
* local SQLite database with tabular mapping.
|
|
134
|
-
*
|
|
135
|
-
* ```js
|
|
136
|
-
* import {Database} from 'sqlite3';
|
|
137
|
-
* import {createStore} from 'tinybase';
|
|
138
|
-
* import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
|
|
139
|
-
*
|
|
140
|
-
* const db = new Database(':memory:');
|
|
141
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
142
|
-
* const persister = createSqlite3Persister(store, db, {
|
|
143
|
-
* mode: 'tabular',
|
|
144
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
145
|
-
* });
|
|
146
|
-
*
|
|
147
|
-
* await persister.save();
|
|
148
|
-
* console.log(
|
|
149
|
-
* await new Promise((resolve) =>
|
|
150
|
-
* db.all('SELECT * FROM pets;', (_, rows) => resolve(rows)),
|
|
151
|
-
* ),
|
|
152
|
-
* );
|
|
153
|
-
* // -> [{_id: 'fido', species: 'dog'}]
|
|
154
|
-
*
|
|
155
|
-
* await new Promise((resolve) =>
|
|
156
|
-
* db.all(
|
|
157
|
-
* `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
|
|
158
|
-
* resolve,
|
|
159
|
-
* ),
|
|
160
|
-
* );
|
|
161
|
-
* await persister.load();
|
|
162
|
-
* console.log(store.getTables());
|
|
163
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
164
|
-
*
|
|
165
|
-
* persister.destroy();
|
|
166
|
-
* ```
|
|
167
|
-
* @category Creation
|
|
168
|
-
* @since v4.0.0
|
|
169
|
-
*/
|
|
170
|
-
export function createSqlite3Persister(
|
|
171
|
-
store: Store | MergeableStore,
|
|
172
|
-
db: Database,
|
|
173
|
-
configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
174
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
175
|
-
onIgnoredError?: (error: any) => void,
|
|
176
|
-
): Sqlite3Persister;
|