tinybase 9.3.0 → 9.4.0-beta.1

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 (37) hide show
  1. package/@types/queries/index.d.ts +141 -7
  2. package/@types/queries/with-schemas/index.d.ts +153 -7
  3. package/@types/ui-react-dom/index.d.ts +10 -10
  4. package/@types/ui-react-dom/with-schemas/index.d.ts +10 -10
  5. package/@types/ui-react-dom-charts/index.d.ts +7 -7
  6. package/@types/ui-react-dom-charts/with-schemas/index.d.ts +7 -7
  7. package/@types/ui-react-inspector/index.d.ts +1 -1
  8. package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
  9. package/@types/ui-solid-dom/index.d.ts +10 -10
  10. package/@types/ui-solid-dom/with-schemas/index.d.ts +10 -10
  11. package/@types/ui-solid-inspector/index.d.ts +1 -1
  12. package/@types/ui-solid-inspector/with-schemas/index.d.ts +1 -1
  13. package/@types/ui-svelte-dom/index.d.ts +10 -10
  14. package/@types/ui-svelte-dom/with-schemas/index.d.ts +10 -10
  15. package/@types/ui-svelte-inspector/index.d.ts +1 -1
  16. package/@types/ui-svelte-inspector/with-schemas/index.d.ts +1 -1
  17. package/index.js +200 -24
  18. package/min/index.js +1 -1
  19. package/min/index.js.gz +0 -0
  20. package/min/omni/index.js +1 -1
  21. package/min/omni/index.js.gz +0 -0
  22. package/min/omni/with-schemas/index.js +1 -1
  23. package/min/omni/with-schemas/index.js.gz +0 -0
  24. package/min/queries/index.js +1 -1
  25. package/min/queries/index.js.gz +0 -0
  26. package/min/queries/with-schemas/index.js +1 -1
  27. package/min/queries/with-schemas/index.js.gz +0 -0
  28. package/min/with-schemas/index.js +1 -1
  29. package/min/with-schemas/index.js.gz +0 -0
  30. package/omni/index.js +200 -24
  31. package/omni/with-schemas/index.js +200 -24
  32. package/package.json +1 -1
  33. package/queries/index.js +205 -24
  34. package/queries/with-schemas/index.js +205 -24
  35. package/readme.md +1 -1
  36. package/releases.md +1 -1
  37. package/with-schemas/index.js +200 -24
@@ -4476,6 +4476,8 @@ const createQueries = getCreateFunction((store) => {
4476
4476
  };
4477
4477
  const preStoreListenerIds = mapNew();
4478
4478
  const sourceStoreListenerIds = mapNew();
4479
+ const selectAllListenerIds = mapNew();
4480
+ const referencedQueryIds = mapNew();
4479
4481
  const {
4480
4482
  _: [, addListener, callListeners],
4481
4483
  delListener: delListenerImpl,
@@ -4549,11 +4551,42 @@ const createQueries = getCreateFunction((store) => {
4549
4551
  resetStoreListeners(mapGet(sourceStoreListenerIds, queryId));
4550
4552
  mapSet(sourceStoreListenerIds, queryId);
4551
4553
  };
4552
- const isResultStoreReferenced = (resultStore2) =>
4554
+ const syncSelectAllListeners = (queryId, nextSources = mapNew()) => {
4555
+ const listenerIds = mapEnsure(selectAllListenerIds, queryId, mapNew);
4556
+ mapForEach(listenerIds, (sourceStore, listenerIdsByTableId) => {
4557
+ mapForEach(listenerIdsByTableId, (sourceTableId, listenerId) =>
4558
+ collHas(mapGet(nextSources, sourceStore), sourceTableId)
4559
+ ? 0
4560
+ : (sourceStore.delListener(listenerId),
4561
+ mapSet(listenerIdsByTableId, sourceTableId)),
4562
+ );
4563
+ if (collIsEmpty(listenerIdsByTableId)) {
4564
+ mapSet(listenerIds, sourceStore);
4565
+ }
4566
+ });
4567
+ mapForEach(nextSources, (sourceStore, tableIds) => {
4568
+ const listenerIdsByTableId = mapEnsure(listenerIds, sourceStore, mapNew);
4569
+ collForEach(tableIds, (sourceTableId) =>
4570
+ mapEnsure(listenerIdsByTableId, sourceTableId, () =>
4571
+ sourceStore.addTableCellIdsListener(sourceTableId, () =>
4572
+ setQueryDefinitionImpl(queryId),
4573
+ ),
4574
+ ),
4575
+ );
4576
+ });
4577
+ if (collIsEmpty(listenerIds)) {
4578
+ mapSet(selectAllListenerIds, queryId);
4579
+ }
4580
+ };
4581
+ const isResultStoreReferenced = (queryId, resultStore2) =>
4553
4582
  !(
4554
4583
  collEvery(routedResultListeners, ([, storeListenerIds]) =>
4555
4584
  collEvery(storeListenerIds, ([store2]) => store2 !== resultStore2),
4556
4585
  ) &&
4586
+ collEvery(
4587
+ referencedQueryIds,
4588
+ (queryIds) => !collHas(queryIds, queryId),
4589
+ ) &&
4557
4590
  arrayEvery(
4558
4591
  [preStoreListenerIds, sourceStoreListenerIds],
4559
4592
  (storeListenerIds) =>
@@ -4568,7 +4601,7 @@ const createQueries = getCreateFunction((store) => {
4568
4601
  hasQuery(queryId) ? 0 : mapSet(preStores, queryId),
4569
4602
  );
4570
4603
  mapForEach(resultStores, (queryId, resultStore2) =>
4571
- hasQuery(queryId) || isResultStoreReferenced(resultStore2)
4604
+ hasQuery(queryId) || isResultStoreReferenced(queryId, resultStore2)
4572
4605
  ? 0
4573
4606
  : mapSet(resultStores, queryId),
4574
4607
  );
@@ -4650,7 +4683,13 @@ const createQueries = getCreateFunction((store) => {
4650
4683
  const rootStore = asQuery ? getResultStore(tableId) : store;
4651
4684
  const resultStore2 = getResultStore(queryId);
4652
4685
  const paramValues = definition?.[3] ?? getParamValues(queryId);
4653
- const selectEntries = [];
4686
+ const nextReferencedQueryIds = stagedDefinition[4];
4687
+ if (asQuery) {
4688
+ setAdd(nextReferencedQueryIds, tableId);
4689
+ }
4690
+ const selectionEntries = [];
4691
+ let hasSelectAll = false;
4692
+ let selectCount = 0;
4654
4693
  const joinEntries = [
4655
4694
  [void 0, [tableId, void 0, void 0, [], mapNew(), rootStore]],
4656
4695
  ];
@@ -4662,21 +4701,35 @@ const createQueries = getCreateFunction((store) => {
4662
4701
  const joinedTableId = isTrue(arg1) ? arg2 : arg1;
4663
4702
  const joinedCellId = isTrue(arg1) ? arg3 : arg2;
4664
4703
  const selectEntry = isFunction(arg1)
4665
- ? [size(selectEntries) + EMPTY_STRING, arg1]
4704
+ ? [0, selectCount + EMPTY_STRING, arg1]
4666
4705
  : isUndefined(joinedCellId)
4667
- ? [arg1, (getTableCell) => getTableCell(arg1)]
4706
+ ? [0, arg1, (getTableCell) => getTableCell(arg1)]
4668
4707
  : [
4708
+ 0,
4669
4709
  joinedCellId,
4670
4710
  (getTableCell) =>
4671
4711
  isTrue(arg1)
4672
4712
  ? getTableCell(true, joinedTableId, joinedCellId)
4673
4713
  : getTableCell(joinedTableId, joinedCellId),
4674
4714
  ];
4675
- arrayPush(selectEntries, selectEntry);
4715
+ selectCount++;
4716
+ arrayPush(selectionEntries, selectEntry);
4676
4717
  return {
4677
- as: (selectedCellId) => (selectEntry[0] = selectedCellId),
4718
+ as: (selectedCellId) => (selectEntry[1] = selectedCellId),
4678
4719
  };
4679
4720
  };
4721
+ const selectAll = (arg1, arg2, arg3) => {
4722
+ hasSelectAll = true;
4723
+ const joinedTableId = isTrue(arg1) ? arg2 : arg1;
4724
+ const prefixOrMapper = isTrue(arg1) ? arg3 : arg2;
4725
+ arrayPush(selectionEntries, [
4726
+ 1,
4727
+ joinedTableId,
4728
+ isFunction(prefixOrMapper)
4729
+ ? prefixOrMapper
4730
+ : (cellId) => (prefixOrMapper ?? EMPTY_STRING) + cellId,
4731
+ ]);
4732
+ };
4680
4733
  const join = (arg1, arg2, arg3, arg4) => {
4681
4734
  const joinedTableId = isTrue(arg1) ? arg2 : arg1;
4682
4735
  const [fromJoinAlias, onArg] = isTrue(arg1)
@@ -4686,6 +4739,13 @@ const createQueries = getCreateFunction((store) => {
4686
4739
  : isUndefined(arg3) || isFunction(arg2)
4687
4740
  ? [void 0, arg2]
4688
4741
  : [arg2, arg3];
4742
+ const sourceIsQuery = isTrue(arg1);
4743
+ const sourceStore = sourceIsQuery
4744
+ ? getResultStore(joinedTableId)
4745
+ : store;
4746
+ if (sourceIsQuery) {
4747
+ setAdd(nextReferencedQueryIds, joinedTableId);
4748
+ }
4689
4749
  const joinEntry = [
4690
4750
  joinedTableId,
4691
4751
  [
@@ -4694,7 +4754,7 @@ const createQueries = getCreateFunction((store) => {
4694
4754
  isFunction(onArg) ? onArg : (getCell) => getCell(onArg),
4695
4755
  [],
4696
4756
  mapNew(),
4697
- isTrue(arg1) ? getResultStore(joinedTableId) : store,
4757
+ sourceStore,
4698
4758
  ],
4699
4759
  ];
4700
4760
  arrayPush(joinEntries, joinEntry);
@@ -4740,10 +4800,9 @@ const createQueries = getCreateFunction((store) => {
4740
4800
  : (getSelectedOrGroupedCell) =>
4741
4801
  getSelectedOrGroupedCell(arg1) === arg2,
4742
4802
  );
4743
- build({select, join, where, group, having, param});
4803
+ build({select, selectAll, join, where, group, having, param});
4744
4804
  resultStore2.delTable(queryId);
4745
- const selects = mapNew(selectEntries);
4746
- if (collIsEmpty(selects)) {
4805
+ if (isEmpty(selectionEntries)) {
4747
4806
  commit?.();
4748
4807
  return queries;
4749
4808
  }
@@ -4755,6 +4814,55 @@ const createQueries = getCreateFunction((store) => {
4755
4814
  );
4756
4815
  const groups = mapNew(groupEntries);
4757
4816
  const hasGroupsOrHavings = !collIsEmpty(groups) || !isEmpty(havings);
4817
+ const selectEntries = [];
4818
+ const selectAllSources = stagedDefinition[3];
4819
+ const getSelectAllJoin = (joinedTableId) =>
4820
+ mapGet(
4821
+ joins,
4822
+ isUndefined(joinedTableId) || joinedTableId === tableId
4823
+ ? void 0
4824
+ : joinedTableId,
4825
+ );
4826
+ arrayForEach(
4827
+ selectionEntries,
4828
+ ([selectAll2, selectedOrJoinedTableId, clauseOrMapper]) => {
4829
+ if (selectAll2) {
4830
+ if (hasGroupsOrHavings) {
4831
+ ifNotUndefined(
4832
+ getSelectAllJoin(selectedOrJoinedTableId),
4833
+ ([realTableId, , , , , sourceStore]) => {
4834
+ setAdd(
4835
+ mapEnsure(selectAllSources, sourceStore, setNew),
4836
+ realTableId,
4837
+ );
4838
+ arrayForEach(
4839
+ arraySort(sourceStore.getTableCellIds(realTableId)),
4840
+ (cellId) =>
4841
+ arrayPush(selectEntries, [
4842
+ clauseOrMapper(cellId),
4843
+ (getTableCell) =>
4844
+ isUndefined(selectedOrJoinedTableId) ||
4845
+ selectedOrJoinedTableId === tableId
4846
+ ? getTableCell(cellId)
4847
+ : getTableCell(selectedOrJoinedTableId, cellId),
4848
+ ]),
4849
+ );
4850
+ },
4851
+ );
4852
+ }
4853
+ } else {
4854
+ arrayPush(selectEntries, [
4855
+ selectedOrJoinedTableId,
4856
+ clauseOrMapper,
4857
+ ]);
4858
+ }
4859
+ },
4860
+ );
4861
+ const selects = mapNew(selectEntries);
4862
+ if (hasGroupsOrHavings && collIsEmpty(selects)) {
4863
+ commit?.();
4864
+ return queries;
4865
+ }
4758
4866
  const selectJoinWhereStore = hasGroupsOrHavings
4759
4867
  ? (stagedDefinition[2] = createStore())
4760
4868
  : resultStore2;
@@ -4933,18 +5041,78 @@ const createQueries = getCreateFunction((store) => {
4933
5041
  joinedCellId,
4934
5042
  );
4935
5043
  };
4936
- selectJoinWhereStore.transaction(() =>
4937
- arrayEvery(wheres, (where2) => where2(getJoinCell))
4938
- ? mapForEach(selects, (asCellId, tableCellGetter) =>
4939
- selectJoinWhereStore._[5](
4940
- queryId,
4941
- rootRowId,
4942
- asCellId,
4943
- tableCellGetter(getJoinCell, rootRowId),
4944
- ),
4945
- )
4946
- : selectJoinWhereStore.delRow(queryId, rootRowId),
4947
- );
5044
+ selectJoinWhereStore.transaction(() => {
5045
+ if (!arrayEvery(wheres, (where2) => where2(getJoinCell))) {
5046
+ selectJoinWhereStore.delRow(queryId, rootRowId);
5047
+ } else if (hasGroupsOrHavings) {
5048
+ mapForEach(selects, (asCellId, tableCellGetter) =>
5049
+ selectJoinWhereStore._[5](
5050
+ queryId,
5051
+ rootRowId,
5052
+ asCellId,
5053
+ tableCellGetter(getJoinCell, rootRowId),
5054
+ ),
5055
+ );
5056
+ } else if (hasSelectAll) {
5057
+ const resultRow = objNew();
5058
+ arrayForEach(
5059
+ selectionEntries,
5060
+ ([selectAll2, selectedOrJoinedTableId, clauseOrMapper]) => {
5061
+ if (selectAll2) {
5062
+ ifNotUndefined(
5063
+ getSelectAllJoin(selectedOrJoinedTableId),
5064
+ ([realTableId, , , , remoteIdPairs, sourceStore]) => {
5065
+ const sourceRowId =
5066
+ isUndefined(selectedOrJoinedTableId) ||
5067
+ selectedOrJoinedTableId === tableId
5068
+ ? rootRowId
5069
+ : mapGet(remoteIdPairs, rootRowId)?.[0];
5070
+ ifNotUndefined(sourceRowId, (sourceRowId2) =>
5071
+ arrayForEach(
5072
+ arraySort(
5073
+ sourceStore.getCellIds(realTableId, sourceRowId2),
5074
+ ),
5075
+ (cellId) =>
5076
+ objSet(
5077
+ resultRow,
5078
+ clauseOrMapper(cellId),
5079
+ sourceStore.getCell(
5080
+ realTableId,
5081
+ sourceRowId2,
5082
+ cellId,
5083
+ ),
5084
+ ),
5085
+ ),
5086
+ );
5087
+ },
5088
+ );
5089
+ } else {
5090
+ const selectedCell = clauseOrMapper(getJoinCell, rootRowId);
5091
+ const selectedCellId = selectedOrJoinedTableId;
5092
+ if (isUndefined(selectedCell)) {
5093
+ objDel(resultRow, selectedCellId);
5094
+ } else {
5095
+ objSet(resultRow, selectedCellId, selectedCell);
5096
+ }
5097
+ }
5098
+ },
5099
+ );
5100
+ if (objIsEmpty(resultRow)) {
5101
+ selectJoinWhereStore.delRow(queryId, rootRowId);
5102
+ } else {
5103
+ selectJoinWhereStore.setRow(queryId, rootRowId, resultRow);
5104
+ }
5105
+ } else {
5106
+ mapForEach(selects, (asCellId, tableCellGetter) =>
5107
+ selectJoinWhereStore._[5](
5108
+ queryId,
5109
+ rootRowId,
5110
+ asCellId,
5111
+ tableCellGetter(getJoinCell, rootRowId),
5112
+ ),
5113
+ );
5114
+ }
5115
+ });
4948
5116
  };
4949
5117
  const listenToTable = (
4950
5118
  rootRowId,
@@ -5063,7 +5231,7 @@ const createQueries = getCreateFunction((store) => {
5063
5231
  }),
5064
5232
  );
5065
5233
  const setQueryDefinitionImpl = (queryId, definition) => {
5066
- const stagedDefinition = [mapNew(), mapNew()];
5234
+ const stagedDefinition = [mapNew(), mapNew(), void 0, mapNew(), setNew()];
5067
5235
  let committed = false;
5068
5236
  tryCatchSync(
5069
5237
  () =>
@@ -5084,6 +5252,12 @@ const createQueries = getCreateFunction((store) => {
5084
5252
  collIsEmpty(stagedDefinition[1]) ? void 0 : stagedDefinition[1],
5085
5253
  );
5086
5254
  mapSet(preStores, queryId, stagedDefinition[2]);
5255
+ syncSelectAllListeners(queryId, stagedDefinition[3]);
5256
+ mapSet(
5257
+ referencedQueryIds,
5258
+ queryId,
5259
+ collIsEmpty(stagedDefinition[4]) ? void 0 : stagedDefinition[4],
5260
+ );
5087
5261
  committed = true;
5088
5262
  }),
5089
5263
  (error) => {
@@ -5133,6 +5307,8 @@ const createQueries = getCreateFunction((store) => {
5133
5307
  paramStore.delRow(PARAMS_TABLE, queryId);
5134
5308
  resetPreStores(queryId);
5135
5309
  resetSourceStores(queryId);
5310
+ syncSelectAllListeners(queryId);
5311
+ mapSet(referencedQueryIds, queryId);
5136
5312
  delDefinition(queryId);
5137
5313
  cleanStores();
5138
5314
  return queries;