@aglyn/shared-util-tools 1.0.0-beta.230 → 1.0.0-beta.232

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.
@@ -0,0 +1,203 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import { type ListFilterField, type ListFilterRequest } from './list-filter';
18
+ /**
19
+ * How the written search fields were normalized, so a typed value becomes the
20
+ * key the query can match: the lower-cased key (`equals`, `startsWith`), the
21
+ * one word-prefix token (`contains`, search), and the reversed key
22
+ * (`endsWith`). `maxPrefix` is the longest token a writer stores.
23
+ */
24
+ export interface ListQueryNormalizers {
25
+ key(value: string): string;
26
+ token(value: string): string;
27
+ reversed(value: string): string;
28
+ maxPrefix: number;
29
+ }
30
+ /** A predicate on the query, SDK-free. `path` `__name__` is the document id. */
31
+ export type ListQueryOp = '==' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'array-contains' | 'array-contains-any';
32
+ export type ListQueryValue = string | number | boolean | null | Date | readonly (string | number)[];
33
+ export interface ListQueryFilter {
34
+ path: string;
35
+ op: ListQueryOp;
36
+ value: ListQueryValue;
37
+ }
38
+ export interface ListQuerySort {
39
+ path: string;
40
+ direction: 'asc' | 'desc';
41
+ /** The grid column this order sorts, when the list lets the grid sort by it. */
42
+ column?: string;
43
+ /**
44
+ * Served ONLY while nothing narrows the list past its base scope — no
45
+ * served clause, no search word (AGL-3680).
46
+ *
47
+ * A header sort on every column would otherwise cost one composite per
48
+ * (equality field × order), a matrix the 1,000-composite cap cannot hold.
49
+ * An `alone` order is paired only with the base predicates
50
+ * (`listQueryIndexes`), so on a top-level or subcollection list it costs
51
+ * NO composite, and on a base-scoped collection-group list one per
52
+ * direction. Asked while a filter or the search is on, the plan falls
53
+ * back to the list's default header order and says so in `notices`.
54
+ *
55
+ * Leave it off the default order and on at most one other high-value
56
+ * order a list wants to hold under every filter.
57
+ */
58
+ alone?: boolean;
59
+ /** How the order reads in a notice — "Created", "Plan". Defaults to the column. */
60
+ label?: string;
61
+ }
62
+ /** The document id, as a path. */
63
+ export declare const LIST_QUERY_ID_PATH = "__name__";
64
+ /** Firestore's cap on disjunctions — `in` values times `array-contains-any` values. */
65
+ export declare const LIST_QUERY_DISJUNCTIONS = 30;
66
+ export interface ListQueryDeclaration {
67
+ /** Every field the Filters panel may name. */
68
+ fields: readonly ListFilterField[];
69
+ /**
70
+ * The orders the list offers, its default FIRST. The query takes no other:
71
+ * each is a composite the index file carries for every filterable field.
72
+ */
73
+ sorts: readonly ListQuerySort[];
74
+ /**
75
+ * The quick search: the token array every write stamps (see
76
+ * `nameSearchTokens`), queried with `array-contains` on one typed word.
77
+ * Absent, the list offers no search box.
78
+ */
79
+ search?: {
80
+ tokensPath: string;
81
+ /**
82
+ * Under a scope clause the search cannot be a SECOND array clause, so a
83
+ * scoped list writes `<scopeToken><join><prefix>` tokens and the query
84
+ * asks `array-contains-any` over the scope's tokens joined to the word —
85
+ * one array clause that answers both.
86
+ *
87
+ * ⛔ Rules that grant a list from `visibleTo.hasAny(...)` DENY this
88
+ * query, because it no longer constrains `visibleTo`. Declare it only for
89
+ * readers whose rule needs no `visibleTo` term, or for an Admin-SDK read;
90
+ * see `SCOPED_SEARCH_JOIN` in `@aglyn/aglyn/app-utils/name-search`.
91
+ */
92
+ scoped?: {
93
+ tokensPath: string;
94
+ join: string;
95
+ };
96
+ };
97
+ /**
98
+ * Keys a presence-map field (`keysOf`) is filtered by, for the index
99
+ * enumeration: `sources.form`, `sources.order`, …
100
+ */
101
+ keys?: Readonly<Record<string, readonly string[]>>;
102
+ }
103
+ /** What the list asks this time. */
104
+ export interface ListQueryRequest {
105
+ clauses: readonly ListFilterRequest[];
106
+ /** The quick search's words. */
107
+ search?: readonly string[];
108
+ /** The order asked for; a sort the declaration does not offer is ignored. */
109
+ sort?: ListQuerySort | null;
110
+ /**
111
+ * Predicates the list always applies — its scope — which count against
112
+ * the same limits. An `array-contains-any` scope is what a scoped search
113
+ * folds into (`declaration.search.scoped`).
114
+ */
115
+ base?: readonly ListQueryFilter[];
116
+ }
117
+ export interface ListQueryRefusal {
118
+ /** The clause refused, or `search`. */
119
+ clause: ListFilterRequest | 'search';
120
+ reason: string;
121
+ }
122
+ export interface ListQueryPlan {
123
+ /** Every predicate, base included, in the order the SDK should add them. */
124
+ filters: ListQueryFilter[];
125
+ /** The one order: the range field's when there is one, else the sort. */
126
+ orderBy: ListQuerySort;
127
+ /** The clauses on the query. */
128
+ served: ListFilterRequest[];
129
+ /** The search word on the query, or null. */
130
+ searched: string | null;
131
+ /** What was asked and could not be put on the query, with why. */
132
+ refused: ListQueryRefusal[];
133
+ /** Said to the reader about what WAS served — e.g. one search word of two. */
134
+ notices: string[];
135
+ /**
136
+ * The order asked for that the plan did not serve, and why: `alone` — it
137
+ * is served only with no filter or search on; `range` — a range filter
138
+ * leads the order with its own field. Absent when the asked order is served.
139
+ */
140
+ sortFallback?: {
141
+ asked: ListQuerySort;
142
+ reason: 'alone' | 'range';
143
+ } | null;
144
+ }
145
+ /**
146
+ * Every clause and the search, as ONE query — or as much of it as one query
147
+ * can hold, with the rest refused by name.
148
+ *
149
+ * Order of claim: the base (the list's scope) first, then the search, then
150
+ * the clauses in the order they were added. A clause that would be a second
151
+ * array clause, a second inequality field or push the disjunctions past
152
+ * thirty is refused, and the list shows the refusal beside its chip; the
153
+ * clauses before it stand. The search is claimed before the clauses because
154
+ * it is the thing a reader typed last and looks at.
155
+ */
156
+ export declare function planListQuery(declaration: ListQueryDeclaration, request: ListQueryRequest, names: ListQueryNormalizers): ListQueryPlan;
157
+ /**
158
+ * The order an `alone` sort falls back to: the first header order that is
159
+ * not itself `alone`, else the list's default. A fallback that could itself
160
+ * fall back would leave the list in no order at all.
161
+ */
162
+ export declare function defaultHeaderSort(declaration: ListQueryDeclaration): ListQuerySort;
163
+ /** A composite the plan's shapes need, in the index file's own terms. */
164
+ export interface ListQueryIndex {
165
+ fields: Array<{
166
+ fieldPath: string;
167
+ order?: 'ASCENDING' | 'DESCENDING';
168
+ arrayConfig?: 'CONTAINS';
169
+ }>;
170
+ }
171
+ /**
172
+ * Every composite a declaration's queries can need, under index merging:
173
+ * one `(predicate field, order)` index per equality or array field and per
174
+ * order the list can take — its sorts, and the order each range imposes.
175
+ *
176
+ * `base` names the scope predicates the list always applies (their paths
177
+ * and whether they are array clauses), which need their own index per order
178
+ * like any other equality. Pinned by each list's spec against
179
+ * `cloud/firebase-firestore.indexes.json`.
180
+ */
181
+ export declare function listQueryIndexes(declaration: ListQueryDeclaration, base?: readonly {
182
+ path: string;
183
+ array?: boolean;
184
+ }[]): ListQueryIndex[];
185
+ /** An index entry as `cloud/firebase-firestore.indexes.json` stores it. */
186
+ interface IndexFileEntry {
187
+ collectionGroup: string;
188
+ queryScope: string;
189
+ fields: Array<{
190
+ fieldPath: string;
191
+ order?: string;
192
+ arrayConfig?: string;
193
+ }>;
194
+ }
195
+ /**
196
+ * The composites a declaration needs that an index file does not hold — what
197
+ * a list's spec asserts is empty, so a declared filter cannot ship without
198
+ * the index that serves it.
199
+ */
200
+ export declare function missingListQueryIndexes(file: {
201
+ indexes: readonly IndexFileEntry[];
202
+ }, collectionGroup: string, needed: readonly ListQueryIndex[], queryScope?: 'COLLECTION' | 'COLLECTION_GROUP'): ListQueryIndex[];
203
+ export {};