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