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,1320 +0,0 @@
1
- /**
2
- * The relationships module of the TinyBase project provides the ability to
3
- * create and track relationships between the data in Store objects.
4
- *
5
- * The main entry point to this module is the createRelationships function,
6
- * which returns a new Relationships object. From there, you can create new
7
- * Relationship definitions, access the associations within those Relationships
8
- * directly, and register listeners for when they change.
9
- * @packageDocumentation
10
- * @module relationships
11
- * @since v1.0.0
12
- */
13
- import type {Id, IdOrNull, Ids} from '../common/index.d.cts';
14
- import type {GetCell, RowCallback, Store} from '../store/index.d.cts';
15
-
16
- /**
17
- * The Relationship type represents the concept of a map that connects one Row
18
- * object to another, often in another Table.
19
- *
20
- * The Relationship has a one-to-many nature. One local Row Id is linked to one
21
- * remote Row Id (in the remote Table), as described by the
22
- * setRelationshipDefinition method - and one remote Row Id may map back to
23
- * multiple local Row Ids (in the local Table).
24
- *
25
- * A Relationship where the local Table is the same as the remote Table can be
26
- * used to model a 'linked list', where Row A references Row B, Row B references
27
- * Row C, and so on.
28
- *
29
- * Note that the Relationship type is not actually used in the API, and you
30
- * instead enumerate and access its structure with the getRemoteRowId method,
31
- * the getLocalRowIds method, and the getLinkedRowIds method.
32
- * @category Concept
33
- * @since v1.0.0
34
- */
35
- export type Relationship = {
36
- remoteRowId: {[localRowId: Id]: Id};
37
- localRowIds: {[remoteRowId: Id]: Ids};
38
- linkedRowIds: {[firstRowId: Id]: Ids};
39
- };
40
-
41
- /**
42
- * The RelationshipCallback type describes a function that takes a
43
- * Relationship's Id and a callback to loop over each local Row within it.
44
- *
45
- * A RelationshipCallback is provided when using the forEachRelationship method,
46
- * so that you can do something based on every Relationship in the Relationships
47
- * object. See that method for specific examples.
48
- * @param relationshipId The Id of the Relationship that the callback can
49
- * operate on.
50
- * @param forEachRow A function that will let you iterate over the local Row
51
- * objects in this Relationship.
52
- * @category Callback
53
- * @since v1.0.0
54
- */
55
- export type RelationshipCallback = (
56
- relationshipId: Id,
57
- forEachRow: (rowCallback: RowCallback) => void,
58
- ) => void;
59
-
60
- /**
61
- * The RelationshipIdsListener type describes a function that is used to listen
62
- * to Relationship definitions being added or removed.
63
- *
64
- * A RelationshipIdsListener is provided when using the
65
- * addRelationshipIdsListener method. See that method for specific examples.
66
- *
67
- * When called, a RelationshipIdsListener is given a reference to the
68
- * Relationships object.
69
- * @param relationships A reference to the Relationships object that changed.
70
- * @category Listener
71
- * @since v1.0.0
72
- */
73
- export type RelationshipIdsListener = (relationships: Relationships) => void;
74
-
75
- /**
76
- * The RemoteRowIdListener type describes a function that is used to listen to
77
- * changes to the remote Row Id end of a Relationship.
78
- *
79
- * A RemoteRowIdListener is provided when using the addRemoteRowIdListener
80
- * method. See that method for specific examples.
81
- *
82
- * When called, a RemoteRowIdListener is given a reference to the Relationships
83
- * object, the Id of the Relationship that changed, and the Id of the local Row
84
- * whose remote Row Id changed.
85
- * @param relationships A reference to the Relationships object that changed.
86
- * @param relationshipId The Id of the Relationship that changed.
87
- * @param localRowId The Id of the local Row whose remote Row Id changed.
88
- * @category Listener
89
- * @since v1.0.0
90
- */
91
- export type RemoteRowIdListener = (
92
- relationships: Relationships,
93
- relationshipId: Id,
94
- localRowId: Id,
95
- ) => void;
96
-
97
- /**
98
- * The LocalRowIdsListener type describes a function that is used to listen to
99
- * changes to the local Row Id ends of a Relationship.
100
- *
101
- * A LocalRowIdsListener is provided when using the addLocalRowIdsListener
102
- * method. See that method for specific examples.
103
- *
104
- * When called, a LocalRowIdsListener is given a reference to the Relationships
105
- * object, the Id of the Relationship that changed, and the Id of the remote Row
106
- * whose local Row Ids changed.
107
- * @param relationships A reference to the Relationships object that changed.
108
- * @param relationshipId The Id of the Relationship that changed.
109
- * @param remoteRowId The Id of the remote Row whose local Row Ids changed.
110
- * @category Listener
111
- * @since v1.0.0
112
- */
113
- export type LocalRowIdsListener = (
114
- relationships: Relationships,
115
- relationshipId: Id,
116
- remoteRowId: Id,
117
- ) => void;
118
-
119
- /**
120
- * The LinkedRowIdsListener type describes a function that is used to listen to
121
- * changes to the local Row Id ends of a Relationship.
122
- *
123
- * A LinkedRowIdsListener is provided when using the addLinkedRowIdsListener
124
- * method. See that method for specific examples.
125
- *
126
- * When called, a LinkedRowIdsListener is given a reference to the Relationships
127
- * object, the Id of the Relationship that changed, and the Id of the first Row
128
- * of the the linked list whose members changed.
129
- * @param relationships A reference to the Relationships object that changed.
130
- * @param relationshipId The Id of the Relationship that changed.
131
- * @param firstRowId The Id of the first Row of the the linked list whose
132
- * members changed.
133
- * @category Listener
134
- * @since v1.0.0
135
- */
136
- export type LinkedRowIdsListener = (
137
- relationships: Relationships,
138
- relationshipId: Id,
139
- firstRowId: Id,
140
- ) => void;
141
-
142
- /**
143
- * The RelationshipsListenerStats type describes the number of listeners
144
- * registered with the Relationships object, and can be used for debugging
145
- * purposes.
146
- *
147
- * A RelationshipsListenerStats object is returned from the getListenerStats
148
- * method.
149
- * @category Development
150
- * @since v1.0.0
151
- */
152
- export type RelationshipsListenerStats = {
153
- /**
154
- * The number of RemoteRowIdListener functions registered with the
155
- * Relationships object.
156
- * @category Stat
157
- * @since v1.0.0
158
- */
159
- remoteRowId: number;
160
- /**
161
- * The number of LocalRowIdsListener functions registered with the
162
- * Relationships object.
163
- * @category Stat
164
- * @since v1.0.0
165
- */
166
- localRowIds: number;
167
- /**
168
- * The number of LinkedRowId functions registered with the Relationships
169
- * object.
170
- * @category Stat
171
- * @since v1.0.0
172
- */
173
- linkedRowIds: number;
174
- };
175
-
176
- /**
177
- * A Relationships object lets you associate a Row in a one Table with the Id of
178
- * a Row in another Table.
179
- *
180
- * This is useful for creating parent-child relationships between the data in
181
- * different Table objects, but it can also be used to model a linked list of
182
- * Row objects in the same Table.
183
- *
184
- * Create a Relationships object easily with the createRelationships function.
185
- * From there, you can add new Relationship definitions (with the
186
- * setRelationshipDefinition method), query their contents (with the
187
- * getRemoteRowId method, the getLocalRowIds method, and the getLinkedRowIds
188
- * method), and add listeners for when they change (with the
189
- * addRemoteRowIdListener method, the addLocalRowIdsListener method, and the
190
- * addLinkedRowIdsListener method).
191
- *
192
- * This module defaults to creating relationships between Row objects by using
193
- * one of their Cell values. However, far more complex relationships can be
194
- * configured with a custom function.
195
- * @example
196
- * This example shows a very simple lifecycle of a Relationships object: from
197
- * creation, to adding definitions (both local/remote table and linked list),
198
- * getting their contents, and then registering and removing listeners for them.
199
- *
200
- * ```js
201
- * import {createRelationships, createStore} from 'tinybase';
202
- *
203
- * const store = createStore()
204
- * .setTable('pets', {
205
- * fido: {species: 'dog', next: 'felix'},
206
- * felix: {species: 'cat', next: 'cujo'},
207
- * cujo: {species: 'dog'},
208
- * })
209
- * .setTable('species', {
210
- * dog: {price: 5},
211
- * cat: {price: 4},
212
- * });
213
- *
214
- * const relationships = createRelationships(store);
215
- *
216
- * // A local/remote table relationship:
217
- * relationships.setRelationshipDefinition(
218
- * 'petSpecies', // relationshipId
219
- * 'pets', // localTableId to link from
220
- * 'species', // remoteTableId to link to
221
- * 'species', // cellId containing remote key
222
- * );
223
- * console.log(relationships.getRemoteRowId('petSpecies', 'fido'));
224
- * // -> 'dog'
225
- * console.log(relationships.getLocalRowIds('petSpecies', 'dog'));
226
- * // -> ['fido', 'cujo']
227
- *
228
- * // A linked list relationship:
229
- * relationships.setRelationshipDefinition(
230
- * 'petSequence', // relationshipId
231
- * 'pets', // localTableId to link from
232
- * 'pets', // the same remoteTableId to link within
233
- * 'next', // cellId containing link key
234
- * );
235
- * console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
236
- * // -> ['fido', 'felix', 'cujo']
237
- *
238
- * const listenerId1 = relationships.addLocalRowIdsListener(
239
- * 'petSpecies',
240
- * 'dog',
241
- * () => {
242
- * console.log('petSpecies relationship (to dog) changed');
243
- * console.log(relationships.getLocalRowIds('petSpecies', 'dog'));
244
- * },
245
- * );
246
- * const listenerId2 = relationships.addLinkedRowIdsListener(
247
- * 'petSequence',
248
- * 'fido',
249
- * () => {
250
- * console.log('petSequence linked list (from fido) changed');
251
- * console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
252
- * },
253
- * );
254
- *
255
- * store.setRow('pets', 'toto', {species: 'dog'});
256
- * // -> 'petSpecies relationship (to dog) changed'
257
- * // -> ['fido', 'cujo', 'toto']
258
- *
259
- * store.setCell('pets', 'cujo', 'next', 'toto');
260
- * // -> 'petSequence linked list (from fido) changed'
261
- * // -> ['fido', 'felix', 'cujo', 'toto']
262
- *
263
- * relationships.delListener(listenerId1);
264
- * relationships.delListener(listenerId2);
265
- * relationships.destroy();
266
- * ```
267
- * @see Using Relationships guide
268
- * @see Drawing demo
269
- * @category Relationships
270
- * @since v1.0.0
271
- */
272
- export interface Relationships {
273
- //
274
- /**
275
- * The setRelationshipDefinition method lets you set the definition of a
276
- * Relationship.
277
- *
278
- * Every Relationship definition is identified by a unique Id, and if you
279
- * re-use an existing Id with this method, the previous definition is
280
- * overwritten.
281
- *
282
- * An Relationship is based on connections between Row objects, often in two
283
- * different Table objects. Therefore the definition requires the
284
- * `localTableId` parameter to specify the 'local' Table to create the
285
- * Relationship from, and the `remoteTableId` parameter to specify the
286
- * 'remote' Table to create Relationship to.
287
- *
288
- * A linked list Relationship is one that has the same Table specified as both
289
- * local Table Id and remote Table Id, allowing you to create a sequence of
290
- * Row objects within that one Table.
291
- *
292
- * A local Row is related to a remote Row by specifying which of its (local)
293
- * Cell values contains the (remote) Row Id, using the `getRemoteRowId`
294
- * parameter. Alternatively, a custom function can be provided that produces
295
- * your own remote Row Id from the local Row as a whole.
296
- * @param relationshipId The Id of the Relationship to define.
297
- * @param localTableId The Id of the local Table for the Relationship.
298
- * @param remoteTableId The Id of the remote Table for the Relationship (or
299
- * the same as the `localTableId` in the case of a linked list).
300
- * @param getRemoteRowId Either the Id of the Cell containing, or a function
301
- * that produces, the Id that is used to indicate which Row in the remote
302
- * Table a local Row is related to.
303
- * @returns A reference to the Relationships object.
304
- * @example
305
- * This example creates a Store, creates a Relationships object, and defines
306
- * a simple Relationship based on the values in the `species` Cell of the
307
- * `pets` Table that relates a Row to another in the `species` Table.
308
- *
309
- * ```js
310
- * import {createRelationships, createStore} from 'tinybase';
311
- *
312
- * const store = createStore()
313
- * .setTable('pets', {
314
- * fido: {species: 'dog'},
315
- * felix: {species: 'cat'},
316
- * cujo: {species: 'dog'},
317
- * })
318
- * .setTable('species', {
319
- * dog: {price: 5},
320
- * cat: {price: 4},
321
- * });
322
- *
323
- * const relationships = createRelationships(store);
324
- * relationships.setRelationshipDefinition(
325
- * 'petSpecies', // relationshipId
326
- * 'pets', // localTableId to link from
327
- * 'species', // remoteTableId to link to
328
- * 'species', // cellId containing remote key
329
- * );
330
- *
331
- * console.log(relationships.getRemoteRowId('petSpecies', 'fido'));
332
- * // -> 'dog'
333
- * console.log(relationships.getLocalRowIds('petSpecies', 'dog'));
334
- * // -> ['fido', 'cujo']
335
- * ```
336
- * @example
337
- * This example creates a Store, creates a Relationships object, and defines
338
- * a linked list Relationship based on the values in the `next` Cell of the
339
- * `pets` Table that relates a Row to another in the same Table.
340
- *
341
- * ```js
342
- * import {createRelationships, createStore} from 'tinybase';
343
- *
344
- * const store = createStore().setTable('pets', {
345
- * fido: {species: 'dog', next: 'felix'},
346
- * felix: {species: 'cat', next: 'cujo'},
347
- * cujo: {species: 'dog'},
348
- * });
349
- *
350
- * const relationships = createRelationships(store);
351
- * relationships.setRelationshipDefinition(
352
- * 'petSequence', // relationshipId
353
- * 'pets', // localTableId to link from
354
- * 'pets', // the same remoteTableId to link within
355
- * 'next', // cellId containing link key
356
- * );
357
- *
358
- * console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
359
- * // -> ['fido', 'felix', 'cujo']
360
- * ```
361
- * @category Configuration
362
- * @since v1.0.0
363
- */
364
- setRelationshipDefinition(
365
- relationshipId: Id,
366
- localTableId: Id,
367
- remoteTableId: Id,
368
- getRemoteRowId: Id | ((getCell: GetCell, localRowId: Id) => Id),
369
- ): Relationships;
370
-
371
- /**
372
- * The delRelationshipDefinition method removes an existing Relationship
373
- * definition.
374
- * @param relationshipId The Id of the Relationship to remove.
375
- * @returns A reference to the Relationships object.
376
- * @example
377
- * This example creates a Store, creates a Relationships object, defines a
378
- * simple Relationship, and then removes it.
379
- *
380
- * ```js
381
- * import {createRelationships, createStore} from 'tinybase';
382
- *
383
- * const store = createStore()
384
- * .setTable('pets', {
385
- * fido: {species: 'dog'},
386
- * felix: {species: 'cat'},
387
- * cujo: {species: 'dog'},
388
- * })
389
- * .setTable('species', {
390
- * dog: {price: 5},
391
- * cat: {price: 4},
392
- * });
393
- *
394
- * const relationships = createRelationships(store);
395
- * relationships.setRelationshipDefinition(
396
- * 'petSpecies',
397
- * 'pets',
398
- * 'species',
399
- * 'species',
400
- * );
401
- * console.log(relationships.getRelationshipIds());
402
- * // -> ['petSpecies']
403
- *
404
- * relationships.delRelationshipDefinition('petSpecies');
405
- * console.log(relationships.getRelationshipIds());
406
- * // -> []
407
- * ```
408
- * @category Configuration
409
- * @since v1.0.0
410
- */
411
- delRelationshipDefinition(relationshipId: Id): Relationships;
412
-
413
- /**
414
- * The getStore method returns a reference to the underlying Store that is
415
- * backing this Relationships object.
416
- * @returns A reference to the Store.
417
- * @example
418
- * This example creates a Relationships object against a newly-created Store
419
- * and then gets its reference in order to update its data.
420
- *
421
- * ```js
422
- * import {createRelationships, createStore} from 'tinybase';
423
- *
424
- * const relationships = createRelationships(createStore());
425
- * relationships.setRelationshipDefinition(
426
- * 'petSpecies',
427
- * 'pets',
428
- * 'species',
429
- * 'species',
430
- * );
431
- * relationships.getStore().setCell('pets', 'fido', 'species', 'dog');
432
- * console.log(relationships.getRemoteRowId('petSpecies', 'fido'));
433
- * // -> 'dog'
434
- * ```
435
- * @category Getter
436
- * @since v1.0.0
437
- */
438
- getStore(): Store;
439
-
440
- /**
441
- * The getRelationshipIds method returns an array of the Relationship Ids
442
- * registered with this Relationships object.
443
- * @returns An array of Ids.
444
- * @example
445
- * This example creates a Relationships object with two definitions, and then
446
- * gets the Ids of the definitions.
447
- *
448
- * ```js
449
- * import {createRelationships, createStore} from 'tinybase';
450
- *
451
- * const relationships = createRelationships(createStore())
452
- * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
453
- * .setRelationshipDefinition('petSequence', 'pets', 'pets', 'next');
454
- * console.log(relationships.getRelationshipIds());
455
- * // -> ['petSpecies', 'petSequence']
456
- * ```
457
- * @category Getter
458
- * @since v1.0.0
459
- */
460
- getRelationshipIds(): Ids;
461
-
462
- /**
463
- * The forEachRelationship method takes a function that it will then call for
464
- * each Relationship in a specified Relationships object.
465
- *
466
- * This method is useful for iterating over the structure of the Relationships
467
- * object in a functional style. The `relationshipCallback` parameter is a
468
- * RelationshipCallback function that will be called with the Id of each
469
- * Relationship, and with a function that can then be used to iterate over
470
- * each local Row involved in the Relationship.
471
- * @param relationshipCallback The function that should be called for every
472
- * Relationship.
473
- * @example
474
- * This example iterates over each Relationship in a Relationships object, and
475
- * lists each Row Id within them.
476
- *
477
- * ```js
478
- * import {createRelationships, createStore} from 'tinybase';
479
- *
480
- * const store = createStore().setTable('pets', {
481
- * fido: {species: 'dog', next: 'felix'},
482
- * felix: {species: 'cat', next: 'cujo'},
483
- * cujo: {species: 'dog'},
484
- * });
485
- * const relationships = createRelationships(store)
486
- * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
487
- * .setRelationshipDefinition('petSequence', 'pets', 'pets', 'next');
488
- *
489
- * relationships.forEachRelationship((relationshipId, forEachRow) => {
490
- * console.log(relationshipId);
491
- * forEachRow((rowId) => console.log(`- ${rowId}`));
492
- * });
493
- * // -> 'petSpecies'
494
- * // -> '- fido'
495
- * // -> '- felix'
496
- * // -> '- cujo'
497
- * // -> 'petSequence'
498
- * // -> '- fido'
499
- * // -> '- felix'
500
- * // -> '- cujo'
501
- * ```
502
- * @category Iterator
503
- * @since v1.0.0
504
- */
505
- forEachRelationship(relationshipCallback: RelationshipCallback): void;
506
-
507
- /**
508
- * The hasRelationship method returns a boolean indicating whether a given
509
- * Relationship exists in the Relationships object.
510
- * @param relationshipId The Id of a possible Relationship in the
511
- * Relationships object.
512
- * @returns Whether a Relationship with that Id exists.
513
- * @example
514
- * This example shows two simple Relationship existence checks.
515
- *
516
- * ```js
517
- * import {createRelationships, createStore} from 'tinybase';
518
- *
519
- * const relationships = createRelationships(
520
- * createStore(),
521
- * ).setRelationshipDefinition('petSpecies', 'pets', 'species', 'species');
522
- * console.log(relationships.hasRelationship('petSpecies'));
523
- * // -> true
524
- * console.log(relationships.hasRelationship('petColor'));
525
- * // -> false
526
- * ```
527
- * @category Getter
528
- * @since v1.0.0
529
- */
530
- hasRelationship(relationshipId: Id): boolean;
531
-
532
- /**
533
- * The getLocalTableId method returns the Id of the underlying local Table
534
- * that is used in the Relationship.
535
- *
536
- * If the Relationship Id is invalid, the method returns `undefined`.
537
- * @param relationshipId The Id of a Relationship.
538
- * @returns The Id of the local Table backing the Relationship, or
539
- * `undefined`.
540
- * @example
541
- * This example creates a Relationship object, a single Relationship
542
- * definition, and then queries it (and a non-existent definition) to get the
543
- * underlying local Table Id.
544
- *
545
- * ```js
546
- * import {createRelationships, createStore} from 'tinybase';
547
- *
548
- * const relationships = createRelationships(createStore());
549
- * relationships.setRelationshipDefinition(
550
- * 'petSpecies',
551
- * 'pets',
552
- * 'species',
553
- * 'species',
554
- * );
555
- *
556
- * console.log(relationships.getLocalTableId('petSpecies'));
557
- * // -> 'pets'
558
- * console.log(relationships.getLocalTableId('petColor'));
559
- * // -> undefined
560
- * ```
561
- * @category Getter
562
- * @since v1.0.0
563
- */
564
- getLocalTableId(relationshipId: Id): Id | undefined;
565
-
566
- /**
567
- * The getRemoteTableId method returns the Id of the underlying remote Table
568
- * that is used in the Relationship.
569
- *
570
- * If the Relationship Id is invalid, the method returns `undefined`.
571
- * @param relationshipId The Id of a Relationship.
572
- * @returns The Id of the remote Table backing the Relationship, or
573
- * `undefined`.
574
- * @example
575
- * This example creates a Relationship object, a single Relationship
576
- * definition, and then queries it (and a non-existent definition) to get the
577
- * underlying remote Table Id.
578
- *
579
- * ```js
580
- * import {createRelationships, createStore} from 'tinybase';
581
- *
582
- * const relationships = createRelationships(createStore());
583
- * relationships.setRelationshipDefinition(
584
- * 'petSpecies',
585
- * 'pets',
586
- * 'species',
587
- * 'species',
588
- * );
589
- *
590
- * console.log(relationships.getRemoteTableId('petSpecies'));
591
- * // -> 'species'
592
- * console.log(relationships.getRemoteTableId('petColor'));
593
- * // -> undefined
594
- * ```
595
- * @category Getter
596
- * @since v1.0.0
597
- */
598
- getRemoteTableId(relationshipId: Id): Id | undefined;
599
-
600
- /**
601
- * The getRemoteRowId method gets the remote Row Id for a given local Row in a
602
- * Relationship.
603
- *
604
- * If the identified Relationship or Row does not exist (or if the definition
605
- * references a Table that does not exist) then `undefined` is returned.
606
- * @param relationshipId The Id of the Relationship.
607
- * @param localRowId The Id of the local Row in the Relationship.
608
- * @returns The remote Row Id in the Relationship, or `undefined`.
609
- * @example
610
- * This example creates a Store, creates a Relationships object, and defines
611
- * a simple Relationship. It then uses getRemoteRowId to see the remote Row Id
612
- * in the Relationship (and also the remote Row Ids for a local Row that does
613
- * not exist, and for a Relationship that has not been defined).
614
- *
615
- * ```js
616
- * import {createRelationships, createStore} from 'tinybase';
617
- *
618
- * const store = createStore()
619
- * .setTable('pets', {
620
- * fido: {species: 'dog'},
621
- * felix: {species: 'cat'},
622
- * cujo: {species: 'dog'},
623
- * })
624
- * .setTable('species', {
625
- * dog: {price: 5},
626
- * cat: {price: 4},
627
- * });
628
- *
629
- * const relationships = createRelationships(store);
630
- * relationships.setRelationshipDefinition(
631
- * 'petSpecies',
632
- * 'pets',
633
- * 'species',
634
- * 'species',
635
- * );
636
- *
637
- * console.log(relationships.getRemoteRowId('petSpecies', 'fido'));
638
- * // -> 'dog'
639
- * console.log(relationships.getRemoteRowId('petSpecies', 'toto'));
640
- * // -> undefined
641
- * console.log(relationships.getRemoteRowId('petColor', 'fido'));
642
- * // -> undefined
643
- * ```
644
- * @category Getter
645
- * @since v1.0.0
646
- */
647
- getRemoteRowId(relationshipId: Id, localRowId: Id): Id | undefined;
648
-
649
- /**
650
- * The getLocalRowIds method gets the local Row Ids for a given remote Row in
651
- * a Relationship.
652
- *
653
- * If the identified Relationship or Row does not exist (or if the definition
654
- * references a Table that does not exist) then an empty array is returned.
655
- * @param relationshipId The Id of the Relationship.
656
- * @param remoteRowId The Id of the remote Row in the Relationship.
657
- * @returns The local Row Ids in the Relationship, or an empty array.
658
- * @example
659
- * This example creates a Store, creates a Relationships object, and defines
660
- * a simple Relationship. It then uses getLocalRowIds to see the local Row Ids
661
- * in the Relationship (and also the local Row Ids for a remote Row that does
662
- * not exist, and for a Relationship that has not been defined).
663
- *
664
- * ```js
665
- * import {createRelationships, createStore} from 'tinybase';
666
- *
667
- * const store = createStore()
668
- * .setTable('pets', {
669
- * fido: {species: 'dog'},
670
- * felix: {species: 'cat'},
671
- * cujo: {species: 'dog'},
672
- * })
673
- * .setTable('species', {
674
- * dog: {price: 5},
675
- * cat: {price: 4},
676
- * });
677
- *
678
- * const relationships = createRelationships(store);
679
- * relationships.setRelationshipDefinition(
680
- * 'petSpecies',
681
- * 'pets',
682
- * 'species',
683
- * 'species',
684
- * );
685
- *
686
- * console.log(relationships.getLocalRowIds('petSpecies', 'dog'));
687
- * // -> ['fido', 'cujo']
688
- * console.log(relationships.getLocalRowIds('petSpecies', 'worm'));
689
- * // -> []
690
- * console.log(relationships.getLocalRowIds('petColor', 'brown'));
691
- * // -> []
692
- * ```
693
- * @category Getter
694
- * @since v1.0.0
695
- */
696
- getLocalRowIds(relationshipId: Id, remoteRowId: Id): Ids;
697
-
698
- /**
699
- * The getLinkedRowIds method gets the linked Row Ids for a given Row in a
700
- * linked list Relationship.
701
- *
702
- * A linked list Relationship is one that has the same Table specified as both
703
- * local Table Id and remote Table Id, allowing you to create a sequence of
704
- * Row objects within that one Table.
705
- *
706
- * If the identified Relationship or Row does not exist (or if the definition
707
- * references a Table that does not exist) then an array containing just the
708
- * first Row Id is returned.
709
- * @param relationshipId The Id of the Relationship.
710
- * @param firstRowId The Id of the first Row in the linked list Relationship.
711
- * @returns The linked Row Ids in the Relationship.
712
- * @example
713
- * This example creates a Store, creates a Relationships object, and defines
714
- * a simple linked list Relationship. It then uses getLinkedRowIds to see the
715
- * linked Row Ids in the Relationship (and also the linked Row Ids for a Row
716
- * that does not exist, and for a Relationship that has not been defined).
717
- *
718
- * ```js
719
- * import {createRelationships, createStore} from 'tinybase';
720
- *
721
- * const store = createStore().setTable('pets', {
722
- * fido: {species: 'dog', next: 'felix'},
723
- * felix: {species: 'cat', next: 'cujo'},
724
- * cujo: {species: 'dog'},
725
- * });
726
- *
727
- * const relationships = createRelationships(store);
728
- * relationships.setRelationshipDefinition(
729
- * 'petSequence',
730
- * 'pets',
731
- * 'pets',
732
- * 'next',
733
- * );
734
- *
735
- * console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
736
- * // -> ['fido', 'felix', 'cujo']
737
- * console.log(relationships.getLinkedRowIds('petSequence', 'felix'));
738
- * // -> ['felix', 'cujo']
739
- * console.log(relationships.getLinkedRowIds('petSequence', 'toto'));
740
- * // -> ['toto']
741
- * console.log(relationships.getLinkedRowIds('petFriendships', 'fido'));
742
- * // -> ['fido']
743
- * ```
744
- * @category Getter
745
- * @since v1.0.0
746
- */
747
- getLinkedRowIds(relationshipId: Id, firstRowId: Id): Ids;
748
-
749
- /**
750
- * The addRelationshipIdsListener method registers a listener function with
751
- * the Relationships object that will be called whenever a Relationship
752
- * definition is added or removed.
753
- *
754
- * The provided listener is a RelationshipIdsListener function, and will be
755
- * called with a reference to the Relationships object.
756
- * @param listener The function that will be called whenever a Relationship
757
- * definition is added or removed.
758
- * @example
759
- * This example creates a Store, a Relationships object, and then registers a
760
- * listener that responds to the addition and the removal of a Relationship
761
- * definition.
762
- *
763
- * ```js
764
- * import {createRelationships, createStore} from 'tinybase';
765
- *
766
- * const store = createStore()
767
- * .setTable('pets', {
768
- * fido: {species: 'dog'},
769
- * felix: {species: 'cat'},
770
- * cujo: {species: 'dog'},
771
- * })
772
- * .setTable('species', {
773
- * wolf: {price: 10},
774
- * dog: {price: 5},
775
- * cat: {price: 4},
776
- * });
777
- *
778
- * const relationships = createRelationships(store);
779
- * const listenerId = relationships.addRelationshipIdsListener(
780
- * (relationships) => {
781
- * console.log(relationships.getRelationshipIds());
782
- * },
783
- * );
784
- *
785
- * relationships.setRelationshipDefinition(
786
- * 'petSpecies',
787
- * 'pets',
788
- * 'species',
789
- * 'species',
790
- * );
791
- * // -> ['petSpecies']
792
- * relationships.delRelationshipDefinition('petSpecies');
793
- * // -> []
794
- *
795
- * relationships.delListener(listenerId);
796
- * ```
797
- * @category Listener
798
- * @since v4.1.0
799
- */
800
- addRelationshipIdsListener(listener: RelationshipIdsListener): Id;
801
-
802
- /**
803
- * The addRemoteRowIdListener method registers a listener function with the
804
- * Relationships object that will be called whenever a remote Row Id in a
805
- * Relationship changes.
806
- *
807
- * You can either listen to a single local Row (by specifying the Relationship
808
- * Id and local Row Id as the method's first two parameters), or changes to
809
- * any local Row (by providing a `null` wildcards).
810
- *
811
- * Both, either, or neither of the `relationshipId` and `localRowId`
812
- * parameters can be wildcarded with `null`. You can listen to a specific
813
- * local Row in a specific Relationship, any local Row in a specific
814
- * Relationship, a specific local Row in any Relationship, or any local Row in
815
- * any Relationship.
816
- *
817
- * The provided listener is a RemoteRowIdListener function, and will be called
818
- * with a reference to the Relationships object, the Id of the Relationship,
819
- * and the Id of the local Row that had its remote Row change.
820
- * @param relationshipId The Id of the Relationship to listen to, or `null` as
821
- * a wildcard.
822
- * @param localRowId The Id of the local Row to listen to, or `null` as a
823
- * wildcard.
824
- * @param listener The function that will be called whenever the remote Row Id
825
- * changes.
826
- * @returns A unique Id for the listener that can later be used to remove it.
827
- * @example
828
- * This example creates a Store, a Relationships object, and then registers a
829
- * listener that responds to any changes to a specific local Row's remote Row.
830
- *
831
- * ```js
832
- * import {createRelationships, createStore} from 'tinybase';
833
- *
834
- * const store = createStore()
835
- * .setTable('pets', {
836
- * fido: {species: 'dog'},
837
- * felix: {species: 'cat'},
838
- * cujo: {species: 'dog'},
839
- * })
840
- * .setTable('species', {
841
- * wolf: {price: 10},
842
- * dog: {price: 5},
843
- * cat: {price: 4},
844
- * });
845
- *
846
- * const relationships = createRelationships(store);
847
- * relationships.setRelationshipDefinition(
848
- * 'petSpecies',
849
- * 'pets',
850
- * 'species',
851
- * 'species',
852
- * );
853
- *
854
- * const listenerId = relationships.addRemoteRowIdListener(
855
- * 'petSpecies',
856
- * 'cujo',
857
- * (relationships) => {
858
- * console.log('petSpecies relationship (from cujo) changed');
859
- * console.log(relationships.getRemoteRowId('petSpecies', 'cujo'));
860
- * },
861
- * );
862
- *
863
- * store.setCell('pets', 'cujo', 'species', 'wolf');
864
- * // -> 'petSpecies relationship (from cujo) changed'
865
- * // -> 'wolf'
866
- *
867
- * relationships.delListener(listenerId);
868
- * ```
869
- * @example
870
- * This example creates a Store, a Relationships object, and then registers a
871
- * listener that responds to any changes to any local Row's remote Row. It
872
- * also illustrates how you can use the getStore method and the getRemoteRowId
873
- * method to resolve the remote Row as a whole.
874
- *
875
- * ```js
876
- * import {createRelationships, createStore} from 'tinybase';
877
- *
878
- * const store = createStore()
879
- * .setTable('pets', {
880
- * fido: {species: 'dog', color: 'brown'},
881
- * felix: {species: 'cat', color: 'black'},
882
- * cujo: {species: 'dog', color: 'brown'},
883
- * })
884
- * .setTable('species', {
885
- * wolf: {price: 10},
886
- * dog: {price: 5},
887
- * cat: {price: 4},
888
- * })
889
- * .setTable('color', {
890
- * brown: {discount: 0.1},
891
- * black: {discount: 0},
892
- * grey: {discount: 0.2},
893
- * });
894
- *
895
- * const relationships = createRelationships(store)
896
- * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
897
- * .setRelationshipDefinition('petColor', 'pets', 'color', 'color');
898
- *
899
- * const listenerId = relationships.addRemoteRowIdListener(
900
- * null,
901
- * null,
902
- * (relationships, relationshipId, localRowId) => {
903
- * console.log(
904
- * `${relationshipId} relationship (from ${localRowId}) changed`,
905
- * );
906
- * console.log(relationships.getRemoteRowId(relationshipId, localRowId));
907
- * console.log(
908
- * relationships
909
- * .getStore()
910
- * .getRow(
911
- * relationships.getRemoteTableId(relationshipId),
912
- * relationships.getRemoteRowId(relationshipId, localRowId),
913
- * ),
914
- * );
915
- * },
916
- * );
917
- *
918
- * store.setRow('pets', 'cujo', {species: 'wolf', color: 'grey'});
919
- * // -> 'petSpecies relationship (from cujo) changed'
920
- * // -> 'wolf'
921
- * // -> {price: 10}
922
- * // -> 'petColor relationship (from cujo) changed'
923
- * // -> 'grey'
924
- * // -> {discount: 0.2}
925
- *
926
- * relationships.delListener(listenerId);
927
- * ```
928
- * @category Listener
929
- * @since v1.0.0
930
- */
931
- addRemoteRowIdListener(
932
- relationshipId: IdOrNull,
933
- localRowId: IdOrNull,
934
- listener: RemoteRowIdListener,
935
- ): Id;
936
-
937
- /**
938
- * The addLocalRowIdsListener method registers a listener function with the
939
- * Relationships object that will be called whenever the local Row Ids in
940
- * a Relationship change.
941
- *
942
- * You can either listen to a single local Row (by specifying the Relationship
943
- * Id and local Row Id as the method's first two parameters), or changes to
944
- * any local Row (by providing a `null` wildcards).
945
- *
946
- * Both, either, or neither of the `relationshipId` and `remoteRowId`
947
- * parameters can be wildcarded with `null`. You can listen to a specific
948
- * remote Row in a specific Relationship, any remote Row in a specific
949
- * Relationship, a specific remote Row in any Relationship, or any remote Row
950
- * in any Relationship.
951
- *
952
- * The provided listener is a LocalRowIdsListener function, and will be called
953
- * with a reference to the Relationships object, the Id of the Relationship,
954
- * and the Id of the remote Row that had its local Row objects change.
955
- * @param relationshipId The Id of the Relationship to listen to, or `null` as
956
- * a wildcard.
957
- * @param remoteRowId The Id of the remote Row to listen to, or `null` as a
958
- * wildcard.
959
- * @param listener The function that will be called whenever the local Row Ids
960
- * change.
961
- * @returns A unique Id for the listener that can later be used to remove it.
962
- * @example
963
- * This example creates a Store, a Relationships object, and then registers a
964
- * listener that responds to any changes to a specific remote Row's local Row
965
- * objects.
966
- *
967
- * ```js
968
- * import {createRelationships, createStore} from 'tinybase';
969
- *
970
- * const store = createStore()
971
- * .setTable('pets', {
972
- * fido: {species: 'dog'},
973
- * felix: {species: 'cat'},
974
- * cujo: {species: 'dog'},
975
- * })
976
- * .setTable('species', {
977
- * wolf: {price: 10},
978
- * dog: {price: 5},
979
- * cat: {price: 4},
980
- * });
981
- *
982
- * const relationships = createRelationships(store);
983
- * relationships.setRelationshipDefinition(
984
- * 'petSpecies',
985
- * 'pets',
986
- * 'species',
987
- * 'species',
988
- * );
989
- *
990
- * const listenerId = relationships.addLocalRowIdsListener(
991
- * 'petSpecies',
992
- * 'dog',
993
- * (relationships) => {
994
- * console.log('petSpecies relationship (to dog) changed');
995
- * console.log(relationships.getLocalRowIds('petSpecies', 'dog'));
996
- * },
997
- * );
998
- *
999
- * store.setRow('pets', 'toto', {species: 'dog'});
1000
- * // -> 'petSpecies relationship (to dog) changed'
1001
- * // -> ['fido', 'cujo', 'toto']
1002
- *
1003
- * relationships.delListener(listenerId);
1004
- * ```
1005
- * @example
1006
- * This example creates a Store, a Relationships object, and then registers a
1007
- * listener that responds to any changes to any remote Row's local Row
1008
- * objects.
1009
- *
1010
- * ```js
1011
- * import {createRelationships, createStore} from 'tinybase';
1012
- *
1013
- * const store = createStore()
1014
- * .setTable('pets', {
1015
- * fido: {species: 'dog', color: 'brown'},
1016
- * felix: {species: 'cat', color: 'black'},
1017
- * cujo: {species: 'dog', color: 'brown'},
1018
- * toto: {species: 'dog', color: 'grey'},
1019
- * })
1020
- * .setTable('species', {
1021
- * wolf: {price: 10},
1022
- * dog: {price: 5},
1023
- * cat: {price: 4},
1024
- * })
1025
- * .setTable('color', {
1026
- * brown: {discount: 0.1},
1027
- * black: {discount: 0},
1028
- * grey: {discount: 0.2},
1029
- * });
1030
- *
1031
- * const relationships = createRelationships(store)
1032
- * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
1033
- * .setRelationshipDefinition('petColor', 'pets', 'color', 'color');
1034
- *
1035
- * const listenerId = relationships.addLocalRowIdsListener(
1036
- * null,
1037
- * null,
1038
- * (relationships, relationshipId, remoteRowId) => {
1039
- * console.log(
1040
- * `${relationshipId} relationship (to ${remoteRowId}) changed`,
1041
- * );
1042
- * console.log(relationships.getLocalRowIds(relationshipId, remoteRowId));
1043
- * },
1044
- * );
1045
- *
1046
- * store.setRow('pets', 'cujo', {species: 'wolf', color: 'grey'});
1047
- * // -> 'petSpecies relationship (to dog) changed'
1048
- * // -> ['fido', 'toto']
1049
- * // -> 'petSpecies relationship (to wolf) changed'
1050
- * // -> ['cujo']
1051
- * // -> 'petColor relationship (to brown) changed'
1052
- * // -> ['fido']
1053
- * // -> 'petColor relationship (to grey) changed'
1054
- * // -> ['toto', 'cujo']
1055
- *
1056
- * relationships.delListener(listenerId);
1057
- * ```
1058
- * @category Listener
1059
- * @since v1.0.0
1060
- */
1061
- addLocalRowIdsListener(
1062
- relationshipId: IdOrNull,
1063
- remoteRowId: IdOrNull,
1064
- listener: LocalRowIdsListener,
1065
- ): Id;
1066
-
1067
- /**
1068
- * The addLinkedRowIdsListener method registers a listener function with the
1069
- * Relationships object that will be called whenever the linked Row Ids in a
1070
- * linked list Relationship change.
1071
- *
1072
- * A linked list Relationship is one that has the same Table specified as both
1073
- * local Table Id and remote Table Id, allowing you to create a sequence of
1074
- * Row objects within that one Table.
1075
- *
1076
- * You listen to changes to a linked list starting from a single first Row by
1077
- * specifying the Relationship Id and local Row Id as the method's first two
1078
- * parameters.
1079
- *
1080
- * Unlike other listener registration methods, you cannot provide `null`
1081
- * wildcards for the first two parameters of the addLinkedRowIdsListener
1082
- * method. This prevents the prohibitive expense of tracking all the possible
1083
- * linked lists (and partial linked lists within them) in a Store.
1084
- *
1085
- * The provided listener is a LinkedRowIdsListener function, and will be
1086
- * called with a reference to the Relationships object, the Id of the
1087
- * Relationship, and the Id of the first Row that had its linked list change.
1088
- * @param relationshipId The Id of the Relationship to listen to.
1089
- * @param firstRowId The Id of the first Row of the linked list to listen to.
1090
- * @param listener The function that will be called whenever the linked Row
1091
- * Ids change.
1092
- * @returns A unique Id for the listener that can later be used to remove it.
1093
- * @example
1094
- * This example creates a Store, a Relationships object, and then registers a
1095
- * listener that responds to any changes to a specific first Row's linked Row
1096
- * objects.
1097
- *
1098
- * ```js
1099
- * import {createRelationships, createStore} from 'tinybase';
1100
- *
1101
- * const store = createStore().setTable('pets', {
1102
- * fido: {species: 'dog', next: 'felix'},
1103
- * felix: {species: 'cat', next: 'cujo'},
1104
- * cujo: {species: 'dog'},
1105
- * });
1106
- *
1107
- * const relationships = createRelationships(store);
1108
- * relationships.setRelationshipDefinition(
1109
- * 'petSequence',
1110
- * 'pets',
1111
- * 'pets',
1112
- * 'next',
1113
- * );
1114
- *
1115
- * const listenerId = relationships.addLinkedRowIdsListener(
1116
- * 'petSequence',
1117
- * 'fido',
1118
- * (relationships) => {
1119
- * console.log('petSequence linked list (from fido) changed');
1120
- * console.log(relationships.getLinkedRowIds('petSequence', 'fido'));
1121
- * },
1122
- * );
1123
- *
1124
- * store.setRow('pets', 'toto', {species: 'dog'});
1125
- * store.setCell('pets', 'cujo', 'next', 'toto');
1126
- * // -> 'petSequence linked list (from fido) changed'
1127
- * // -> ['fido', 'felix', 'cujo', 'toto']
1128
- *
1129
- * relationships.delListener(listenerId);
1130
- * ```
1131
- * @category Listener
1132
- * @since v1.0.0
1133
- */
1134
- addLinkedRowIdsListener(
1135
- relationshipId: Id,
1136
- firstRowId: Id,
1137
- listener: LinkedRowIdsListener,
1138
- ): Id;
1139
-
1140
- /**
1141
- * The delListener method removes a listener that was previously added to the
1142
- * Relationships object.
1143
- *
1144
- * Use the Id returned by whichever method was used to add the listener. Note
1145
- * that the Relationships object may re-use this Id for future listeners added
1146
- * to it.
1147
- * @param listenerId The Id of the listener to remove.
1148
- * @returns A reference to the Relationships object.
1149
- * @example
1150
- * This example creates a Store, a Relationships object, registers a listener,
1151
- * and then removes it.
1152
- *
1153
- * ```js
1154
- * import {createRelationships, createStore} from 'tinybase';
1155
- *
1156
- * const store = createStore()
1157
- * .setTable('pets', {
1158
- * fido: {species: 'dog'},
1159
- * felix: {species: 'cat'},
1160
- * cujo: {species: 'dog'},
1161
- * })
1162
- * .setTable('species', {
1163
- * wolf: {price: 10},
1164
- * dog: {price: 5},
1165
- * cat: {price: 4},
1166
- * });
1167
- *
1168
- * const relationships = createRelationships(store);
1169
- * relationships.setRelationshipDefinition(
1170
- * 'petSpecies',
1171
- * 'pets',
1172
- * 'species',
1173
- * 'species',
1174
- * );
1175
- *
1176
- * const listenerId = relationships.addLocalRowIdsListener(
1177
- * 'petSpecies',
1178
- * 'dog',
1179
- * () => {
1180
- * console.log('petSpecies relationship (to dog) changed');
1181
- * },
1182
- * );
1183
- *
1184
- * store.setRow('pets', 'toto', {species: 'dog'});
1185
- * // -> 'petSpecies relationship (to dog) changed'
1186
- *
1187
- * relationships.delListener(listenerId);
1188
- *
1189
- * store.setRow('pets', 'toto', {species: 'dog'});
1190
- * // -> undefined
1191
- * // The listener is not called.
1192
- * ```
1193
- * @category Listener
1194
- * @since v1.0.0
1195
- */
1196
- delListener(listenerId: Id): Relationships;
1197
-
1198
- /**
1199
- * The destroy method should be called when this Relationships object is no
1200
- * longer used.
1201
- *
1202
- * This guarantees that all of the listeners that the object registered with
1203
- * the underlying Store are removed and it can be correctly garbage collected.
1204
- * @example
1205
- * This example creates a Store, adds a Relationships object with a
1206
- * definition (that registers a RowListener with the underlying Store),
1207
- * and then destroys it again, removing the listener.
1208
- *
1209
- * ```js
1210
- * import {createRelationships, createStore} from 'tinybase';
1211
- *
1212
- * const store = createStore()
1213
- * .setTable('pets', {
1214
- * fido: {species: 'dog'},
1215
- * felix: {species: 'cat'},
1216
- * cujo: {species: 'dog'},
1217
- * })
1218
- * .setTable('species', {
1219
- * wolf: {price: 10},
1220
- * dog: {price: 5},
1221
- * cat: {price: 4},
1222
- * });
1223
- *
1224
- * const relationships = createRelationships(store);
1225
- * relationships.setRelationshipDefinition(
1226
- * 'petSpecies',
1227
- * 'pets',
1228
- * 'species',
1229
- * 'species',
1230
- * );
1231
- * console.log(store.getListenerStats().row);
1232
- * // -> 1
1233
- *
1234
- * relationships.destroy();
1235
- *
1236
- * console.log(store.getListenerStats().row);
1237
- * // -> 0
1238
- * ```
1239
- * @category Lifecycle
1240
- * @since v1.0.0
1241
- */
1242
- destroy(): void;
1243
-
1244
- /**
1245
- * The getListenerStats method provides a set of statistics about the
1246
- * listeners registered with the Relationships object, and is used for
1247
- * debugging purposes.
1248
- *
1249
- * The RelationshipsListenerStats object contains a breakdown of the different
1250
- * types of listener.
1251
- *
1252
- * The method is intended to be used during development to ensure your
1253
- * application is not leaking listener registrations, for example.
1254
- * @returns A RelationshipsListenerStats object containing Relationships
1255
- * listener statistics.
1256
- * @example
1257
- * This example gets the listener statistics of a Relationships object.
1258
- *
1259
- * ```js
1260
- * import {createRelationships, createStore} from 'tinybase';
1261
- *
1262
- * const store = createStore();
1263
- * const relationships = createRelationships(store);
1264
- * relationships.addRemoteRowIdListener(null, null, () => {
1265
- * console.log('Remote Row Id changed');
1266
- * });
1267
- * relationships.addLocalRowIdsListener(null, null, () => {
1268
- * console.log('Local Row Id changed');
1269
- * });
1270
- *
1271
- * const listenerStats = relationships.getListenerStats();
1272
- * console.log(listenerStats.remoteRowId);
1273
- * // -> 1
1274
- * console.log(listenerStats.localRowIds);
1275
- * // -> 1
1276
- * ```
1277
- * @category Development
1278
- * @since v1.0.0
1279
- */
1280
- getListenerStats(): RelationshipsListenerStats;
1281
- //
1282
- }
1283
-
1284
- /**
1285
- * The createRelationships function creates a Relationships object, and is the
1286
- * main entry point into the relationships module.
1287
- *
1288
- * A given Store can only have one Relationships object associated with it. If
1289
- * you call this function twice on the same Store, your second call will return
1290
- * a reference to the Relationships object created by the first.
1291
- * @param store The Store for which to register Relationships.
1292
- * @returns A reference to the new Relationships object.
1293
- * @example
1294
- * This example creates a Relationships object.
1295
- *
1296
- * ```js
1297
- * import {createRelationships, createStore} from 'tinybase';
1298
- *
1299
- * const store = createStore();
1300
- * const relationships = createRelationships(store);
1301
- * console.log(relationships.getRelationshipIds());
1302
- * // -> []
1303
- * ```
1304
- * @example
1305
- * This example creates a Relationships object, and calls the method a second
1306
- * time for the same Store to return the same object.
1307
- *
1308
- * ```js
1309
- * import {createRelationships, createStore} from 'tinybase';
1310
- *
1311
- * const store = createStore();
1312
- * const relationships1 = createRelationships(store);
1313
- * const relationships2 = createRelationships(store);
1314
- * console.log(relationships1 === relationships2);
1315
- * // -> true
1316
- * ```
1317
- * @category Creation
1318
- * @since v1.0.0
1319
- */
1320
- export function createRelationships(store: Store): Relationships;