@budibase/shared-core 2.9.8-alpha.0 → 2.9.8
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/package.json +3 -3
- package/src/filters.ts +64 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@budibase/shared-core",
|
|
3
|
-
"version": "2.9.8
|
|
3
|
+
"version": "2.9.8",
|
|
4
4
|
"description": "Shared data utils",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
"check:types": "tsc -p tsconfig.json --noEmit --paths null"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@budibase/types": "2.9.8
|
|
23
|
+
"@budibase/types": "2.9.8"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"concurrently": "^7.6.0",
|
|
27
27
|
"rimraf": "3.0.2",
|
|
28
28
|
"typescript": "4.7.3"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "51b7f5a8b02bd23aa5e2a4617e29448f234ed59e"
|
|
31
31
|
}
|
package/src/filters.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Datasource,
|
|
3
|
-
FieldType,
|
|
4
|
-
SearchFilter,
|
|
5
|
-
SearchQuery,
|
|
6
|
-
SearchQueryFields,
|
|
7
|
-
SortDirection,
|
|
8
|
-
SortType,
|
|
9
|
-
} from "@budibase/types"
|
|
1
|
+
import { Datasource, FieldType, SortDirection, SortType } from "@budibase/types"
|
|
10
2
|
import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants"
|
|
11
3
|
import { deepGet } from "./helpers"
|
|
12
4
|
|
|
@@ -81,13 +73,13 @@ export const NoEmptyFilterStrings = [
|
|
|
81
73
|
OperatorOptions.NotEquals.value,
|
|
82
74
|
OperatorOptions.Contains.value,
|
|
83
75
|
OperatorOptions.NotContains.value,
|
|
84
|
-
] as (keyof
|
|
76
|
+
] as (keyof QueryFields)[]
|
|
85
77
|
|
|
86
78
|
/**
|
|
87
79
|
* Removes any fields that contain empty strings that would cause inconsistent
|
|
88
80
|
* behaviour with how backend tables are filtered (no value means no filter).
|
|
89
81
|
*/
|
|
90
|
-
const cleanupQuery = (query:
|
|
82
|
+
const cleanupQuery = (query: Query) => {
|
|
91
83
|
if (!query) {
|
|
92
84
|
return query
|
|
93
85
|
}
|
|
@@ -118,12 +110,66 @@ const removeKeyNumbering = (key: string) => {
|
|
|
118
110
|
}
|
|
119
111
|
}
|
|
120
112
|
|
|
113
|
+
type Filter = {
|
|
114
|
+
operator: keyof Query
|
|
115
|
+
field: string
|
|
116
|
+
type: any
|
|
117
|
+
value: any
|
|
118
|
+
externalType: keyof typeof SqlNumberTypeRangeMap
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type Query = QueryFields & QueryConfig
|
|
122
|
+
type QueryFields = {
|
|
123
|
+
string?: {
|
|
124
|
+
[key: string]: string
|
|
125
|
+
}
|
|
126
|
+
fuzzy?: {
|
|
127
|
+
[key: string]: string
|
|
128
|
+
}
|
|
129
|
+
range?: {
|
|
130
|
+
[key: string]: {
|
|
131
|
+
high: number | string
|
|
132
|
+
low: number | string
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
equal?: {
|
|
136
|
+
[key: string]: any
|
|
137
|
+
}
|
|
138
|
+
notEqual?: {
|
|
139
|
+
[key: string]: any
|
|
140
|
+
}
|
|
141
|
+
empty?: {
|
|
142
|
+
[key: string]: any
|
|
143
|
+
}
|
|
144
|
+
notEmpty?: {
|
|
145
|
+
[key: string]: any
|
|
146
|
+
}
|
|
147
|
+
oneOf?: {
|
|
148
|
+
[key: string]: any[]
|
|
149
|
+
}
|
|
150
|
+
contains?: {
|
|
151
|
+
[key: string]: any[]
|
|
152
|
+
}
|
|
153
|
+
notContains?: {
|
|
154
|
+
[key: string]: any[]
|
|
155
|
+
}
|
|
156
|
+
containsAny?: {
|
|
157
|
+
[key: string]: any[]
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
type QueryConfig = {
|
|
162
|
+
allOr?: boolean
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
type QueryFieldsType = keyof QueryFields
|
|
166
|
+
|
|
121
167
|
/**
|
|
122
168
|
* Builds a lucene JSON query from the filter structure generated in the builder
|
|
123
169
|
* @param filter the builder filter structure
|
|
124
170
|
*/
|
|
125
|
-
export const buildLuceneQuery = (filter:
|
|
126
|
-
let query:
|
|
171
|
+
export const buildLuceneQuery = (filter: Filter[]) => {
|
|
172
|
+
let query: Query = {
|
|
127
173
|
string: {},
|
|
128
174
|
fuzzy: {},
|
|
129
175
|
range: {},
|
|
@@ -181,13 +227,9 @@ export const buildLuceneQuery = (filter: SearchFilter[]) => {
|
|
|
181
227
|
}
|
|
182
228
|
if (operator.startsWith("range") && query.range) {
|
|
183
229
|
const minint =
|
|
184
|
-
SqlNumberTypeRangeMap[
|
|
185
|
-
externalType as keyof typeof SqlNumberTypeRangeMap
|
|
186
|
-
]?.min || Number.MIN_SAFE_INTEGER
|
|
230
|
+
SqlNumberTypeRangeMap[externalType]?.min || Number.MIN_SAFE_INTEGER
|
|
187
231
|
const maxint =
|
|
188
|
-
SqlNumberTypeRangeMap[
|
|
189
|
-
externalType as keyof typeof SqlNumberTypeRangeMap
|
|
190
|
-
]?.max || Number.MAX_SAFE_INTEGER
|
|
232
|
+
SqlNumberTypeRangeMap[externalType]?.max || Number.MAX_SAFE_INTEGER
|
|
191
233
|
if (!query.range[field]) {
|
|
192
234
|
query.range[field] = {
|
|
193
235
|
low: type === "number" ? minint : "0000-00-00T00:00:00.000Z",
|
|
@@ -233,7 +275,7 @@ export const buildLuceneQuery = (filter: SearchFilter[]) => {
|
|
|
233
275
|
* @param docs the data
|
|
234
276
|
* @param query the JSON lucene query
|
|
235
277
|
*/
|
|
236
|
-
export const runLuceneQuery = (docs: any[], query?:
|
|
278
|
+
export const runLuceneQuery = (docs: any[], query?: Query) => {
|
|
237
279
|
if (!docs || !Array.isArray(docs)) {
|
|
238
280
|
return []
|
|
239
281
|
}
|
|
@@ -247,7 +289,7 @@ export const runLuceneQuery = (docs: any[], query?: SearchQuery) => {
|
|
|
247
289
|
// Iterates over a set of filters and evaluates a fail function against a doc
|
|
248
290
|
const match =
|
|
249
291
|
(
|
|
250
|
-
type:
|
|
292
|
+
type: QueryFieldsType,
|
|
251
293
|
failFn: (docValue: any, testValue: any) => boolean
|
|
252
294
|
) =>
|
|
253
295
|
(doc: any) => {
|
|
@@ -414,7 +456,7 @@ export const luceneLimit = (docs: any[], limit: string) => {
|
|
|
414
456
|
return docs.slice(0, numLimit)
|
|
415
457
|
}
|
|
416
458
|
|
|
417
|
-
export const hasFilters = (query?:
|
|
459
|
+
export const hasFilters = (query?: Query) => {
|
|
418
460
|
if (!query) {
|
|
419
461
|
return false
|
|
420
462
|
}
|