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