Create data-utils
This commit is contained in:
parent
10cba43ac4
commit
a24a1e33b3
|
@ -1 +1,2 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
|
dist/
|
|
@ -1,11 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "@budibase/data-utils",
|
"name": "@budibase/data-utils",
|
||||||
|
"private": true,
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "Shared data utils",
|
"description": "Shared data utils",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"author": "Budibase",
|
"author": "Budibase",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"private": true,
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"dev:builder": "tsc --watch"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "4.7.3"
|
"typescript": "4.7.3"
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,12 +109,35 @@ const removeKeyNumbering = (key: string) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Filter = {
|
||||||
|
operator: keyof Query
|
||||||
|
field: string
|
||||||
|
type: any
|
||||||
|
value: any
|
||||||
|
externalType: keyof typeof SqlNumberTypeRangeMap
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query = {
|
||||||
|
string: Record<string, any>
|
||||||
|
fuzzy: Record<string, any>
|
||||||
|
range: Record<string, { low: string | number; high: string | number }>
|
||||||
|
equal: Record<string, true>
|
||||||
|
notEqual: Record<string, true>
|
||||||
|
empty: Record<string, any>
|
||||||
|
notEmpty: Record<string, any>
|
||||||
|
contains: Record<string, any>
|
||||||
|
notContains: Record<string, any>
|
||||||
|
oneOf: Record<string, any>
|
||||||
|
containsAny: Record<string, any>
|
||||||
|
allOr?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a lucene JSON query from the filter structure generated in the builder
|
* Builds a lucene JSON query from the filter structure generated in the builder
|
||||||
* @param filter the builder filter structure
|
* @param filter the builder filter structure
|
||||||
*/
|
*/
|
||||||
export const buildLuceneQuery = (filter: any[]) => {
|
export const buildLuceneQuery = (filter: Filter[]) => {
|
||||||
let query = {
|
let query: Query = {
|
||||||
string: {},
|
string: {},
|
||||||
fuzzy: {},
|
fuzzy: {},
|
||||||
range: {},
|
range: {},
|
||||||
|
@ -131,7 +154,7 @@ export const buildLuceneQuery = (filter: any[]) => {
|
||||||
filter.forEach(expression => {
|
filter.forEach(expression => {
|
||||||
let { operator, field, type, value, externalType } = expression
|
let { operator, field, type, value, externalType } = expression
|
||||||
const isHbs =
|
const isHbs =
|
||||||
typeof value === "string" && value.match(HBS_REGEX)?.length > 0
|
typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
|
||||||
// Parse all values into correct types
|
// Parse all values into correct types
|
||||||
if (operator === "allOr") {
|
if (operator === "allOr") {
|
||||||
query.allOr = true
|
query.allOr = true
|
||||||
|
@ -181,9 +204,13 @@ export const buildLuceneQuery = (filter: any[]) => {
|
||||||
high: type === "number" ? maxint : "9999-00-00T00:00:00.000Z",
|
high: type === "number" ? maxint : "9999-00-00T00:00:00.000Z",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (operator === "rangeLow" && value != null && value !== "") {
|
if ((operator as any) === "rangeLow" && value != null && value !== "") {
|
||||||
query.range[field].low = value
|
query.range[field].low = value
|
||||||
} else if (operator === "rangeHigh" && value != null && value !== "") {
|
} else if (
|
||||||
|
(operator as any) === "rangeHigh" &&
|
||||||
|
value != null &&
|
||||||
|
value !== ""
|
||||||
|
) {
|
||||||
query.range[field].high = value
|
query.range[field].high = value
|
||||||
}
|
}
|
||||||
} else if (query[operator]) {
|
} else if (query[operator]) {
|
||||||
|
@ -242,25 +269,9 @@ export const runLuceneQuery = (
|
||||||
|
|
||||||
// Iterates over a set of filters and evaluates a fail function against a doc
|
// Iterates over a set of filters and evaluates a fail function against a doc
|
||||||
const match =
|
const match =
|
||||||
(
|
(type: string, failFn: (docValue: any, testValue: any) => boolean) =>
|
||||||
type: string,
|
|
||||||
failFn: {
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any): boolean
|
|
||||||
(docValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): boolean
|
|
||||||
(docValue: any, testValue: any): any
|
|
||||||
(arg0: any, arg1: unknown): any
|
|
||||||
}
|
|
||||||
) =>
|
|
||||||
(doc: any) => {
|
(doc: any) => {
|
||||||
const filters = Object.entries(query[type] || {})
|
const filters = Object.entries(query![type] || {})
|
||||||
for (let i = 0; i < filters.length; i++) {
|
for (let i = 0; i < filters.length; i++) {
|
||||||
const [key, testValue] = filters[i]
|
const [key, testValue] = filters[i]
|
||||||
const docValue = deepGet(doc, removeKeyNumbering(key))
|
const docValue = deepGet(doc, removeKeyNumbering(key))
|
||||||
|
@ -328,7 +339,7 @@ export const runLuceneQuery = (
|
||||||
})
|
})
|
||||||
|
|
||||||
// Process an includes match (fails if the value is not included)
|
// Process an includes match (fails if the value is not included)
|
||||||
const oneOf = match("oneOf", (docValue: any, testValue: string[]) => {
|
const oneOf = match("oneOf", (docValue: any, testValue: any) => {
|
||||||
if (typeof testValue === "string") {
|
if (typeof testValue === "string") {
|
||||||
testValue = testValue.split(",")
|
testValue = testValue.split(",")
|
||||||
if (typeof docValue === "number") {
|
if (typeof docValue === "number") {
|
||||||
|
@ -338,12 +349,9 @@ export const runLuceneQuery = (
|
||||||
return !testValue?.includes(docValue)
|
return !testValue?.includes(docValue)
|
||||||
})
|
})
|
||||||
|
|
||||||
const containsAny = match(
|
const containsAny = match("containsAny", (docValue: any, testValue: any) => {
|
||||||
"containsAny",
|
return !docValue?.includes(...testValue)
|
||||||
(docValue: string | any[], testValue: any) => {
|
})
|
||||||
return !docValue?.includes(...testValue)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const contains = match(
|
const contains = match(
|
||||||
"contains",
|
"contains",
|
||||||
|
|
|
@ -10,15 +10,11 @@
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"types": [ "node", "jest" ],
|
"types": ["node"],
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"skipLibCheck": true
|
"skipLibCheck": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["**/*.js", "**/*.ts", "package.json"],
|
||||||
"**/*.js",
|
|
||||||
"**/*.ts",
|
|
||||||
"package.json"
|
|
||||||
],
|
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
"dist",
|
"dist",
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
{
|
{
|
||||||
"extends": "./tsconfig.build.json",
|
"extends": "./tsconfig.build.json"
|
||||||
"compilerOptions": {
|
|
||||||
"composite": true,
|
|
||||||
"baseUrl": "."
|
|
||||||
},
|
|
||||||
"exclude": ["node_modules", "dist"]
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,12 @@ yarn unlink
|
||||||
yarn link
|
yarn link
|
||||||
cd -
|
cd -
|
||||||
|
|
||||||
|
echo "Linking data-utils"
|
||||||
|
cd packages/data-utils
|
||||||
|
yarn unlink
|
||||||
|
yarn link
|
||||||
|
cd -
|
||||||
|
|
||||||
if [ -d "../budibase-pro" ]; then
|
if [ -d "../budibase-pro" ]; then
|
||||||
cd ../budibase-pro
|
cd ../budibase-pro
|
||||||
echo "Bootstrapping budibase-pro"
|
echo "Bootstrapping budibase-pro"
|
||||||
|
|
Loading…
Reference in New Issue