@arrai-innovations/reactive-helpers 13.1.0 → 14.0.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 (51) hide show
  1. package/README.md +11 -1
  2. package/config/listCrud.js +4 -1
  3. package/config/objectCrud.js +10 -4
  4. package/docs/README.md +1 -1
  5. package/docs/config/listCrud.md +20 -2
  6. package/docs/config/objectCrud.md +51 -15
  7. package/docs/use/list.md +6 -0
  8. package/docs/use/listCalculated.md +58 -39
  9. package/docs/use/listFilter.md +57 -39
  10. package/docs/use/listInstance.md +29 -11
  11. package/docs/use/listRelated.md +47 -28
  12. package/docs/use/listSearch.md +40 -28
  13. package/docs/use/listSort.md +58 -39
  14. package/docs/use/listSubscription.md +8 -2
  15. package/docs/use/object.md +28 -14
  16. package/docs/use/objectCalculated.md +26 -13
  17. package/docs/use/objectInstance.md +49 -24
  18. package/docs/use/objectRelated.md +27 -14
  19. package/docs/use/objectSubscription.md +18 -9
  20. package/docs/use/paginatedListInstance.md +8 -2
  21. package/docs/use/search.md +8 -2
  22. package/docs/utils/{getFakeId.md → getFakePk.md} +7 -7
  23. package/index.js +1 -1
  24. package/package.json +1 -1
  25. package/tests/unit/use/listCalculated.spec.js +4 -4
  26. package/tests/unit/use/listFilter.spec.js +10 -9
  27. package/tests/unit/use/listInstance.spec.js +147 -23
  28. package/tests/unit/use/listRelated.spec.js +5 -5
  29. package/tests/unit/use/listSearch.spec.js +14 -12
  30. package/tests/unit/use/listSort.spec.js +8 -1
  31. package/tests/unit/use/listSubscription.spec.js +119 -10
  32. package/tests/unit/use/objectInstance.spec.js +575 -46
  33. package/tests/unit/use/objectSubscription.spec.js +152 -23
  34. package/tests/unit/use/search.spec.js +11 -11
  35. package/use/list.js +1 -0
  36. package/use/listCalculated.js +3 -2
  37. package/use/listFilter.js +9 -9
  38. package/use/listInstance.js +39 -28
  39. package/use/listKeys.js +4 -3
  40. package/use/listRelated.js +23 -22
  41. package/use/listSearch.js +37 -33
  42. package/use/listSort.js +1 -0
  43. package/use/listSubscription.js +25 -15
  44. package/use/object.js +8 -13
  45. package/use/objectCalculated.js +2 -1
  46. package/use/objectInstance.js +29 -14
  47. package/use/objectRelated.js +3 -2
  48. package/use/objectSubscription.js +31 -11
  49. package/use/paginatedListInstance.js +1 -1
  50. package/use/search.js +13 -4
  51. package/utils/{getFakeId.js → getFakePk.js} +4 -4
@@ -97,7 +97,7 @@ Called to patch the current object on the server.
97
97
 
98
98
  > **retrieve**: `Function`
99
99
 
100
- Called to retrieve the current object by id from the server.
100
+ Called to retrieve the current object by pk from the server.
101
101
 
102
102
  ##### update
103
103
 
@@ -219,11 +219,17 @@ A function to be used instead of the default crud update function.
219
219
 
220
220
  [`ResponseData`](../config/objectCrud.md#responsedata)
221
221
 
222
- ###### id
222
+ ###### pk
223
223
 
224
- > **id**: `string`
224
+ > **pk**: `string`
225
225
 
226
- The id of the object.
226
+ The pk of the object, optional to support creating new objects.
227
+
228
+ ###### pkKey
229
+
230
+ > **pkKey**: `string`
231
+
232
+ The pk key of the object.
227
233
 
228
234
  ###### retrieveArgs
229
235
 
@@ -357,12 +363,6 @@ The error.
357
363
 
358
364
  Whether the object errored.
359
365
 
360
- ###### id
361
-
362
- > **id**: `string`
363
-
364
- The id of the object.
365
-
366
366
  ###### loading
367
367
 
368
368
  > **loading**: `boolean`
@@ -375,6 +375,18 @@ Whether the object is loading.
375
375
 
376
376
  The object.
377
377
 
378
+ ###### pk
379
+
380
+ > **pk**: `string`
381
+
382
+ The pk of the object.
383
+
384
+ ###### pkKey
385
+
386
+ > **pkKey**: `string`
387
+
388
+ The pk key of the object.
389
+
378
390
  ###### retrieveArgs
379
391
 
380
392
  > **retrieveArgs**: `any`
@@ -393,11 +405,17 @@ The arguments to be passed to the retrieve function.
393
405
 
394
406
  The arguments to be passed to the crud functions.
395
407
 
396
- ##### id
408
+ ##### pk
397
409
 
398
- > **id**: `string`
410
+ > **pk**: `string`
399
411
 
400
- The id of the object.
412
+ The pk of the object, optional to support creating new objects.
413
+
414
+ ##### pkKey
415
+
416
+ > **pkKey**: `string`
417
+
418
+ The pk key of the object.
401
419
 
402
420
  ##### retrieveArgs
403
421
 
@@ -435,12 +453,6 @@ The error.
435
453
 
436
454
  Whether the object errored.
437
455
 
438
- ##### id
439
-
440
- > **id**: `string`
441
-
442
- The id of the object.
443
-
444
456
  ##### loading
445
457
 
446
458
  > **loading**: `Readonly`\<`Ref`\<`boolean`\>\>
@@ -453,6 +465,18 @@ Whether the object is loading.
453
465
 
454
466
  The object.
455
467
 
468
+ ##### pk
469
+
470
+ > **pk**: `string`
471
+
472
+ The pk of the object.
473
+
474
+ ##### pkKey
475
+
476
+ > **pkKey**: `string`
477
+
478
+ The pk key of the object.
479
+
456
480
  ##### retrieveArgs
457
481
 
458
482
  > **retrieveArgs**: `any`
@@ -533,15 +557,16 @@ const props = defineProps({
533
557
  type: String,
534
558
  required: true,
535
559
  },
536
- id: {
560
+ pk: {
537
561
  type: String,
538
562
  default: '',
539
563
  },
540
564
  });
541
565
 
542
- const idRef = toRef(props, 'id');
566
+ const pkRef = toRef(props, 'pk');
543
567
  const objectInstanceProps = reactive({
544
- id: idRef,
568
+ pk: pkRef,
569
+ pkKey: 'id',
545
570
  crudArgs: {
546
571
  app: toRef(props, "app"),
547
572
  model: toRef(props, "model"),
@@ -549,14 +574,14 @@ const objectInstanceProps = reactive({
549
574
  retrieveArgs: {},
550
575
  });
551
576
  const objectInstance = useObjectInstance(objectInstanceProps);
552
- watch(idRef, (newValue, oldValue) => {
577
+ watch(pkRef, (newValue, oldValue) => {
553
578
  if (newValue !== oldValue && newValue) {
554
579
  objectInstance.retrieve();
555
580
  }
556
581
  });
557
582
  </script>
558
583
  <template>
559
- <!-- Display the retrieved object reactively, as a valid id is provided in props. -->
584
+ <!-- Display the retrieved object reactively, as a valid pk is provided in props. -->
560
585
  <div>{{ objectInstance.state.object }}</div>
561
586
  </template>
562
587
  ```
@@ -138,12 +138,6 @@ The error.
138
138
 
139
139
  Whether the object errored.
140
140
 
141
- ###### id
142
-
143
- > **id**: `string`
144
-
145
- The id of the object.
146
-
147
141
  ###### intendToRetrieve
148
142
 
149
143
  > **intendToRetrieve**: `boolean`
@@ -168,6 +162,18 @@ Whether the object is loading.
168
162
 
169
163
  The object.
170
164
 
165
+ ###### pk
166
+
167
+ > **pk**: `string`
168
+
169
+ The pk of the object.
170
+
171
+ ###### pkKey
172
+
173
+ > **pkKey**: `string`
174
+
175
+ The pk key of the object.
176
+
171
177
  ###### retrieveArgs
172
178
 
173
179
  > **retrieveArgs**: `any`
@@ -318,12 +324,6 @@ The error.
318
324
 
319
325
  Whether the object errored.
320
326
 
321
- ###### id
322
-
323
- > **id**: `string`
324
-
325
- The id of the object.
326
-
327
327
  ###### intendToRetrieve
328
328
 
329
329
  > **intendToRetrieve**: `boolean`
@@ -354,6 +354,18 @@ The object.
354
354
 
355
355
  Whether the parent state object watch is running.
356
356
 
357
+ ###### pk
358
+
359
+ > **pk**: `string`
360
+
361
+ The pk of the object.
362
+
363
+ ###### pkKey
364
+
365
+ > **pkKey**: `string`
366
+
367
+ The pk key of the object.
368
+
357
369
  ###### relatedObject
358
370
 
359
371
  > **relatedObject**: `object`
@@ -490,7 +502,7 @@ Whether the related objects are loading or the parent state is loading.
490
502
 
491
503
  ##### objects
492
504
 
493
- > **objects**: [`ObjectsById`](listInstance.md#objectsbyid)
505
+ > **objects**: [`ObjectsByPk`](listInstance.md#objectsbypk)
494
506
 
495
507
  The related objects, indexed by the key in the related object.
496
508
 
@@ -620,7 +632,8 @@ const someOtherObjectsSource = reactive({
620
632
  const objectSubscriptionProps = reactive({
621
633
  crudArgs: { app: 'foo', model: 'bar'},
622
634
  retrieveArgs: {},
623
- id: '99',
635
+ pk: '99',
636
+ pkKey: 'id',
624
637
  intendToSubscribe: true,
625
638
  intendToRetreive: true,
626
639
  });
@@ -269,12 +269,6 @@ The error.
269
269
 
270
270
  Whether the object errored.
271
271
 
272
- ###### id
273
-
274
- > **id**: `string`
275
-
276
- The id of the object.
277
-
278
272
  ###### intendToRetrieve
279
273
 
280
274
  > **intendToRetrieve**: `boolean`
@@ -299,6 +293,18 @@ Whether the object is loading.
299
293
 
300
294
  The object.
301
295
 
296
+ ###### pk
297
+
298
+ > **pk**: `string`
299
+
300
+ The pk of the object.
301
+
302
+ ###### pkKey
303
+
304
+ > **pkKey**: `string`
305
+
306
+ The pk key of the object.
307
+
302
308
  ###### retrieveArgs
303
309
 
304
310
  > **retrieveArgs**: `any`
@@ -458,10 +464,11 @@ Options for initializing the object subscription.
458
464
  import { useObjectSubscription } from "@arrai-innovations/reactive-helpers";
459
465
  import { reactive, ref, toRef } from "vue";
460
466
 
467
+ const pkKey = "id";
461
468
  const props = defineProps({
462
469
  app: { type: String, required: true },
463
470
  model: { type: String, required: true },
464
- id: { type: String, default: "" },
471
+ pk: { type: String, default: "" },
465
472
  });
466
473
 
467
474
  const objectSubscriptionProps = reactive({
@@ -469,19 +476,21 @@ const objectSubscriptionProps = reactive({
469
476
  app: toRef(props, "app"),
470
477
  model: toRef(props, "model"),
471
478
  },
479
+ pk: toRef(props, "pk"),
480
+ pkKey: pkKey,
472
481
  retrieveArgs: {
473
482
  fields: ['foo', 'bar'],
474
483
  },
475
484
  intendToRetrieve: false,
476
485
  intendToSubscribe: false,
477
486
  });
478
- objectSubscriptionProps.intendToRetrieve = objectSubscriptionProps.intendToSubscribe = computed(()=> !!props.id);
487
+ objectSubscriptionProps.intendToRetrieve = objectSubscriptionProps.intendToSubscribe = computed(()=> !!props.pk);
479
488
  const objectSubscription = useObjectSubscription(objectSubscriptionProps);
480
489
  </script>
481
490
  <template>
482
491
  <div v-if="objectSubscription.state.loading">Loading...</div>
483
492
  <div v-else-if="objectSubscription.state.errored">Error: {{ objectSubscription.state.error.message }}</div>
484
- <div v-else-if="objectSubscription.state.object.id">Foo: {{ objectSubscription.state.object.foo }}</div>
493
+ <div v-else-if="objectSubscription.state.object[pkKey]">Foo: {{ objectSubscription.state.object.foo }}</div>
485
494
  <div v-else>Object not found.</div>
486
495
  </template>
487
496
  ```
@@ -64,9 +64,9 @@ Indicates if the list is currently loading.
64
64
 
65
65
  ###### objects
66
66
 
67
- > **objects**: [`ObjectsById`](listInstance.md#objectsbyid)
67
+ > **objects**: [`ObjectsByPk`](listInstance.md#objectsbypk)
68
68
 
69
- The list objects stored by their IDs.
69
+ The list objects stored by their pks.
70
70
 
71
71
  ###### objectsInOrder
72
72
 
@@ -80,6 +80,12 @@ The objects in the order specified by the list.
80
80
 
81
81
  The order of objects in the list.
82
82
 
83
+ ###### pkKey
84
+
85
+ > **pkKey**: `string`
86
+
87
+ The primary key field for the list objects.
88
+
83
89
  ###### retrieveArgs
84
90
 
85
91
  > **retrieveArgs**: `any`
@@ -16,7 +16,7 @@
16
16
 
17
17
  > **id**: `string`
18
18
 
19
- The document field to use as an identifier. Default is "id".
19
+ The document field to use as an identifier. Populated from `pkKey`.
20
20
 
21
21
  ##### index
22
22
 
@@ -106,7 +106,7 @@ FlexSearch.Document options.
106
106
 
107
107
  > **id**: `string`
108
108
 
109
- The document field to use as an identifier. Default is "id".
109
+ The document field to use as an identifier. Populated from `pkKey`.
110
110
 
111
111
  ###### customDocumentOptions.index
112
112
 
@@ -222,6 +222,12 @@ FlexSearch.Document options.
222
222
 
223
223
  Search options.
224
224
 
225
+ ##### pkKey
226
+
227
+ > **pkKey**: `string`
228
+
229
+ The primary key field.
230
+
225
231
  ***
226
232
 
227
233
  ### SearchRawState
@@ -2,23 +2,23 @@
2
2
 
3
3
  ***
4
4
 
5
- [@arrai-innovations/reactive-helpers](../README.md) / utils/getFakeId
5
+ [@arrai-innovations/reactive-helpers](../README.md) / utils/getFakePk
6
6
 
7
- # utils/getFakeId
7
+ # utils/getFakePk
8
8
 
9
9
  ## Functions
10
10
 
11
- ### getFakeId()
11
+ ### getFakePk()
12
12
 
13
- > **getFakeId**(`arraySetMapOrObject`, `key`): `string`
13
+ > **getFakePk**(`arraySetMapOrObject`, `key`): `string`
14
14
 
15
- Get a fake id that is not in the array, set, map, or object.
15
+ Get a fake pk that is not in the array, set, map, or object.
16
16
 
17
17
  #### Parameters
18
18
 
19
19
  • **arraySetMapOrObject**: `any`
20
20
 
21
- The array, set, map, or object to check for the fake id.
21
+ The array, set, map, or object to check for the fake pk.
22
22
  An array is assumed to be an array of objects.
23
23
  A set is assumed to be a set of ids.
24
24
  A map or object is assumed to be an object with keys that are ids.
@@ -31,4 +31,4 @@ The key to check for in the array or object.
31
31
 
32
32
  `string`
33
33
 
34
- - The fake id.
34
+ - The fake pk.
package/index.js CHANGED
@@ -27,7 +27,7 @@ export * from "./utils/classes.js";
27
27
  export * from "./utils/compact.js";
28
28
  export * from "./utils/deleteKey.js";
29
29
  export * from "./utils/flattenPaths.js";
30
- export * from "./utils/getFakeId.js";
30
+ export * from "./utils/getFakePk.js";
31
31
  export * from "./utils/keyDiff.js";
32
32
  export * from "./utils/loadingCombine.js";
33
33
  export * from "./utils/relatedCalculatedHelpers.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "13.1.0",
3
+ "version": "14.0.2",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -15,8 +15,8 @@ describe("use/listCalculated", () => {
15
15
  AwaitNot = watchesModule.AwaitNot;
16
16
  });
17
17
  it("should return a list of calculated items", async () => {
18
- const mainListInstance = useListInstance({ props: {} });
19
- const calculatedListInstance = useListInstance({ props: {} });
18
+ const mainListInstance = useListInstance({ props: { pkKey: "id" } });
19
+ const calculatedListInstance = useListInstance({ props: { pkKey: "id" } });
20
20
  mainListInstance.addListObject({
21
21
  id: "1",
22
22
  name: "main",
@@ -77,8 +77,8 @@ describe("use/listCalculated", () => {
77
77
  });
78
78
  });
79
79
  it("should allow calculated objects to return results based on related objects", async () => {
80
- const mainListInstance = useListInstance({ props: {} });
81
- const relatedListInstance = useListInstance({ props: {} });
80
+ const mainListInstance = useListInstance({ props: { pkKey: "id" } });
81
+ const relatedListInstance = useListInstance({ props: { pkKey: "id" } });
82
82
  mainListInstance.addListObject({
83
83
  id: "1",
84
84
  name: "main",
@@ -19,7 +19,7 @@ describe("use/listFilter", () => {
19
19
  });
20
20
 
21
21
  it("should match an allowed filter function", async () => {
22
- const list = useListInstance({ props: {} });
22
+ const list = useListInstance({ props: { pkKey: "id" } });
23
23
  const filter = useListFilter({
24
24
  parentState: list.state,
25
25
  allowedFilter: (object) => object.id === 1 || object.id === 3,
@@ -65,7 +65,7 @@ describe("use/listFilter", () => {
65
65
  });
66
66
  });
67
67
  it("should match an excluded filter function", async () => {
68
- const list = useListInstance({ props: {} });
68
+ const list = useListInstance({ props: { pkKey: "id" } });
69
69
  const filter = useListFilter({
70
70
  parentState: list.state,
71
71
  excludedFilter: (object) => object.id == 2 || object.id == 4,
@@ -111,7 +111,7 @@ describe("use/listFilter", () => {
111
111
  });
112
112
  });
113
113
  it("no args: returns objects unfiltered", async () => {
114
- const listInstance = useListInstance({ props: {} });
114
+ const listInstance = useListInstance({ props: { pkKey: "id" } });
115
115
  const listItems = [
116
116
  { id: 4, name: "four", has_things: true },
117
117
  { id: 2, name: "two", has_things: true },
@@ -135,7 +135,7 @@ describe("use/listFilter", () => {
135
135
  vi.resetAllMocks();
136
136
  const orderByRules = [{ key: "name", desc: true, localeCompare: false }];
137
137
  const sortThrottleWait = 0;
138
- const listInstance = useListInstance({ props: {} });
138
+ const listInstance = useListInstance({ props: { pkKey: "id" } });
139
139
  const listItems = [
140
140
  { id: 4, name: "four", has_things: true },
141
141
  { id: 2, name: "two", has_things: true },
@@ -172,6 +172,7 @@ describe("use/listFilter", () => {
172
172
  props: {
173
173
  crudArgs: { stream: "test_streamA" },
174
174
  listArgs: { user: 1 },
175
+ pkKey: "id",
175
176
  retrieveArgs: {
176
177
  fields,
177
178
  },
@@ -222,7 +223,7 @@ describe("use/listFilter", () => {
222
223
  });
223
224
  describe("useListFilters updates index when", () => {
224
225
  it("parentInstance.objects is updated", async () => {
225
- const list = useListInstance({ props: {} });
226
+ const list = useListInstance({ props: { pkKey: "id" } });
226
227
  const filter = useListFilter({
227
228
  parentState: list.state,
228
229
  allowedFilter: (object) => !!object.allowed?.every((e) => e),
@@ -263,8 +264,8 @@ describe("use/listFilter", () => {
263
264
  });
264
265
  });
265
266
  it("parentInstance.relatedObjects is updated", async () => {
266
- const list = useListInstance({ props: {} });
267
- const relatedList = useListInstance({ props: {} });
267
+ const list = useListInstance({ props: { pkKey: "id" } });
268
+ const relatedList = useListInstance({ props: { pkKey: "id" } });
268
269
  const related = useListRelated({
269
270
  parentState: list.state,
270
271
  relatedObjectsRules: {
@@ -319,7 +320,7 @@ describe("use/listFilter", () => {
319
320
  });
320
321
  });
321
322
  it("parentInstance.calculatedObjects is updated", async () => {
322
- const list = useListInstance({ props: {} });
323
+ const list = useListInstance({ props: { pkKey: "id" } });
323
324
  const calculated = useListCalculated({
324
325
  parentState: list.state,
325
326
  calculatedObjectsRules: reactive({
@@ -363,7 +364,7 @@ describe("use/listFilter", () => {
363
364
  });
364
365
  });
365
366
  it("you can use nested useListFilters", async () => {
366
- const list = useListInstance({ props: {} });
367
+ const list = useListInstance({ props: { pkKey: "id" } });
367
368
 
368
369
  function filter1AllowedFilter(object) {
369
370
  return object.has_things && object.has_stuff;