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.
Files changed (108) hide show
  1. package/@types/common/index.d.ts +4 -4
  2. package/@types/common/with-schemas/index.d.ts +4 -4
  3. package/@types/mergeable-store/index.d.ts +45 -20
  4. package/@types/mergeable-store/with-schemas/index.d.ts +49 -21
  5. package/@types/persisters/index.d.ts +1 -1
  6. package/@types/persisters/with-schemas/index.d.ts +1 -1
  7. package/index.js +19 -11
  8. package/indexes/index.js +15 -3
  9. package/indexes/with-schemas/index.js +15 -3
  10. package/mergeable-store/index.js +4 -8
  11. package/mergeable-store/with-schemas/index.js +4 -8
  12. package/min/index.js +1 -1
  13. package/min/index.js.gz +0 -0
  14. package/min/indexes/index.js +1 -1
  15. package/min/indexes/index.js.gz +0 -0
  16. package/min/indexes/with-schemas/index.js +1 -1
  17. package/min/indexes/with-schemas/index.js.gz +0 -0
  18. package/min/mergeable-store/index.js +1 -1
  19. package/min/mergeable-store/index.js.gz +0 -0
  20. package/min/mergeable-store/with-schemas/index.js +1 -1
  21. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  22. package/min/with-schemas/index.js +1 -1
  23. package/min/with-schemas/index.js.gz +0 -0
  24. package/package.json +9 -9
  25. package/readme.md +2 -2
  26. package/with-schemas/index.js +19 -11
  27. package/@types/_internal/queries/index.d.cts +0 -0
  28. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  29. package/@types/_internal/store/index.d.cts +0 -3
  30. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  31. package/@types/_internal/ui-react/index.d.cts +0 -0
  32. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  33. package/@types/checkpoints/index.d.cts +0 -1059
  34. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  35. package/@types/common/index.d.cts +0 -158
  36. package/@types/common/with-schemas/index.d.cts +0 -158
  37. package/@types/index.d.cts +0 -17
  38. package/@types/indexes/index.d.cts +0 -1064
  39. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  40. package/@types/mergeable-store/index.d.cts +0 -1139
  41. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  42. package/@types/metrics/index.d.cts +0 -917
  43. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  44. package/@types/persisters/index.d.cts +0 -1877
  45. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  46. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  47. package/@types/persisters/persister-browser/index.d.cts +0 -185
  48. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  49. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  50. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  51. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  52. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  53. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  54. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  55. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  56. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  57. package/@types/persisters/persister-file/index.d.cts +0 -94
  58. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  59. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  60. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  61. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  62. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  63. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  64. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  65. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  66. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  67. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  68. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  69. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  70. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  71. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  72. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  73. package/@types/persisters/persister-remote/index.d.cts +0 -117
  74. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  75. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  76. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  77. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  78. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  79. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  80. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  81. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  82. package/@types/queries/index.d.cts +0 -3695
  83. package/@types/queries/with-schemas/index.d.cts +0 -4016
  84. package/@types/relationships/index.d.cts +0 -1320
  85. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  86. package/@types/store/index.d.cts +0 -7598
  87. package/@types/store/with-schemas/index.d.cts +0 -9278
  88. package/@types/synchronizers/index.d.cts +0 -485
  89. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  90. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  91. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  92. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  93. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  94. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  95. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  96. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  97. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  98. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  99. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  100. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  101. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  102. package/@types/ui-react/index.d.cts +0 -16640
  103. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  104. package/@types/ui-react-dom/index.d.cts +0 -1862
  105. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  106. package/@types/ui-react-inspector/index.d.cts +0 -79
  107. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  108. package/@types/with-schemas/index.d.cts +0 -17
@@ -1,136 +0,0 @@
1
- /**
2
- * The persister-durable-object-storage module of the TinyBase project lets you
3
- * save and load Store data to and from Cloudflare Durable Object storage (in an
4
- * appropriate environment).
5
- * @see Cloudflare Durable Objects guide
6
- * @see Persistence guides
7
- * @packageDocumentation
8
- * @module persister-durable-object-storage
9
- * @since v5.4.0
10
- */
11
- import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
12
- import type {OptionalSchemas} from '../../../store/with-schemas/index.d.cts';
13
- import type {Persister, Persists} from '../../with-schemas/index.d.cts';
14
-
15
- /**
16
- * The DurableObjectStoragePersister interface represents a Persister that lets
17
- * you save and load Store data to and from Cloudflare Durable Object storage.
18
- *
19
- * You should use the createDurableObjectStoragePersister function to create a
20
- * DurableObjectStoragePersister object, most likely within the createPersister
21
- * method of a WsServerDurableObject.
22
- *
23
- * It is a minor extension to the Persister interface and simply provides an
24
- * extra getStorage method for accessing a reference to the storage that the
25
- * Store is being persisted to.
26
- * @category Persister
27
- * @since v5.4.0
28
- */
29
- export interface DurableObjectStoragePersister<Schemas extends OptionalSchemas>
30
- extends Persister<Schemas, Persists.MergeableStoreOnly> {
31
- /**
32
- * The getStorage method returns a reference to the storage that the Store is
33
- * being persisted to.
34
- * @returns The reference to the storage.
35
- * @example
36
- * This example creates a Persister object against a newly-created Store
37
- * (within the createPersister method of a WsServerDurableObject instance) and
38
- * then gets the storage reference back out again.
39
- *
40
- * ```js yolo
41
- * import {createMergeableStore} from 'tinybase';
42
- * import {createDurableObjectStoragePersister} from 'tinybase/persisters/persister-durable-object-storage';
43
- * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
44
- *
45
- * export class MyDurableObject extends WsServerDurableObject {
46
- * createPersister() {
47
- * const store = createMergeableStore();
48
- * const persister = createDurableObjectStoragePersister(
49
- * store,
50
- * this.ctx.storage,
51
- * );
52
- * console.log(persister.getStorage() == this.ctx.storage);
53
- * // -> true
54
- *
55
- * return persister;
56
- * }
57
- * }
58
- * ```
59
- * @category Getter
60
- * @since v5.4.0
61
- */
62
- getStorage(): DurableObjectStorage;
63
- }
64
-
65
- /**
66
- * The createDurableObjectStoragePersister function creates a
67
- * DurableObjectStoragePersister object that can persist the Store to and from
68
- * Cloudflare Durable Object storage.
69
- *
70
- * This has schema-based typing. The following is a simplified representation:
71
- *
72
- * ```ts override
73
- * createDurableObjectStoragePersister(
74
- * store: MergeableStore,
75
- * storage: DurableObjectStorage,
76
- * storagePrefix?: string,
77
- * onIgnoredError?: (error: any) => void,
78
- * ): DurableObjectStoragePersister;
79
- * ```
80
- *
81
- * You will mostly use this within the createPersister method of a
82
- * WsServerDurableObject.
83
- *
84
- * A DurableObjectStoragePersister only supports MergeableStore objects, and
85
- * cannot be used to persist a regular Store.
86
- *
87
- * Durable Objects have limitations on the data that can be stored in each key
88
- * of their key-value structure. The DurableObjectStoragePersister uses one key
89
- * per TinyBase Value, one key per Cell, one key per Row, and one key per Table.
90
- * Mostly this is CRDT metadata, but the main caution is to ensure that each
91
- * individual TinyBase Cell and Value data does not exceed the (128 KiB) limit.
92
- *
93
- * As well as providing a reference to the MergeableStore to persist, you must
94
- * provide a `storage` parameter which identifies the Durable Object storage to
95
- * persist it to.
96
- * @param store The MergeableStore to persist.
97
- * @param storage The Durable Object storage to persist the Store to.
98
- * @param storagePrefix An optional prefix to use on the keys in storage, which
99
- * is useful if you want to ensure the Persister will not affect unrelated
100
- * Durable Object storage. Defaults to an empty string.
101
- * @param onIgnoredError An optional handler for the errors that the Persister
102
- * would otherwise ignore when trying to save or load data. This is suitable for
103
- * debugging persistence issues in a development environment.
104
- * @returns A reference to the new DurableObjectStoragePersister object.
105
- * @example
106
- * This example creates a Persister object against a newly-created
107
- * MergeableStore (within the createPersister method of a WsServerDurableObject
108
- * instance) and then gets the storage reference back out again.
109
- *
110
- * ```js yolo
111
- * import {createMergeableStore} from 'tinybase';
112
- * import {createDurableObjectStoragePersister} from 'tinybase/persisters/persister-durable-object-storage';
113
- * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';
114
- *
115
- * export class MyDurableObject extends WsServerDurableObject {
116
- * createPersister() {
117
- * const store = createMergeableStore();
118
- * const persister = createDurableObjectStoragePersister(
119
- * store,
120
- * this.ctx.storage,
121
- * );
122
- * return persister;
123
- * }
124
- * }
125
- * ```
126
- * @category Creation
127
- * @since v5.4.0
128
- */
129
- export function createDurableObjectStoragePersister<
130
- Schemas extends OptionalSchemas,
131
- >(
132
- store: MergeableStore<Schemas>,
133
- storage: DurableObjectStorage,
134
- storagePrefix?: string,
135
- onIgnoredError?: (error: any) => void,
136
- ): DurableObjectStoragePersister<Schemas>;
@@ -1,185 +0,0 @@
1
- /**
2
- * The persister-electric-sql module of the TinyBase project lets you save and
3
- * load Store data to and from a local ElectricSQL database (in an appropriate
4
- * environment).
5
- * @see Database Persistence guide
6
- * @packageDocumentation
7
- * @module persister-electric-sql
8
- * @since v4.6.0
9
- */
10
- import type {ElectricClient} from 'electric-sql/client/model';
11
- import type {Store} from '../../store/index.d.cts';
12
- import type {DatabasePersisterConfig, Persister} from '../index.d.cts';
13
-
14
- /**
15
- * The ElectricSqlPersister interface represents a Persister that lets you save
16
- * and load Store data to and from a local ElectricSQL database.
17
- *
18
- * You should use the createElectricSqlPersister function to create an
19
- * ElectricSqlPersister object.
20
- *
21
- * It is a minor extension to the Persister interface and simply provides an
22
- * extra getElectricClient method for accessing a reference to the Electric
23
- * client the Store is being persisted to.
24
- * @category Persister
25
- * @since v4.6.0
26
- */
27
- export interface ElectricSqlPersister extends Persister {
28
- /**
29
- * The getElectricClient method returns a reference to the Electric client the
30
- * Store is being persisted to.
31
- * @returns A reference to the Electric client.
32
- * @example
33
- * This example creates a Persister object against a newly-created Store and
34
- * then gets the Electric client back out again.
35
- *
36
- * ```js yolo
37
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
38
- * import {createStore} from 'tinybase';
39
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
40
- * import {schema} from './generated/client';
41
- *
42
- * const electricClient = await electrify(
43
- * await ElectricDatabase.init('electric.db', ''),
44
- * schema,
45
- * {url: import.meta.env.ELECTRIC_SERVICE},
46
- * );
47
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
48
- * const persister = createElectricSqlPersister(
49
- * store,
50
- * electricClient,
51
- * 'my_tinybase',
52
- * );
53
- *
54
- * console.log(persister.getElectricClient() == electricClient);
55
- * // -> true
56
- *
57
- * persister.destroy();
58
- * ```
59
- * @category Getter
60
- * @since v4.6.0
61
- */
62
- getElectricClient(): ElectricClient<any>;
63
- }
64
-
65
- /**
66
- * The createElectricSqlPersister function creates an ElectricSqlPersister
67
- * object that can persist a Store to a local ElectricSQL database.
68
- *
69
- * An ElectricSqlPersister only supports regular Store objects, and cannot be
70
- * used to persist the metadata of a MergeableStore.
71
- *
72
- * As well as providing a reference to the Store to persist, you must provide a
73
- * `electricClient` parameter which identifies the Electric client.
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 third argument is a DatabasePersisterConfig object that configures which
80
- * of those modes to use, and settings for each. If the third argument is simply
81
- * 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 to persist.
87
- * @param electricClient The Electric client that was returned from `await
88
- * electrify(...)`.
89
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
90
- * persistence mode (or a string to set the `storeTableName` property of the
91
- * JSON serialization).
92
- * @param onSqlCommand An optional handler called every time the Persister
93
- * executes a SQL command or query. This is suitable for logging persistence
94
- * behavior in a development environment.
95
- * @param onIgnoredError An optional handler for the errors that the Persister
96
- * would otherwise ignore when trying to save or load data. This is suitable for
97
- * debugging persistence issues in a development environment.
98
- * @returns A reference to the new ElectricSqlPersister object.
99
- * @example
100
- * This example creates a ElectricSqlPersister object and persists the Store to
101
- * a local ElectricSql database as a JSON serialization into the `my_tinybase`
102
- * table. It makes a change to the database directly and then reloads it back
103
- * into the Store.
104
- *
105
- * ```js yolo
106
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
107
- * import {createStore} from 'tinybase';
108
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
109
- * import {schema} from './generated/client';
110
- *
111
- * const electricClient = await electrify(
112
- * await ElectricDatabase.init('electric.db', ''),
113
- * schema,
114
- * {url: import.meta.env.ELECTRIC_SERVICE},
115
- * );
116
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
117
- * const persister = createElectricSqlPersister(
118
- * store,
119
- * electricClient,
120
- * 'my_tinybase',
121
- * );
122
- *
123
- * await persister.save();
124
- * // Store will be saved to the database.
125
- *
126
- * console.log(
127
- * await electricClient.db.raw({sql: 'SELECT * FROM my_tinybase;'}),
128
- * );
129
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
130
- *
131
- * await electricClient.db.raw({
132
- * sql:
133
- * 'UPDATE my_tinybase SET store = ' +
134
- * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
135
- * });
136
- * await persister.load();
137
- * console.log(store.getTables());
138
- * // -> {pets: {felix: {species: 'cat'}}}
139
- *
140
- * persister.destroy();
141
- * ```
142
- * @example
143
- * This example creates a ElectricSqlPersister object and persists the Store to
144
- * a local ElectricSql database with tabular mapping.
145
- *
146
- * ```js yolo
147
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
148
- * import {createStore} from 'tinybase';
149
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
150
- * import {schema} from './generated/client';
151
- *
152
- * const electricClient = await electrify(
153
- * await ElectricDatabase.init('electric.db', ''),
154
- * schema,
155
- * {url: import.meta.env.ELECTRIC_SERVICE},
156
- * );
157
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
158
- * const persister = createElectricSqlPersister(store, electricClient, {
159
- * mode: 'tabular',
160
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
161
- * });
162
- *
163
- * await persister.save();
164
- * console.log(await electricClient.db.raw({sql: 'SELECT * FROM pets;'}));
165
- * // -> [{_id: 'fido', species: 'dog'}]
166
- *
167
- * await electricClient.db.raw({
168
- * sql: `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
169
- * });
170
- * await persister.load();
171
- * console.log(store.getTables());
172
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
173
- *
174
- * persister.destroy();
175
- * ```
176
- * @category Creation
177
- * @since v4.6.0
178
- */
179
- export function createElectricSqlPersister(
180
- store: Store,
181
- electricClient: ElectricClient<any>,
182
- configOrStoreTableName?: DatabasePersisterConfig | string,
183
- onSqlCommand?: (sql: string, params?: any[]) => void,
184
- onIgnoredError?: (error: any) => void,
185
- ): ElectricSqlPersister;
@@ -1,204 +0,0 @@
1
- /**
2
- * The persister-electric-sql module of the TinyBase project lets you save and
3
- * load Store data to and from a local ElectricSQL database (in an appropriate
4
- * environment).
5
- * @see Database Persistence guide
6
- * @packageDocumentation
7
- * @module persister-electric-sql
8
- * @since v4.6.0
9
- */
10
- import type {ElectricClient} from 'electric-sql/client/model';
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 ElectricSqlPersister interface represents a Persister that lets you save
22
- * and load Store data to and from a local ElectricSQL database.
23
- *
24
- * You should use the createElectricSqlPersister function to create an
25
- * ElectricSqlPersister object.
26
- *
27
- * It is a minor extension to the Persister interface and simply provides an
28
- * extra getElectricClient method for accessing a reference to the Electric
29
- * client the Store is being persisted to.
30
- * @category Persister
31
- * @since v4.6.0
32
- */
33
- export interface ElectricSqlPersister<Schemas extends OptionalSchemas>
34
- extends Persister<Schemas> {
35
- /**
36
- * The getElectricClient method returns a reference to the Electric client the
37
- * Store is being persisted to.
38
- * @returns A reference to the Electric client.
39
- * @example
40
- * This example creates a Persister object against a newly-created Store and
41
- * then gets the Electric client back out again.
42
- *
43
- * ```js yolo
44
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
45
- * import {createStore} from 'tinybase';
46
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
47
- * import {schema} from './generated/client';
48
- *
49
- * const electricClient = await electrify(
50
- * await ElectricDatabase.init('electric.db', ''),
51
- * schema,
52
- * {url: import.meta.env.ELECTRIC_SERVICE},
53
- * );
54
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
55
- * const persister = createElectricSqlPersister(
56
- * store,
57
- * electricClient,
58
- * 'my_tinybase',
59
- * );
60
- *
61
- * console.log(persister.getElectricClient() == electricClient);
62
- * // -> true
63
- *
64
- * persister.destroy();
65
- * ```
66
- * @category Getter
67
- * @since v4.6.0
68
- */
69
- getElectricClient(): ElectricClient<any>;
70
- }
71
-
72
- /**
73
- * The createElectricSqlPersister function creates an ElectricSqlPersister
74
- * object that can persist a Store to a local ElectricSQL database.
75
- *
76
- * This has schema-based typing. The following is a simplified representation:
77
- *
78
- * ```ts override
79
- * createElectricSqlPersister(
80
- * store: Store,
81
- * electricClient: ElectricClient<any>,
82
- * configOrStoreTableName?: DatabasePersisterConfig | string,
83
- * onSqlCommand?: (sql: string, params?: any[]) => void,
84
- * onIgnoredError?: (error: any) => void,
85
- * ): ElectricSqlPersister;
86
- * ```
87
- *
88
- * An ElectricSqlPersister only supports regular Store objects, and cannot be
89
- * used to persist the metadata of a MergeableStore.
90
- *
91
- * As well as providing a reference to the Store to persist, you must provide a
92
- * `electricClient` parameter which identifies the Electric client.
93
- *
94
- * A database Persister uses one of two modes: either a JSON serialization of
95
- * the whole Store stored in a single row of a table (the default), or a tabular
96
- * mapping of Table Ids to database table names and vice-versa).
97
- *
98
- * The third argument is a DatabasePersisterConfig object that configures which
99
- * of those modes to use, and settings for each. If the third argument is simply
100
- * a string, it is used as the `storeTableName` property of the JSON
101
- * serialization.
102
- *
103
- * See the documentation for the DpcJson and DpcTabular types for more
104
- * information on how both of those modes can be configured.
105
- * @param store The Store to persist.
106
- * @param electricClient The Electric client that was returned from `await
107
- * electrify(...)`.
108
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
109
- * persistence mode (or a string to set the `storeTableName` property of the
110
- * JSON serialization).
111
- * @param onSqlCommand An optional handler called every time the Persister
112
- * executes a SQL command or query. This is suitable for logging persistence
113
- * behavior in a development environment.
114
- * @param onIgnoredError An optional handler for the errors that the Persister
115
- * would otherwise ignore when trying to save or load data. This is suitable for
116
- * debugging persistence issues in a development environment.
117
- * @returns A reference to the new ElectricSqlPersister object.
118
- * @example
119
- * This example creates a ElectricSqlPersister object and persists the Store to
120
- * a local ElectricSql database as a JSON serialization into the `my_tinybase`
121
- * table. It makes a change to the database directly and then reloads it back
122
- * into the Store.
123
- *
124
- * ```js yolo
125
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
126
- * import {createStore} from 'tinybase';
127
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
128
- * import {schema} from './generated/client';
129
- *
130
- * const electricClient = await electrify(
131
- * await ElectricDatabase.init('electric.db', ''),
132
- * schema,
133
- * {url: import.meta.env.ELECTRIC_SERVICE},
134
- * );
135
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
136
- * const persister = createElectricSqlPersister(
137
- * store,
138
- * electricClient,
139
- * 'my_tinybase',
140
- * );
141
- *
142
- * await persister.save();
143
- * // Store will be saved to the database.
144
- *
145
- * console.log(
146
- * await electricClient.db.raw({sql: 'SELECT * FROM my_tinybase;'}),
147
- * );
148
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
149
- *
150
- * await electricClient.db.raw({
151
- * sql:
152
- * 'UPDATE my_tinybase SET store = ' +
153
- * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
154
- * });
155
- * await persister.load();
156
- * console.log(store.getTables());
157
- * // -> {pets: {felix: {species: 'cat'}}}
158
- *
159
- * persister.destroy();
160
- * ```
161
- * @example
162
- * This example creates a ElectricSqlPersister object and persists the Store to
163
- * a local ElectricSql database with tabular mapping.
164
- *
165
- * ```js yolo
166
- * import {ElectricDatabase, electrify} from 'electric-sql/wa-sqlite';
167
- * import {createStore} from 'tinybase';
168
- * import {createElectricSqlPersister} from 'tinybase/persisters/persister-electric-sql';
169
- * import {schema} from './generated/client';
170
- *
171
- * const electricClient = await electrify(
172
- * await ElectricDatabase.init('electric.db', ''),
173
- * schema,
174
- * {url: import.meta.env.ELECTRIC_SERVICE},
175
- * );
176
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
177
- * const persister = createElectricSqlPersister(store, electricClient, {
178
- * mode: 'tabular',
179
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
180
- * });
181
- *
182
- * await persister.save();
183
- * console.log(await electricClient.db.raw({sql: 'SELECT * FROM pets;'}));
184
- * // -> [{_id: 'fido', species: 'dog'}]
185
- *
186
- * await electricClient.db.raw({
187
- * sql: `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
188
- * });
189
- * await persister.load();
190
- * console.log(store.getTables());
191
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
192
- *
193
- * persister.destroy();
194
- * ```
195
- * @category Creation
196
- * @since v4.6.0
197
- */
198
- export function createElectricSqlPersister<Schemas extends OptionalSchemas>(
199
- store: Store<Schemas>,
200
- electricClient: ElectricClient<any>,
201
- configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
202
- onSqlCommand?: (sql: string, params?: any[]) => void,
203
- onIgnoredError?: (error: any) => void,
204
- ): ElectricSqlPersister<Schemas>;