tinybase 6.1.0-beta.1 → 6.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/@types/common/index.d.ts +4 -4
  2. package/@types/common/with-schemas/index.d.ts +4 -4
  3. package/index.js +15 -3
  4. package/indexes/index.js +15 -3
  5. package/indexes/with-schemas/index.js +15 -3
  6. package/min/index.js +1 -1
  7. package/min/index.js.gz +0 -0
  8. package/min/indexes/index.js +1 -1
  9. package/min/indexes/index.js.gz +0 -0
  10. package/min/indexes/with-schemas/index.js +1 -1
  11. package/min/indexes/with-schemas/index.js.gz +0 -0
  12. package/min/with-schemas/index.js +1 -1
  13. package/min/with-schemas/index.js.gz +0 -0
  14. package/package.json +7 -7
  15. package/readme.md +1 -1
  16. package/with-schemas/index.js +15 -3
  17. package/@types/_internal/queries/index.d.cts +0 -0
  18. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  19. package/@types/_internal/store/index.d.cts +0 -3
  20. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  21. package/@types/_internal/ui-react/index.d.cts +0 -0
  22. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  23. package/@types/checkpoints/index.d.cts +0 -1059
  24. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  25. package/@types/common/index.d.cts +0 -158
  26. package/@types/common/with-schemas/index.d.cts +0 -158
  27. package/@types/index.d.cts +0 -17
  28. package/@types/indexes/index.d.cts +0 -1064
  29. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  30. package/@types/mergeable-store/index.d.cts +0 -1139
  31. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  32. package/@types/metrics/index.d.cts +0 -917
  33. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  34. package/@types/persisters/index.d.cts +0 -1877
  35. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  36. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  37. package/@types/persisters/persister-browser/index.d.cts +0 -185
  38. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  39. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  40. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  41. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  42. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  43. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  44. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  45. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  46. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  47. package/@types/persisters/persister-file/index.d.cts +0 -94
  48. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  49. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  50. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  51. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  52. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  53. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  54. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  55. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  56. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  57. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  58. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  59. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  60. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  61. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  62. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  63. package/@types/persisters/persister-remote/index.d.cts +0 -117
  64. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  65. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  66. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  67. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  68. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  69. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  70. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  71. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  72. package/@types/queries/index.d.cts +0 -3695
  73. package/@types/queries/with-schemas/index.d.cts +0 -4016
  74. package/@types/relationships/index.d.cts +0 -1320
  75. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  76. package/@types/store/index.d.cts +0 -7598
  77. package/@types/store/with-schemas/index.d.cts +0 -9278
  78. package/@types/synchronizers/index.d.cts +0 -485
  79. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  80. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  81. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  82. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  83. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  84. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  85. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  86. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  87. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  88. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  89. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  90. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  91. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  92. package/@types/ui-react/index.d.cts +0 -16640
  93. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  94. package/@types/ui-react-dom/index.d.cts +0 -1862
  95. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  96. package/@types/ui-react-inspector/index.d.cts +0 -79
  97. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  98. package/@types/with-schemas/index.d.cts +0 -17
@@ -1,195 +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/with-schemas/index.d.cts';
12
- import type {
13
- OptionalSchemas,
14
- Store,
15
- } from '../../../store/with-schemas/index.d.cts';
16
- import type {
17
- DatabasePersisterConfig,
18
- Persister,
19
- Persists,
20
- } from '../../with-schemas/index.d.cts';
21
-
22
- /**
23
- * The Sqlite3Persister interface represents a Persister that lets you save and
24
- * load Store data to and from a local SQLite database.
25
- *
26
- * You should use the createSqlite3Persister function to create a
27
- * Sqlite3Persister object.
28
- *
29
- * It is a minor extension to the Persister interface and simply provides an
30
- * extra getDb method for accessing a reference to the database instance the
31
- * Store is being persisted to.
32
- * @category Persister
33
- * @since v4.3.14
34
- */
35
- export interface Sqlite3Persister<Schemas extends OptionalSchemas>
36
- extends Persister<Schemas, Persists.StoreOrMergeableStore> {
37
- /**
38
- * The getDb method returns a reference to the database instance the Store is
39
- * being persisted to.
40
- * @returns A reference to the database instance.
41
- * @example
42
- * This example creates a Persister object against a newly-created Store and
43
- * then gets the database instance back out again.
44
- *
45
- * ```js
46
- * import {Database} from 'sqlite3';
47
- * import {createStore} from 'tinybase';
48
- * import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
49
- *
50
- * const db = new Database(':memory:');
51
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
52
- * const persister = createSqlite3Persister(store, db, 'my_tinybase');
53
- *
54
- * console.log(persister.getDb() == db);
55
- * // -> true
56
- *
57
- * persister.destroy();
58
- * ```
59
- * @category Getter
60
- * @since v4.3.14
61
- */
62
- getDb(): Database;
63
- }
64
-
65
- /**
66
- * The createSqlite3Persister function creates a Sqlite3Persister object that
67
- * can persist the Store to a local SQLite database.
68
- *
69
- * This has schema-based typing. The following is a simplified representation:
70
- *
71
- * ```ts override
72
- * createSqlite3Persister(
73
- * store: Store | MergeableStore,
74
- * db: Database,
75
- * configOrStoreTableName?: DatabasePersisterConfig | string,
76
- * onSqlCommand?: (sql: string, params?: any[]) => void,
77
- * onIgnoredError?: (error: any) => void,
78
- * ): Sqlite3Persister;
79
- * ```
80
- *
81
- * A Sqlite3Persister 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
- * `db` parameter which identifies the database instance.
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
- * @param store The Store or MergeableStore to persist.
100
- * @param db The database instance that was returned from `new
101
- * sqlite3.Database(...)`.
102
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
103
- * persistence mode (or a string to set the `storeTableName` property of the
104
- * JSON serialization).
105
- * @param onSqlCommand An optional handler called every time the Persister
106
- * executes a SQL command or query. This is suitable for logging persistence
107
- * behavior in a development environment, since v4.0.4.
108
- * @param onIgnoredError An optional handler for the errors that the Persister
109
- * would otherwise ignore when trying to save or load data. This is suitable for
110
- * debugging persistence issues in a development environment, since v4.0.4.
111
- * @returns A reference to the new Sqlite3Persister object.
112
- * @example
113
- * This example creates a Sqlite3Persister object and persists the Store to a
114
- * local SQLite database as a JSON serialization into the `my_tinybase` table.
115
- * It makes a change to the database directly and then reloads it back into the
116
- * Store.
117
- *
118
- * ```js
119
- * import {Database} from 'sqlite3';
120
- * import {createStore} from 'tinybase';
121
- * import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
122
- *
123
- * const db = new Database(':memory:');
124
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
125
- * const persister = createSqlite3Persister(store, db, 'my_tinybase');
126
- *
127
- * await persister.save();
128
- * // Store will be saved to the database.
129
- *
130
- * console.log(
131
- * await new Promise((resolve) =>
132
- * db.all('SELECT * FROM my_tinybase;', (_, rows) => resolve(rows)),
133
- * ),
134
- * );
135
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
136
- *
137
- * await new Promise((resolve) =>
138
- * db.all(
139
- * 'UPDATE my_tinybase SET store = ' +
140
- * `'[{"pets":{"felix":{"species":"cat"}}},{}]' WHERE _id = '_';`,
141
- * resolve,
142
- * ),
143
- * );
144
- * await persister.load();
145
- * console.log(store.getTables());
146
- * // -> {pets: {felix: {species: 'cat'}}}
147
- *
148
- * persister.destroy();
149
- * ```
150
- * @example
151
- * This example creates a Sqlite3Persister object and persists the Store to a
152
- * local SQLite database with tabular mapping.
153
- *
154
- * ```js
155
- * import {Database} from 'sqlite3';
156
- * import {createStore} from 'tinybase';
157
- * import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
158
- *
159
- * const db = new Database(':memory:');
160
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
161
- * const persister = createSqlite3Persister(store, db, {
162
- * mode: 'tabular',
163
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
164
- * });
165
- *
166
- * await persister.save();
167
- * console.log(
168
- * await new Promise((resolve) =>
169
- * db.all('SELECT * FROM pets;', (_, rows) => resolve(rows)),
170
- * ),
171
- * );
172
- * // -> [{_id: 'fido', species: 'dog'}]
173
- *
174
- * await new Promise((resolve) =>
175
- * db.all(
176
- * `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
177
- * resolve,
178
- * ),
179
- * );
180
- * await persister.load();
181
- * console.log(store.getTables());
182
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
183
- *
184
- * persister.destroy();
185
- * ```
186
- * @category Creation
187
- * @since v4.0.0
188
- */
189
- export function createSqlite3Persister<Schemas extends OptionalSchemas>(
190
- store: Store<Schemas> | MergeableStore<Schemas>,
191
- db: Database,
192
- configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
193
- onSqlCommand?: (sql: string, params?: any[]) => void,
194
- onIgnoredError?: (error: any) => void,
195
- ): Sqlite3Persister<Schemas>;
@@ -1,161 +0,0 @@
1
- /**
2
- * The persister-yjs module of the TinyBase project provides a way to save and
3
- * load Store data to and from a Yjs document.
4
- *
5
- * A single entry point, the createYjsPersister function, is provided, which
6
- * returns a new Persister object that can bind a Store to a provided Yjs
7
- * document.
8
- * @see Third-Party CRDT Persistence guide
9
- * @packageDocumentation
10
- * @module persister-yjs
11
- * @since v4.0.0
12
- */
13
- import type {Doc as YDoc} from 'yjs';
14
- import type {Store} from '../../store/index.d.cts';
15
- import type {Persister} from '../index.d.cts';
16
-
17
- /**
18
- * The YjsPersister interface represents a Persister that lets you save and load
19
- * Store data to and from a Yjs document.
20
- *
21
- * You should use the createYjsPersister function to create a YjsPersister
22
- * object.
23
- *
24
- * It is a minor extension to the Persister interface and simply provides an
25
- * extra getYDoc method for accessing the Yjs document the Store is being
26
- * persisted to.
27
- * @category Persister
28
- * @since v4.3.14
29
- */
30
- export interface YjsPersister extends Persister {
31
- /**
32
- * The getYDoc method returns the Yjs document the Store is being persisted
33
- * to.
34
- * @returns The Yjs document.
35
- * @example
36
- * This example creates a Persister object against a newly-created Store and
37
- * then gets the Yjs document back out again.
38
- *
39
- * ```js
40
- * import {createStore} from 'tinybase';
41
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
42
- * import {Doc} from 'yjs';
43
- *
44
- * const doc = new Doc();
45
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
46
- * const persister = createYjsPersister(store, doc);
47
- *
48
- * console.log(persister.getYDoc() == doc);
49
- * // -> true
50
- *
51
- * persister.destroy();
52
- * ```
53
- * @category Getter
54
- * @since v4.3.14
55
- */
56
- getYDoc(): YDoc;
57
- }
58
-
59
- /**
60
- * The createYjsPersister function creates a YjsPersister object that can
61
- * persist the Store to a Yjs document.
62
- *
63
- * A YjsPersister only supports regular Store objects, and cannot be used to
64
- * persist the metadata of a MergeableStore.
65
- *
66
- * As well as providing a reference to the Store to persist, you must provide
67
- * the Yjs document to persist it to.
68
- * @param store The Store to persist.
69
- * @param yDoc The Yjs document to persist the Store to.
70
- * @param yMapName The name of the Y.Map used inside the Yjs document to sync
71
- * the Store to (which otherwise will default to 'tinybase').
72
- * @param onIgnoredError An optional handler for the errors that the Persister
73
- * would otherwise ignore when trying to save or load data. This is suitable for
74
- * debugging persistence issues in a development environment, since v4.0.4.
75
- * @returns A reference to the new YjsPersister object.
76
- * @example
77
- * This example creates a YjsPersister object and persists the Store to a Yjs
78
- * document.
79
- *
80
- * ```js
81
- * import {createStore} from 'tinybase';
82
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
83
- * import {Doc} from 'yjs';
84
- *
85
- * const doc = new Doc();
86
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
87
- * const persister = createYjsPersister(store, doc);
88
- *
89
- * await persister.save();
90
- * // Store will be saved to the document.
91
- *
92
- * console.log(doc.toJSON());
93
- * // -> {tinybase: {t: {pets: {fido: {species: 'dog'}}}, v: {}}}
94
- *
95
- * persister.destroy();
96
- * ```
97
- * @example
98
- * This more complex example uses Yjs updates to keep two Store objects (each
99
- * with their own YjsPersister objects and Yjs documents) in sync with each
100
- * other. We use the `await` keyword extensively for the purpose of ensuring
101
- * sequentiality in this example.
102
- *
103
- * Typically, real-world synchronization would happen between two systems via a
104
- * Yjs connection provider. Here, we synthesize that with the `syncDocs`
105
- * function.
106
- *
107
- * ```js
108
- * import {createStore} from 'tinybase';
109
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
110
- * import {Doc, applyUpdate, encodeStateAsUpdate} from 'yjs';
111
- *
112
- * const doc1 = new Doc();
113
- * const doc2 = new Doc();
114
- *
115
- * // A function to manually synchronize documents with each other. Typically
116
- * // this would happen over the wire, via a Yjs connection provider.
117
- * const syncDocs = async () => {
118
- * applyUpdate(doc1, encodeStateAsUpdate(doc2));
119
- * applyUpdate(doc2, encodeStateAsUpdate(doc1));
120
- * };
121
- *
122
- * // Bind a persisted Store to each document.
123
- * const store1 = createStore();
124
- * const persister1 = createYjsPersister(store1, doc1);
125
- * await persister1.startAutoLoad();
126
- * await persister1.startAutoSave();
127
- *
128
- * const store2 = createStore();
129
- * const persister2 = createYjsPersister(store2, doc2);
130
- * await persister2.startAutoLoad();
131
- * await persister2.startAutoSave();
132
- *
133
- * // Synchronize the documents in their initial state.
134
- * await syncDocs();
135
- *
136
- * // Make a change to each of the two Stores.
137
- * store1.setTables({pets: {fido: {species: 'dog'}}});
138
- * store2.setValues({open: true});
139
- * // ...
140
- *
141
- * // Synchronize the documents with each other again.
142
- * await syncDocs();
143
- *
144
- * // Ensure the Stores are in sync.
145
- * console.log(store1.getContent());
146
- * // -> [{pets: {fido: {species: 'dog'}}}, {open: true}]
147
- * console.log(store2.getContent());
148
- * // -> [{pets: {fido: {species: 'dog'}}}, {open: true}]
149
- *
150
- * persister1.destroy();
151
- * persister2.destroy();
152
- * ```
153
- * @category Creation
154
- * @since v4.0.0
155
- */
156
- export function createYjsPersister(
157
- store: Store,
158
- yDoc: YDoc,
159
- yMapName?: string,
160
- onIgnoredError?: (error: any) => void,
161
- ): YjsPersister;
@@ -1,176 +0,0 @@
1
- /**
2
- * The persister-yjs module of the TinyBase project provides a way to save and
3
- * load Store data to and from a Yjs document.
4
- *
5
- * A single entry point, the createYjsPersister function, is provided, which
6
- * returns a new Persister object that can bind a Store to a provided Yjs
7
- * document.
8
- * @see Third-Party CRDT Persistence guide
9
- * @packageDocumentation
10
- * @module persister-yjs
11
- * @since v4.0.0
12
- */
13
- import type {Doc as YDoc} from 'yjs';
14
- import type {
15
- OptionalSchemas,
16
- Store,
17
- } from '../../../store/with-schemas/index.d.cts';
18
- import type {Persister} from '../../with-schemas/index.d.cts';
19
-
20
- /**
21
- * The YjsPersister interface represents a Persister that lets you save and load
22
- * Store data to and from a Yjs document.
23
- *
24
- * You should use the createYjsPersister function to create a YjsPersister
25
- * object.
26
- *
27
- * It is a minor extension to the Persister interface and simply provides an
28
- * extra getYDoc method for accessing the Yjs document the Store is being
29
- * persisted to.
30
- * @category Persister
31
- * @since v4.3.14
32
- */
33
- export interface YjsPersister<Schemas extends OptionalSchemas>
34
- extends Persister<Schemas> {
35
- /**
36
- * The getYDoc method returns the Yjs document the Store is being persisted
37
- * to.
38
- * @returns The Yjs document.
39
- * @example
40
- * This example creates a Persister object against a newly-created Store and
41
- * then gets the Yjs document back out again.
42
- *
43
- * ```js
44
- * import {createStore} from 'tinybase';
45
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
46
- * import {Doc} from 'yjs';
47
- *
48
- * const doc = new Doc();
49
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
50
- * const persister = createYjsPersister(store, doc);
51
- *
52
- * console.log(persister.getYDoc() == doc);
53
- * // -> true
54
- *
55
- * persister.destroy();
56
- * ```
57
- * @category Getter
58
- * @since v4.3.14
59
- */
60
- getYDoc(): YDoc;
61
- }
62
-
63
- /**
64
- * The createYjsPersister function creates a YjsPersister object that can
65
- * persist the Store to a Yjs document.
66
- *
67
- * This has schema-based typing. The following is a simplified representation:
68
- *
69
- * ```ts override
70
- * createYjsPersister(
71
- * store: Store,
72
- * yDoc: YDoc,
73
- * yMapName?: string,
74
- * onIgnoredError?: (error: any) => void,
75
- * ): YjsPersister;
76
- * ```
77
- *
78
- * A YjsPersister only supports regular Store objects, and cannot be used to
79
- * persist the metadata of a MergeableStore.
80
- *
81
- * As well as providing a reference to the Store to persist, you must provide
82
- * the Yjs document to persist it to.
83
- * @param store The Store to persist.
84
- * @param yDoc The Yjs document to persist the Store to.
85
- * @param yMapName The name of the Y.Map used inside the Yjs document to sync
86
- * the Store to (which otherwise will default to 'tinybase').
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 YjsPersister object.
91
- * @example
92
- * This example creates a YjsPersister object and persists the Store to a Yjs
93
- * document.
94
- *
95
- * ```js
96
- * import {createStore} from 'tinybase';
97
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
98
- * import {Doc} from 'yjs';
99
- *
100
- * const doc = new Doc();
101
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
102
- * const persister = createYjsPersister(store, doc);
103
- *
104
- * await persister.save();
105
- * // Store will be saved to the document.
106
- *
107
- * console.log(doc.toJSON());
108
- * // -> {tinybase: {t: {pets: {fido: {species: 'dog'}}}, v: {}}}
109
- *
110
- * persister.destroy();
111
- * ```
112
- * @example
113
- * This more complex example uses Yjs updates to keep two Store objects (each
114
- * with their own YjsPersister objects and Yjs documents) in sync with each
115
- * other. We use the `await` keyword extensively for the purpose of ensuring
116
- * sequentiality in this example.
117
- *
118
- * Typically, real-world synchronization would happen between two systems via a
119
- * Yjs connection provider. Here, we synthesize that with the `syncDocs`
120
- * function.
121
- *
122
- * ```js
123
- * import {createStore} from 'tinybase';
124
- * import {createYjsPersister} from 'tinybase/persisters/persister-yjs';
125
- * import {Doc, applyUpdate, encodeStateAsUpdate} from 'yjs';
126
- *
127
- * const doc1 = new Doc();
128
- * const doc2 = new Doc();
129
- *
130
- * // A function to manually synchronize documents with each other. Typically
131
- * // this would happen over the wire, via a Yjs connection provider.
132
- * const syncDocs = async () => {
133
- * applyUpdate(doc1, encodeStateAsUpdate(doc2));
134
- * applyUpdate(doc2, encodeStateAsUpdate(doc1));
135
- * };
136
- *
137
- * // Bind a persisted Store to each document.
138
- * const store1 = createStore();
139
- * const persister1 = createYjsPersister(store1, doc1);
140
- * await persister1.startAutoLoad();
141
- * await persister1.startAutoSave();
142
- *
143
- * const store2 = createStore();
144
- * const persister2 = createYjsPersister(store2, doc2);
145
- * await persister2.startAutoLoad();
146
- * await persister2.startAutoSave();
147
- *
148
- * // Synchronize the documents in their initial state.
149
- * await syncDocs();
150
- *
151
- * // Make a change to each of the two Stores.
152
- * store1.setTables({pets: {fido: {species: 'dog'}}});
153
- * store2.setValues({open: true});
154
- * // ...
155
- *
156
- * // Synchronize the documents with each other again.
157
- * await syncDocs();
158
- *
159
- * // Ensure the Stores are in sync.
160
- * console.log(store1.getContent());
161
- * // -> [{pets: {fido: {species: 'dog'}}}, {open: true}]
162
- * console.log(store2.getContent());
163
- * // -> [{pets: {fido: {species: 'dog'}}}, {open: true}]
164
- *
165
- * persister1.destroy();
166
- * persister2.destroy();
167
- * ```
168
- * @category Creation
169
- * @since v4.0.0
170
- */
171
- export function createYjsPersister<Schemas extends OptionalSchemas>(
172
- store: Store<Schemas>,
173
- yDoc: YDoc,
174
- yMapName?: string,
175
- onIgnoredError?: (error: any) => void,
176
- ): YjsPersister<Schemas>;