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,185 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-postgres module of the TinyBase project lets you save and load
|
|
3
|
-
* Store data to and from a PostgreSQL database (in an appropriate environment).
|
|
4
|
-
* @see Database Persistence guide
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
* @module persister-postgres
|
|
7
|
-
* @since 5.2.0
|
|
8
|
-
*/
|
|
9
|
-
import type {Sql} from 'postgres';
|
|
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 PostgresPersister interface represents a Persister that lets you save and
|
|
23
|
-
* load Store data to and from a local PostgreSQL database.
|
|
24
|
-
*
|
|
25
|
-
* You should use the createPostgresPersister function to create a
|
|
26
|
-
* PostgresPersister object.
|
|
27
|
-
*
|
|
28
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
29
|
-
* extra getSql method for accessing a reference to the database connection the
|
|
30
|
-
* Store is being persisted to.
|
|
31
|
-
* @category Persister
|
|
32
|
-
* @since 5.2.0
|
|
33
|
-
*/
|
|
34
|
-
export interface PostgresPersister<Schemas extends OptionalSchemas>
|
|
35
|
-
extends Persister<Schemas, Persists.StoreOrMergeableStore> {
|
|
36
|
-
/**
|
|
37
|
-
* The getSql method returns a reference to the database connection the Store
|
|
38
|
-
* is being persisted to.
|
|
39
|
-
* @returns A reference to the database connection.
|
|
40
|
-
* @example
|
|
41
|
-
* This example creates a Persister object against a newly-created Store and
|
|
42
|
-
* then gets the database connection back out again.
|
|
43
|
-
*
|
|
44
|
-
* ```js
|
|
45
|
-
* import postgres from 'postgres';
|
|
46
|
-
* import {createStore} from 'tinybase';
|
|
47
|
-
* import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
|
|
48
|
-
*
|
|
49
|
-
* const sql = postgres('postgres://localhost:5432/tinybase');
|
|
50
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
51
|
-
* const persister = await createPostgresPersister(store, sql, 'my_tinybase');
|
|
52
|
-
*
|
|
53
|
-
* console.log(persister.getSql() == sql);
|
|
54
|
-
* // -> true
|
|
55
|
-
*
|
|
56
|
-
* persister.destroy();
|
|
57
|
-
* await sql.end();
|
|
58
|
-
* ```
|
|
59
|
-
* @category Getter
|
|
60
|
-
* @since 5.2.0
|
|
61
|
-
*/
|
|
62
|
-
getSql(): Sql;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* The createPostgresPersister function creates a PostgresPersister object that
|
|
67
|
-
* can persist the Store to a local PostgreSQL database.
|
|
68
|
-
*
|
|
69
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
70
|
-
*
|
|
71
|
-
* ```ts override
|
|
72
|
-
* createPostgresPersister(
|
|
73
|
-
* store: Store | MergeableStore,
|
|
74
|
-
* sql: Sql,
|
|
75
|
-
* configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
76
|
-
* onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
77
|
-
* onIgnoredError?: (error: any) => void,
|
|
78
|
-
* ): Promise<PostgresPersister>;
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* A PostgresPersister supports regular Store objects, and can also be used to
|
|
82
|
-
* persist the metadata of a MergeableStore when using the JSON serialization
|
|
83
|
-
* mode, as described below.
|
|
84
|
-
*
|
|
85
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
86
|
-
* `sql` parameter which identifies the database connection.
|
|
87
|
-
*
|
|
88
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
89
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
90
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
91
|
-
*
|
|
92
|
-
* The third argument is a DatabasePersisterConfig object that configures which
|
|
93
|
-
* of those modes to use, and settings for each. If the third argument is simply
|
|
94
|
-
* a string, it is used as the `storeTableName` property of the JSON
|
|
95
|
-
* serialization.
|
|
96
|
-
*
|
|
97
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
98
|
-
* information on how both of those modes can be configured.
|
|
99
|
-
*
|
|
100
|
-
* This method is asynchronous because it will await the creation of dedicated
|
|
101
|
-
* new connections to the database. You will need to `await` a call to this
|
|
102
|
-
* function or handle the return type natively as a Promise.
|
|
103
|
-
* @param store The Store or MergeableStore to persist.
|
|
104
|
-
* @param sql The database connection that was returned from `postgres(...)`.
|
|
105
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
106
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
107
|
-
* JSON serialization).
|
|
108
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
109
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
110
|
-
* behavior in a development environment.
|
|
111
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
112
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
113
|
-
* debugging persistence issues in a development environment.
|
|
114
|
-
* @returns A reference to the new PostgresPersister object.
|
|
115
|
-
* @example
|
|
116
|
-
* This example creates a PostgresPersister object and persists the Store to a
|
|
117
|
-
* local PostgreSQL database as a JSON serialization into the `my_tinybase`
|
|
118
|
-
* table. It makes a change to the database directly and then reloads it back
|
|
119
|
-
* into the Store.
|
|
120
|
-
*
|
|
121
|
-
* ```js
|
|
122
|
-
* import postgres from 'postgres';
|
|
123
|
-
* import {createStore} from 'tinybase';
|
|
124
|
-
* import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
|
|
125
|
-
*
|
|
126
|
-
* const sql = postgres('postgres://localhost:5432/tinybase');
|
|
127
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
128
|
-
* const persister = await createPostgresPersister(store, sql, 'my_tinybase');
|
|
129
|
-
*
|
|
130
|
-
* await persister.save();
|
|
131
|
-
* // Store will be saved to the database.
|
|
132
|
-
*
|
|
133
|
-
* console.log(await sql`SELECT * FROM my_tinybase;`);
|
|
134
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
135
|
-
*
|
|
136
|
-
* const json = '[{"pets":{"felix":{"species":"cat"}}},{}]';
|
|
137
|
-
* await sql`UPDATE my_tinybase SET store = ${json} WHERE _id = '_';`;
|
|
138
|
-
*
|
|
139
|
-
* await persister.load();
|
|
140
|
-
* console.log(store.getTables());
|
|
141
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
142
|
-
*
|
|
143
|
-
* persister.destroy();
|
|
144
|
-
* await sql.end();
|
|
145
|
-
* ```
|
|
146
|
-
* @example
|
|
147
|
-
* This example creates a PostgresPersister object and persists the Store to a
|
|
148
|
-
* local PostgreSQL database with tabular mapping.
|
|
149
|
-
*
|
|
150
|
-
* ```js
|
|
151
|
-
* import postgres from 'postgres';
|
|
152
|
-
* import {createStore} from 'tinybase';
|
|
153
|
-
* import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
|
|
154
|
-
*
|
|
155
|
-
* const sql = postgres('postgres://localhost:5432/tinybase');
|
|
156
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
157
|
-
* const persister = await createPostgresPersister(store, sql, {
|
|
158
|
-
* mode: 'tabular',
|
|
159
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
160
|
-
* });
|
|
161
|
-
*
|
|
162
|
-
* await persister.save();
|
|
163
|
-
* console.log(await sql`SELECT * FROM pets;`);
|
|
164
|
-
* // -> [{_id: 'fido', species: '"dog"'}]
|
|
165
|
-
* // Note that Cells and Values are JSON-encoded in PostgreSQL databases.
|
|
166
|
-
*
|
|
167
|
-
* await sql`INSERT INTO pets (_id, species) VALUES ('felix', '"cat"')`;
|
|
168
|
-
* await persister.load();
|
|
169
|
-
* console.log(store.getTables());
|
|
170
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
171
|
-
*
|
|
172
|
-
* persister.destroy();
|
|
173
|
-
* await sql`DROP TABLE IF EXISTS pets`;
|
|
174
|
-
* await sql.end();
|
|
175
|
-
* ```
|
|
176
|
-
* @category Creation
|
|
177
|
-
* @since 5.2.0
|
|
178
|
-
*/
|
|
179
|
-
export function createPostgresPersister<Schemas extends OptionalSchemas>(
|
|
180
|
-
store: Store<Schemas> | MergeableStore<Schemas>,
|
|
181
|
-
sql: Sql,
|
|
182
|
-
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
183
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
184
|
-
onIgnoredError?: (error: any) => void,
|
|
185
|
-
): Promise<PostgresPersister<Schemas>>;
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-powersync module of the TinyBase project lets you save and load
|
|
3
|
-
* Store data to and from a local SQLite database that is automatically synced
|
|
4
|
-
* using the PowerSync service.
|
|
5
|
-
* @see Database Persistence guide
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
* @module persister-powersync
|
|
8
|
-
* @since v4.8.0
|
|
9
|
-
*/
|
|
10
|
-
import type {AbstractPowerSyncDatabase} from '@powersync/common';
|
|
11
|
-
import type {Store} from '../../store/index.d.cts';
|
|
12
|
-
import type {DatabasePersisterConfig, Persister} from '../index.d.cts';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The PowerSyncPersister interface represents a Persister that lets you save
|
|
16
|
-
* and load Store data to and from a local SQLite database that is automatically
|
|
17
|
-
* synced using the PowerSync service.
|
|
18
|
-
*
|
|
19
|
-
* You should use the createPowerSyncPersister function to create a
|
|
20
|
-
* PowerSyncPersister object.
|
|
21
|
-
*
|
|
22
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
23
|
-
* extra getPowerSync method for accessing a reference to the PowerSync instance
|
|
24
|
-
* the Store is being persisted to.
|
|
25
|
-
* @category Persister
|
|
26
|
-
* @since v4.8.0
|
|
27
|
-
*/
|
|
28
|
-
export interface PowerSyncPersister extends Persister {
|
|
29
|
-
/**
|
|
30
|
-
* The getPowerSync method returns a reference to the PowerSync instance the
|
|
31
|
-
* Store is being persisted to.
|
|
32
|
-
* @returns A reference to the PowerSync instance.
|
|
33
|
-
* @example
|
|
34
|
-
* This example creates a Persister object against a newly-created Store and
|
|
35
|
-
* then gets the PowerSync instance back out again.
|
|
36
|
-
*
|
|
37
|
-
* ```js yolo
|
|
38
|
-
* import {usePowerSync} from '@powersync/react';
|
|
39
|
-
* import {createStore} from 'tinybase';
|
|
40
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
41
|
-
*
|
|
42
|
-
* const ps = usePowerSync();
|
|
43
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
44
|
-
* const persister = createPowerSyncPersister(store, ps, 'my_tinybase');
|
|
45
|
-
*
|
|
46
|
-
* console.log(persister.getPowerSync() == ps);
|
|
47
|
-
* // -> true
|
|
48
|
-
*
|
|
49
|
-
* persister.destroy();
|
|
50
|
-
* ```
|
|
51
|
-
* @category Getter
|
|
52
|
-
* @since v4.8.0
|
|
53
|
-
*/
|
|
54
|
-
getPowerSync(): AbstractPowerSyncDatabase;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The createPowerSyncPersister function creates a PowerSyncPersister object
|
|
59
|
-
* that can persist the Store to a local SQLite database that is automatically
|
|
60
|
-
* synced using the PowerSync service.
|
|
61
|
-
*
|
|
62
|
-
* A PowerSyncPersister only supports regular Store objects, and cannot be used
|
|
63
|
-
* to persist the metadata of a MergeableStore.
|
|
64
|
-
*
|
|
65
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
66
|
-
* `powerSync` parameter which identifies the PowerSync instance.
|
|
67
|
-
*
|
|
68
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
69
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
70
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
71
|
-
*
|
|
72
|
-
* The third argument is a DatabasePersisterConfig object that configures which
|
|
73
|
-
* of those modes to use, and settings for each. If the third argument is simply
|
|
74
|
-
* a string, it is used as the `storeTableName` property of the JSON
|
|
75
|
-
* serialization.
|
|
76
|
-
*
|
|
77
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
78
|
-
* information on how both of those modes can be configured.
|
|
79
|
-
* @param store The Store to persist.
|
|
80
|
-
* @param powerSync The PowerSync instance.
|
|
81
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
82
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
83
|
-
* JSON serialization).
|
|
84
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
85
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
86
|
-
* behavior in a development environment, since v4.0.4.
|
|
87
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
88
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
89
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
90
|
-
* @returns A reference to the new PowerSyncPersister object.
|
|
91
|
-
* @example
|
|
92
|
-
* This example creates a PowerSyncPersister object and persists the Store to a
|
|
93
|
-
* local PowerSync instance.
|
|
94
|
-
* It makes a change to the database directly and then reloads it back into the
|
|
95
|
-
* Store.
|
|
96
|
-
*
|
|
97
|
-
* ```js yolo
|
|
98
|
-
* import {usePowerSync} from '@powersync/react';
|
|
99
|
-
* import {createStore} from 'tinybase';
|
|
100
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
101
|
-
*
|
|
102
|
-
* const ps = usePowerSync();
|
|
103
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
104
|
-
* const persister = createPowerSyncPersister(store, ps, 'my_tinybase');
|
|
105
|
-
*
|
|
106
|
-
* await persister.save();
|
|
107
|
-
* // Store will be saved to the database.
|
|
108
|
-
*
|
|
109
|
-
* console.log(
|
|
110
|
-
* await new Promise((resolve) =>
|
|
111
|
-
* ps.execute('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)),
|
|
112
|
-
* ),
|
|
113
|
-
* );
|
|
114
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
115
|
-
*
|
|
116
|
-
* await new Promise((resolve) =>
|
|
117
|
-
* ps.execute(
|
|
118
|
-
* 'UPDATE my_tinybase SET store = ' +
|
|
119
|
-
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
120
|
-
* resolve,
|
|
121
|
-
* ),
|
|
122
|
-
* );
|
|
123
|
-
* await persister.load();
|
|
124
|
-
* console.log(store.getTables());
|
|
125
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
126
|
-
*
|
|
127
|
-
* persister.destroy();
|
|
128
|
-
* ```
|
|
129
|
-
* @example
|
|
130
|
-
* This example creates a PowerSyncPersister object and persists the Store to a
|
|
131
|
-
* local PowerSync instance with tabular mapping.
|
|
132
|
-
*
|
|
133
|
-
* ```js yolo
|
|
134
|
-
* import {usePowerSync} from '@powersync/react';
|
|
135
|
-
* import {createStore} from 'tinybase';
|
|
136
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
137
|
-
*
|
|
138
|
-
* const ps = usePowerSync();
|
|
139
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
140
|
-
* const persister = createPowerSyncPersister(store, ps, {
|
|
141
|
-
* mode: 'tabular',
|
|
142
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
143
|
-
* });
|
|
144
|
-
*
|
|
145
|
-
* await persister.save();
|
|
146
|
-
* console.log(
|
|
147
|
-
* await new Promise((resolve) =>
|
|
148
|
-
* ps.execute('SELECT * FROM pets;', (_, rows) => resolve(rows)),
|
|
149
|
-
* ),
|
|
150
|
-
* );
|
|
151
|
-
* // -> [{_id: 'fido', species: 'dog'}]
|
|
152
|
-
*
|
|
153
|
-
* await new Promise((resolve) =>
|
|
154
|
-
* ps.execute(
|
|
155
|
-
* `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
|
|
156
|
-
* resolve,
|
|
157
|
-
* ),
|
|
158
|
-
* );
|
|
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.8.0
|
|
167
|
-
*/
|
|
168
|
-
export function createPowerSyncPersister(
|
|
169
|
-
store: Store,
|
|
170
|
-
powerSync: AbstractPowerSyncDatabase,
|
|
171
|
-
configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
172
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
173
|
-
onIgnoredError?: (error: any) => void,
|
|
174
|
-
): PowerSyncPersister;
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persister-powersync module of the TinyBase project lets you save and load
|
|
3
|
-
* Store data to and from a local SQLite database that is automatically synced
|
|
4
|
-
* using the PowerSync service.
|
|
5
|
-
* @see Database Persistence guide
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
* @module persister-powersync
|
|
8
|
-
* @since v4.8.0
|
|
9
|
-
*/
|
|
10
|
-
import type {AbstractPowerSyncDatabase} from '@powersync/common';
|
|
11
|
-
import type {
|
|
12
|
-
OptionalSchemas,
|
|
13
|
-
Store,
|
|
14
|
-
} from '../../../store/with-schemas/index.d.cts';
|
|
15
|
-
import type {
|
|
16
|
-
DatabasePersisterConfig,
|
|
17
|
-
Persister,
|
|
18
|
-
} from '../../with-schemas/index.d.cts';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The PowerSyncPersister interface represents a Persister that lets you save
|
|
22
|
-
* and load Store data to and from a local SQLite database that is automatically
|
|
23
|
-
* synced using the PowerSync service.
|
|
24
|
-
*
|
|
25
|
-
* You should use the createPowerSyncPersister function to create a
|
|
26
|
-
* PowerSyncPersister object.
|
|
27
|
-
*
|
|
28
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
29
|
-
* extra getPowerSync method for accessing a reference to the PowerSync instance
|
|
30
|
-
* the Store is being persisted to.
|
|
31
|
-
* @category Persister
|
|
32
|
-
* @since v4.8.0
|
|
33
|
-
*/
|
|
34
|
-
export interface PowerSyncPersister<Schemas extends OptionalSchemas>
|
|
35
|
-
extends Persister<Schemas> {
|
|
36
|
-
/**
|
|
37
|
-
* The getPowerSync method returns a reference to the PowerSync instance the
|
|
38
|
-
* Store is being persisted to.
|
|
39
|
-
* @returns A reference to the PowerSync instance.
|
|
40
|
-
* @example
|
|
41
|
-
* This example creates a Persister object against a newly-created Store and
|
|
42
|
-
* then gets the PowerSync instance back out again.
|
|
43
|
-
*
|
|
44
|
-
* ```js yolo
|
|
45
|
-
* import {usePowerSync} from '@powersync/react';
|
|
46
|
-
* import {createStore} from 'tinybase';
|
|
47
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
48
|
-
*
|
|
49
|
-
* const ps = usePowerSync();
|
|
50
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
51
|
-
* const persister = createPowerSyncPersister(store, ps, 'my_tinybase');
|
|
52
|
-
*
|
|
53
|
-
* console.log(persister.getPowerSync() == ps);
|
|
54
|
-
* // -> true
|
|
55
|
-
*
|
|
56
|
-
* persister.destroy();
|
|
57
|
-
* ```
|
|
58
|
-
* @category Getter
|
|
59
|
-
* @since v4.8.0
|
|
60
|
-
*/
|
|
61
|
-
getPowerSync(): AbstractPowerSyncDatabase;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The createPowerSyncPersister function creates a PowerSyncPersister object
|
|
66
|
-
* that can persist the Store to a local SQLite database that is automatically
|
|
67
|
-
* synced using the PowerSync service.
|
|
68
|
-
*
|
|
69
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
70
|
-
*
|
|
71
|
-
* ```ts override
|
|
72
|
-
* createPowerSyncPersister(
|
|
73
|
-
* store: Store,
|
|
74
|
-
* powerSync: AbstractPowerSyncDatabase,
|
|
75
|
-
* configOrStoreTableName?: DatabasePersisterConfig | string,
|
|
76
|
-
* onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
77
|
-
* onIgnoredError?: (error: any) => void,
|
|
78
|
-
* ): PowerSyncPersister;
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* A PowerSyncPersister only supports regular Store objects, and cannot be used
|
|
82
|
-
* to persist the metadata of a MergeableStore.
|
|
83
|
-
*
|
|
84
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
85
|
-
* `powerSync` parameter which identifies the PowerSync instance.
|
|
86
|
-
*
|
|
87
|
-
* A database Persister uses one of two modes: either a JSON serialization of
|
|
88
|
-
* the whole Store stored in a single row of a table (the default), or a tabular
|
|
89
|
-
* mapping of Table Ids to database table names and vice-versa).
|
|
90
|
-
*
|
|
91
|
-
* The third argument is a DatabasePersisterConfig object that configures which
|
|
92
|
-
* of those modes to use, and settings for each. If the third argument is simply
|
|
93
|
-
* a string, it is used as the `storeTableName` property of the JSON
|
|
94
|
-
* serialization.
|
|
95
|
-
*
|
|
96
|
-
* See the documentation for the DpcJson and DpcTabular types for more
|
|
97
|
-
* information on how both of those modes can be configured.
|
|
98
|
-
* @param store The Store to persist.
|
|
99
|
-
* @param powerSync The PowerSync instance.
|
|
100
|
-
* @param configOrStoreTableName A DatabasePersisterConfig to configure the
|
|
101
|
-
* persistence mode (or a string to set the `storeTableName` property of the
|
|
102
|
-
* JSON serialization).
|
|
103
|
-
* @param onSqlCommand An optional handler called every time the Persister
|
|
104
|
-
* executes a SQL command or query. This is suitable for logging persistence
|
|
105
|
-
* behavior in a development environment, since v4.0.4.
|
|
106
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
107
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
108
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
109
|
-
* @returns A reference to the new PowerSyncPersister object.
|
|
110
|
-
* @example
|
|
111
|
-
* This example creates a PowerSyncPersister object and persists the Store to a
|
|
112
|
-
* local PowerSync instance.
|
|
113
|
-
* It makes a change to the database directly and then reloads it back into the
|
|
114
|
-
* Store.
|
|
115
|
-
*
|
|
116
|
-
* ```js yolo
|
|
117
|
-
* import {usePowerSync} from '@powersync/react';
|
|
118
|
-
* import {createStore} from 'tinybase';
|
|
119
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
120
|
-
*
|
|
121
|
-
* const ps = usePowerSync();
|
|
122
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
123
|
-
* const persister = createPowerSyncPersister(store, ps, 'my_tinybase');
|
|
124
|
-
*
|
|
125
|
-
* await persister.save();
|
|
126
|
-
* // Store will be saved to the database.
|
|
127
|
-
*
|
|
128
|
-
* console.log(
|
|
129
|
-
* await new Promise((resolve) =>
|
|
130
|
-
* ps.execute('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)),
|
|
131
|
-
* ),
|
|
132
|
-
* );
|
|
133
|
-
* // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
|
|
134
|
-
*
|
|
135
|
-
* await new Promise((resolve) =>
|
|
136
|
-
* ps.execute(
|
|
137
|
-
* 'UPDATE my_tinybase SET store = ' +
|
|
138
|
-
* `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
|
|
139
|
-
* resolve,
|
|
140
|
-
* ),
|
|
141
|
-
* );
|
|
142
|
-
* await persister.load();
|
|
143
|
-
* console.log(store.getTables());
|
|
144
|
-
* // -> {pets: {felix: {species: 'cat'}}}
|
|
145
|
-
*
|
|
146
|
-
* persister.destroy();
|
|
147
|
-
* ```
|
|
148
|
-
* @example
|
|
149
|
-
* This example creates a PowerSyncPersister object and persists the Store to a
|
|
150
|
-
* local PowerSync instance with tabular mapping.
|
|
151
|
-
*
|
|
152
|
-
* ```js yolo
|
|
153
|
-
* import {usePowerSync} from '@powersync/react';
|
|
154
|
-
* import {createStore} from 'tinybase';
|
|
155
|
-
* import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
|
|
156
|
-
*
|
|
157
|
-
* const ps = usePowerSync();
|
|
158
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
159
|
-
* const persister = createPowerSyncPersister(store, ps, {
|
|
160
|
-
* mode: 'tabular',
|
|
161
|
-
* tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
|
|
162
|
-
* });
|
|
163
|
-
*
|
|
164
|
-
* await persister.save();
|
|
165
|
-
* console.log(
|
|
166
|
-
* await new Promise((resolve) =>
|
|
167
|
-
* ps.execute('SELECT * FROM pets;', (_, rows) => resolve(rows)),
|
|
168
|
-
* ),
|
|
169
|
-
* );
|
|
170
|
-
* // -> [{_id: 'fido', species: 'dog'}]
|
|
171
|
-
*
|
|
172
|
-
* await new Promise((resolve) =>
|
|
173
|
-
* ps.execute(
|
|
174
|
-
* `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
|
|
175
|
-
* resolve,
|
|
176
|
-
* ),
|
|
177
|
-
* );
|
|
178
|
-
* await persister.load();
|
|
179
|
-
* console.log(store.getTables());
|
|
180
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
181
|
-
*
|
|
182
|
-
* persister.destroy();
|
|
183
|
-
* ```
|
|
184
|
-
* @category Creation
|
|
185
|
-
* @since v4.8.0
|
|
186
|
-
*/
|
|
187
|
-
export function createPowerSyncPersister<Schemas extends OptionalSchemas>(
|
|
188
|
-
store: Store<Schemas>,
|
|
189
|
-
powerSync: AbstractPowerSyncDatabase,
|
|
190
|
-
configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
|
|
191
|
-
onSqlCommand?: (sql: string, params?: any[]) => void,
|
|
192
|
-
onIgnoredError?: (error: any) => void,
|
|
193
|
-
): PowerSyncPersister<Schemas>;
|
|
@@ -1,117 +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 {Store} from '../../store/index.d.cts';
|
|
10
|
-
import type {Persister} from '../index.d.cts';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The RemotePersister interface represents a Persister that lets you save and
|
|
14
|
-
* load Store data to and from a remote server.
|
|
15
|
-
*
|
|
16
|
-
* You should use the createRemotePersister function to create a RemotePersister
|
|
17
|
-
* object.
|
|
18
|
-
*
|
|
19
|
-
* It is a minor extension to the Persister interface and simply provides an
|
|
20
|
-
* extra getUrls method for accessing the URLs the Store is being persisted to.
|
|
21
|
-
* @category Persister
|
|
22
|
-
* @since v4.3.14
|
|
23
|
-
*/
|
|
24
|
-
export interface RemotePersister extends Persister {
|
|
25
|
-
/**
|
|
26
|
-
* The getUrls method returns the URLs the Store is being persisted to.
|
|
27
|
-
* @returns The load and save URLs as a two-item array.
|
|
28
|
-
* @example
|
|
29
|
-
* This example creates a RemotePersister object against a newly-created Store
|
|
30
|
-
* and then gets the URLs back out again.
|
|
31
|
-
*
|
|
32
|
-
* ```js
|
|
33
|
-
* import {createStore} from 'tinybase';
|
|
34
|
-
* import {createRemotePersister} from 'tinybase/persisters/persister-remote';
|
|
35
|
-
*
|
|
36
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
37
|
-
* const persister = createRemotePersister(
|
|
38
|
-
* store,
|
|
39
|
-
* 'https://example.com/load',
|
|
40
|
-
* 'https://example.com/save',
|
|
41
|
-
* 5,
|
|
42
|
-
* );
|
|
43
|
-
*
|
|
44
|
-
* console.log(persister.getUrls());
|
|
45
|
-
* // -> ['https://example.com/load', 'https://example.com/save']
|
|
46
|
-
*
|
|
47
|
-
* persister.destroy();
|
|
48
|
-
* ```
|
|
49
|
-
* @category Getter
|
|
50
|
-
* @since v4.3.14
|
|
51
|
-
*/
|
|
52
|
-
getUrls(): [string, string];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The createRemotePersister function creates a RemotePersister object that can
|
|
57
|
-
* persist the Store to a remote server.
|
|
58
|
-
*
|
|
59
|
-
* A RemotePersister only supports regular Store objects, and cannot be used to
|
|
60
|
-
* persist the metadata of a MergeableStore.
|
|
61
|
-
*
|
|
62
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
63
|
-
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
64
|
-
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
65
|
-
* the `POST` method (to send the Store JSON to save) respectively.
|
|
66
|
-
*
|
|
67
|
-
* For when you choose to enable automatic loading for the Persister (with the
|
|
68
|
-
* startAutoLoad method), it will poll the loadUrl for changes, using the `ETag`
|
|
69
|
-
* HTTP header to identify if things have changed. The `autoLoadIntervalSeconds`
|
|
70
|
-
* method is used to indicate how often to do this.
|
|
71
|
-
*
|
|
72
|
-
* If you are implementing the server portion of this functionality yourself,
|
|
73
|
-
* remember to ensure that the `ETag` header changes every time the server's
|
|
74
|
-
* Store content does - otherwise changes will not be detected by the client.
|
|
75
|
-
* @param store The Store to persist.
|
|
76
|
-
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
77
|
-
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
78
|
-
* @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
|
|
79
|
-
* automatically loading changes from the server, defaulting to 5.
|
|
80
|
-
* @param onIgnoredError An optional handler for the errors that the Persister
|
|
81
|
-
* would otherwise ignore when trying to save or load data. This is suitable for
|
|
82
|
-
* debugging persistence issues in a development environment, since v4.0.4.
|
|
83
|
-
* @returns A reference to the new RemotePersister object.
|
|
84
|
-
* @example
|
|
85
|
-
* This example creates a RemotePersister object and persists the Store to a
|
|
86
|
-
* remote server.
|
|
87
|
-
*
|
|
88
|
-
* ```js yolo
|
|
89
|
-
* import {createStore} from 'tinybase';
|
|
90
|
-
* import {createRemotePersister} from 'tinybase/persisters/persister-remote';
|
|
91
|
-
*
|
|
92
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
93
|
-
* const persister = createRemotePersister(
|
|
94
|
-
* store,
|
|
95
|
-
* 'https://example.com/load',
|
|
96
|
-
* 'https://example.com/save',
|
|
97
|
-
* 5,
|
|
98
|
-
* );
|
|
99
|
-
*
|
|
100
|
-
* await persister.save();
|
|
101
|
-
* // Store JSON will be sent to server in a POST request.
|
|
102
|
-
*
|
|
103
|
-
* await persister.load();
|
|
104
|
-
* // Store JSON will be fetched from server with a GET request.
|
|
105
|
-
*
|
|
106
|
-
* persister.destroy();
|
|
107
|
-
* ```
|
|
108
|
-
* @category Creation
|
|
109
|
-
* @since v1.0.0
|
|
110
|
-
*/
|
|
111
|
-
export function createRemotePersister(
|
|
112
|
-
store: Store,
|
|
113
|
-
loadUrl: string,
|
|
114
|
-
saveUrl: string,
|
|
115
|
-
autoLoadIntervalSeconds?: number,
|
|
116
|
-
onIgnoredError?: (error: any) => void,
|
|
117
|
-
): RemotePersister;
|