1
0
Fork 0
mirror of synced 2024-09-15 16:59:43 +12:00
budibase/packages/shared-core/src/utils.ts
melohagan c12e5fd196
Fix array type missing from query schema selector (#12772)
* Tidy MongoDB aggregation pipeline view

* Remove unused code

* WIP

* Add bindings for bindings drawer

* Is not external table if it's a query

* Add QueryArrayFetch

* Bug fix

* JavaScript is the worst

* refactor

* Add array label to query schema

* Remove console log

* type fix

* Don't include Array in SchemaTypeOptions, but show label

* Fix bindings

* refactor

* Rename isObject to hasSchema

* WIP

* Typing WIP

* Type not Types

* Unused import

* type fix

* Handle json array subtype

* Support queryarray datasource type

* refactor

* yarn lock

* update account portal

---------

Co-authored-by: Sam Rose <hello@samwho.dev>
2024-02-19 09:13:03 +00:00

69 lines
1.4 KiB
TypeScript

import * as Constants from "./constants"
export function unreachable(
value: never,
message = `No such case in exhaustive switch: ${value}`
) {
throw new Error(message)
}
export async function parallelForeach<T>(
items: T[],
task: (item: T) => Promise<void>,
maxConcurrency: number
): Promise<void> {
const promises: Promise<void>[] = []
let index = 0
const processItem = async (item: T) => {
try {
await task(item)
} finally {
processNext()
}
}
const processNext = () => {
if (index >= items.length) {
// No more items to process
return
}
const item = items[index]
index++
const promise = processItem(item)
promises.push(promise)
if (promises.length >= maxConcurrency) {
Promise.race(promises).then(processNext)
} else {
processNext()
}
}
processNext()
await Promise.all(promises)
}
export function filterValueToLabel() {
return Object.keys(Constants.OperatorOptions).reduce(
(acc: { [key: string]: string }, key: string) => {
const ops: { [key: string]: any } = Constants.OperatorOptions
const op: { [key: string]: string } = ops[key]
acc[op["value"]] = op.label
return acc
},
{}
)
}
export function hasSchema(test: any) {
return (
typeof test === "object" &&
!Array.isArray(test) &&
test !== null &&
!(test instanceof Date) &&
Object.keys(test).length > 0
)
}