@dolusoft/query-table 3.0.1 → 3.2.0

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.
package/dist/local.js ADDED
@@ -0,0 +1,88 @@
1
+ import { computed, toValue, watch } from "vue";
2
+ import { cloneQuery, sameRules, searchOf } from "@dolusoft/query-protocol";
3
+ import { applyQuery, slicePage } from "@dolusoft/query-protocol/local";
4
+ //#region src/local/use-local-query.ts
5
+ var isDevelopment = () => {
6
+ try {
7
+ return process.env.NODE_ENV !== "production";
8
+ } catch {
9
+ return true;
10
+ }
11
+ };
12
+ /** Evaluates locally without rerunning filtering, search or sorting for a page-only change. */
13
+ function useLocalQuery(options) {
14
+ const semantic = computed((previous) => {
15
+ const query = toValue(options.query);
16
+ const copy = cloneQuery({
17
+ page: 1,
18
+ pageSize: 1,
19
+ filters: query.filters,
20
+ sort: query.sort,
21
+ search: searchOf(query)
22
+ });
23
+ const next = {
24
+ filters: copy.filters,
25
+ sort: copy.sort,
26
+ search: searchOf(copy)
27
+ };
28
+ return previous && sameRules(previous.filters, next.filters) && previous.sort?.field === next.sort?.field && previous.sort?.direction === next.sort?.direction && previous.sort === null === (next.sort === null) && previous.search === next.search ? previous : next;
29
+ });
30
+ const evaluated = computed(() => applyQuery(toValue(options.allRows), {
31
+ page: 1,
32
+ pageSize: 1,
33
+ ...semantic.value
34
+ }, toValue(options.dataset), {
35
+ profile: options.profile,
36
+ paginate: false
37
+ }));
38
+ const result = computed(() => {
39
+ const query = toValue(options.query);
40
+ const paginate = toValue(options.paginate) ?? true;
41
+ const check = applyQuery([], query, toValue(options.dataset), {
42
+ profile: options.profile,
43
+ paginate
44
+ });
45
+ if (!check.ok) return check;
46
+ const all = evaluated.value;
47
+ if (!all.ok) return all;
48
+ if (!paginate) return {
49
+ ok: true,
50
+ rows: [...all.rows],
51
+ totalRows: all.totalRows
52
+ };
53
+ const page = slicePage(all.rows, query);
54
+ return page.ok ? {
55
+ ok: true,
56
+ rows: page.rows,
57
+ totalRows: all.totalRows
58
+ } : page;
59
+ });
60
+ const rows = computed(() => result.value.ok ? result.value.rows : []);
61
+ const totalRows = computed(() => result.value.ok ? result.value.totalRows : 0);
62
+ const error = computed(() => result.value.ok ? null : result.value.error);
63
+ let last = "";
64
+ watch(error, (value) => {
65
+ if (!value) {
66
+ last = "";
67
+ return;
68
+ }
69
+ const key = JSON.stringify([
70
+ value.code,
71
+ value.path,
72
+ value.field
73
+ ]);
74
+ if (isDevelopment() && key !== last) {
75
+ last = key;
76
+ console.error("[useLocalQuery]", value);
77
+ }
78
+ }, { immediate: true });
79
+ return {
80
+ rows,
81
+ totalRows,
82
+ error
83
+ };
84
+ }
85
+ //#endregion
86
+ export { useLocalQuery };
87
+
88
+ //# sourceMappingURL=local.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local.js","names":[],"sources":["../src/local/use-local-query.ts"],"sourcesContent":["import {\n cloneQuery,\n sameRules,\n searchOf,\n type FilterRule,\n type Query,\n type SortState\n} from '@dolusoft/query-protocol'\nimport {\n applyQuery,\n slicePage,\n type Dataset,\n type LocalQueryError,\n type LocalQueryResult,\n type SemanticsProfile\n} from '@dolusoft/query-protocol/local'\nimport {\n computed,\n toValue,\n watch,\n type ComputedRef,\n type MaybeRefOrGetter\n} from 'vue'\n\n// Replaced by the consumer's bundler, just as in Vue's esm-bundler build.\ndeclare const process: { env: { NODE_ENV?: string } }\n\nexport interface UseLocalQueryOptions<T> {\n /** Every row of the source context. In-place changes in a deeply reactive array are seen; replace a shallowRef's array to trigger evaluation. */\n allRows: MaybeRefOrGetter<readonly T[]>\n /** The consumer's query, read without writing or correcting it. */\n query: MaybeRefOrGetter<Query>\n /** Replace together with allRows when the schema changes. */\n dataset: MaybeRefOrGetter<Dataset<T>>\n profile: SemanticsProfile\n /** false returns every matching row for print or export. Defaults to true. */\n paginate?: MaybeRefOrGetter<boolean>\n}\n\nexport interface LocalQuery<T> {\n /** The supplied row objects; empty while error is set. */\n rows: ComputedRef<T[]>\n /** The matching count; zero while error is set. */\n totalRows: ComputedRef<number>\n /** Render this separately from an empty result. */\n error: ComputedRef<LocalQueryError | null>\n}\n\n// A try/catch preserves constant replacement in production browser builds.\n// A typeof guard would stay true when the browser has no process global.\nconst isDevelopment = (): boolean => {\n try {\n return process.env.NODE_ENV !== 'production'\n } catch {\n return true\n }\n}\n\ninterface Semantic {\n filters: FilterRule[]\n sort: SortState | null\n search: string\n}\n\n/** Evaluates locally without rerunning filtering, search or sorting for a page-only change. */\nexport function useLocalQuery<T>(\n options: UseLocalQueryOptions<T>\n): LocalQuery<T> {\n const semantic = computed<Semantic>(previous => {\n const query = toValue(options.query)\n // Include nested extra rule properties: sameRules compares them by value.\n const copy = cloneQuery({\n page: 1,\n pageSize: 1,\n filters: query.filters,\n sort: query.sort,\n search: searchOf(query)\n })\n const next = {\n filters: copy.filters,\n sort: copy.sort,\n search: searchOf(copy)\n }\n return previous &&\n sameRules(previous.filters, next.filters) &&\n previous.sort?.field === next.sort?.field &&\n previous.sort?.direction === next.sort?.direction &&\n (previous.sort === null) === (next.sort === null) &&\n previous.search === next.search\n ? previous\n : next\n })\n const evaluated = computed(() =>\n applyQuery(\n toValue(options.allRows),\n { page: 1, pageSize: 1, ...semantic.value },\n toValue(options.dataset),\n { profile: options.profile, paginate: false }\n )\n )\n const result = computed<LocalQueryResult<T>>(() => {\n const query = toValue(options.query)\n const paginate = toValue(options.paginate) ?? true\n const check = applyQuery([], query, toValue(options.dataset), {\n profile: options.profile,\n paginate\n })\n if (!check.ok) {\n return check\n }\n const all = evaluated.value\n if (!all.ok) {\n return all\n }\n if (!paginate) {\n return { ok: true, rows: [...all.rows], totalRows: all.totalRows }\n }\n // The precheck already validated paging.\n const page = slicePage(all.rows, query)\n return page.ok\n ? { ok: true, rows: page.rows, totalRows: all.totalRows }\n : page\n })\n const rows = computed(() => (result.value.ok ? result.value.rows : []))\n const totalRows = computed(() =>\n result.value.ok ? result.value.totalRows : 0\n )\n const error = computed(() => (result.value.ok ? null : result.value.error))\n let last = ''\n watch(\n error,\n value => {\n if (!value) {\n last = ''\n return\n }\n const key = JSON.stringify([value.code, value.path, value.field])\n if (isDevelopment() && key !== last) {\n last = key\n console.error('[useLocalQuery]', value)\n }\n },\n { immediate: true }\n )\n return { rows, totalRows, error }\n}\n"],"mappings":";;;;AAkDA,IAAM,sBAA+B;CACnC,IAAI;EACF,OAAA,QAAA,IAAA,aAAgC;CAClC,QAAQ;EACN,OAAO;CACT;AACF;;AASA,SAAgB,cACd,SACe;CACf,MAAM,WAAW,UAAmB,aAAY;EAC9C,MAAM,QAAQ,QAAQ,QAAQ,KAAK;EAEnC,MAAM,OAAO,WAAW;GACtB,MAAM;GACN,UAAU;GACV,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,QAAQ,SAAS,KAAK;EACxB,CAAC;EACD,MAAM,OAAO;GACX,SAAS,KAAK;GACd,MAAM,KAAK;GACX,QAAQ,SAAS,IAAI;EACvB;EACA,OAAO,YACL,UAAU,SAAS,SAAS,KAAK,OAAO,KACxC,SAAS,MAAM,UAAU,KAAK,MAAM,SACpC,SAAS,MAAM,cAAc,KAAK,MAAM,aACvC,SAAS,SAAS,UAAW,KAAK,SAAS,SAC5C,SAAS,WAAW,KAAK,SACvB,WACA;CACN,CAAC;CACD,MAAM,YAAY,eAChB,WACE,QAAQ,QAAQ,OAAO,GACvB;EAAE,MAAM;EAAG,UAAU;EAAG,GAAG,SAAS;CAAM,GAC1C,QAAQ,QAAQ,OAAO,GACvB;EAAE,SAAS,QAAQ;EAAS,UAAU;CAAM,CAC9C,CACF;CACA,MAAM,SAAS,eAAoC;EACjD,MAAM,QAAQ,QAAQ,QAAQ,KAAK;EACnC,MAAM,WAAW,QAAQ,QAAQ,QAAQ,KAAK;EAC9C,MAAM,QAAQ,WAAW,CAAC,GAAG,OAAO,QAAQ,QAAQ,OAAO,GAAG;GAC5D,SAAS,QAAQ;GACjB;EACF,CAAC;EACD,IAAI,CAAC,MAAM,IACT,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