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