Merge pull request #4134 from Budibase/fix/default-query-param

Multiple fixes for v1.0.45
This commit is contained in:
Michael Drury 2022-01-20 19:51:37 +00:00 committed by GitHub
commit 534182ea28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 25 deletions

View File

@ -15,8 +15,6 @@
queryBindings = [...queryBindings, {}] queryBindings = [...queryBindings, {}]
} }
$: console.log(bindings)
function deleteQueryBinding(idx) { function deleteQueryBinding(idx) {
queryBindings.splice(idx, 1) queryBindings.splice(idx, 1)
queryBindings = queryBindings queryBindings = queryBindings

View File

@ -111,12 +111,6 @@ export default class DataFetch {
*/ */
async getInitialData() { async getInitialData() {
const { datasource, filter, sortColumn, paginate } = this.options const { datasource, filter, sortColumn, paginate } = this.options
const tableId = datasource?.tableId
// Ensure table ID exists
if (!tableId) {
return
}
// Fetch datasource definition and determine feature flags // Fetch datasource definition and determine feature flags
const definition = await this.constructor.getDefinition(datasource) const definition = await this.constructor.getDefinition(datasource)

View File

@ -141,6 +141,16 @@ async function execute(ctx, opts = { rowsOnly: false }) {
const query = await db.get(ctx.params.queryId) const query = await db.get(ctx.params.queryId)
const datasource = await db.get(query.datasourceId) const datasource = await db.get(query.datasourceId)
const enrichedParameters = ctx.request.body.parameters || {}
// make sure parameters are fully enriched with defaults
if (query && query.parameters) {
for (let parameter of query.parameters) {
if (!enrichedParameters[parameter.name]) {
enrichedParameters[parameter.name] = parameter.default
}
}
}
// call the relevant CRUD method on the integration class // call the relevant CRUD method on the integration class
try { try {
const { rows, pagination, extra } = await Runner.run({ const { rows, pagination, extra } = await Runner.run({
@ -149,7 +159,7 @@ async function execute(ctx, opts = { rowsOnly: false }) {
queryVerb: query.queryVerb, queryVerb: query.queryVerb,
fields: query.fields, fields: query.fields,
pagination: ctx.request.body.pagination, pagination: ctx.request.body.pagination,
parameters: ctx.request.body.parameters, parameters: enrichedParameters,
transformer: query.transformer, transformer: query.transformer,
queryId: ctx.params.queryId, queryId: ctx.params.queryId,
}) })
@ -178,8 +188,9 @@ const removeDynamicVariables = async (db, queryId) => {
if (dynamicVariables) { if (dynamicVariables) {
// delete dynamic variables from the datasource // delete dynamic variables from the datasource
const newVariables = dynamicVariables.filter(dv => dv.queryId !== queryId) datasource.config.dynamicVariables = dynamicVariables.filter(
datasource.config.dynamicVariables = newVariables dv => dv.queryId !== queryId
)
await db.put(datasource) await db.put(datasource)
// invalidate the deleted variables // invalidate the deleted variables

View File

@ -52,21 +52,29 @@ exports.validate = async ({ appId, tableId, row, table }) => {
const constraints = cloneDeep(table.schema[fieldName].constraints) const constraints = cloneDeep(table.schema[fieldName].constraints)
const type = table.schema[fieldName].type const type = table.schema[fieldName].type
// special case for options, need to always allow unselected (null) // special case for options, need to always allow unselected (null)
if ( if (type === FieldTypes.OPTIONS && constraints.inclusion) {
(type === FieldTypes.OPTIONS || type === FieldTypes.ARRAY) &&
constraints.inclusion
) {
constraints.inclusion.push(null) constraints.inclusion.push(null)
} }
let res let res
// Validate.js doesn't seem to handle array // Validate.js doesn't seem to handle array
if (type === FieldTypes.ARRAY && row[fieldName] && row[fieldName].length) { if (type === FieldTypes.ARRAY) {
row[fieldName].map(val => { const hasValues =
if (!constraints.inclusion.includes(val)) { Array.isArray(row[fieldName]) && row[fieldName].length > 0
errors[fieldName] = "Field not in list"
} // Check values are valid if values are specified
}) if (hasValues) {
row[fieldName].map(val => {
if (!constraints.inclusion.includes(val)) {
errors[fieldName] = "Value not in list"
}
})
}
// Check for required constraint
if (constraints.presence === true && !hasValues) {
errors[fieldName] = "Required field"
}
} else if (type === FieldTypes.JSON && typeof row[fieldName] === "string") { } else if (type === FieldTypes.JSON && typeof row[fieldName] === "string") {
// this should only happen if there is an error // this should only happen if there is an error
try { try {

View File

@ -1,5 +1,6 @@
const { atob } = require("../utilities") const { atob } = require("../utilities")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
const { LITERAL_MARKER } = require("../helpers/constants")
// The method of executing JS scripts depends on the bundle being built. // The method of executing JS scripts depends on the bundle being built.
// This setter is used in the entrypoint (either index.cjs or index.mjs). // This setter is used in the entrypoint (either index.cjs or index.mjs).
@ -46,8 +47,9 @@ module.exports.processJS = (handlebars, context) => {
$: path => getContextValue(path, cloneDeep(context)), $: path => getContextValue(path, cloneDeep(context)),
} }
// Create a sandbox with out context and run the JS // Create a sandbox with our context and run the JS
return runJS(js, sandboxContext) const res = { data: runJS(js, sandboxContext) }
return `{{${LITERAL_MARKER} js_result-${JSON.stringify(res)}}}`
} catch (error) { } catch (error) {
return "Error while executing JS" return "Error while executing JS"
} }

View File

@ -112,9 +112,10 @@ module.exports.processStringSync = (string, context, opts) => {
const template = instance.compile(string, { const template = instance.compile(string, {
strict: false, strict: false,
}) })
const now = Math.floor(Date.now() / 1000) * 1000
return processors.postprocess( return processors.postprocess(
template({ template({
now: new Date().toISOString(), now: new Date(now).toISOString(),
...context, ...context,
}) })
) )

View File

@ -36,6 +36,11 @@ module.exports.processors = [
return value === "true" return value === "true"
case "object": case "object":
return JSON.parse(value) return JSON.parse(value)
case "js_result":
// We use the literal helper to process the result of JS expressions
// as we want to be able to return any types.
// We wrap the value in an abject to be able to use undefined properly.
return JSON.parse(value).data
} }
return value return value
}), }),

View File

@ -7,7 +7,7 @@ const processJS = (js, context) => {
describe("Test the JavaScript helper", () => { describe("Test the JavaScript helper", () => {
it("should execute a simple expression", () => { it("should execute a simple expression", () => {
const output = processJS(`return 1 + 2`) const output = processJS(`return 1 + 2`)
expect(output).toBe("3") expect(output).toBe(3)
}) })
it("should be able to use primitive bindings", () => { it("should be able to use primitive bindings", () => {
@ -50,6 +50,52 @@ describe("Test the JavaScript helper", () => {
expect(output).toBe("shazbat") expect(output).toBe("shazbat")
}) })
it("should be able to return an object", () => {
const output = processJS(`return $("foo")`, {
foo: {
bar: {
baz: "shazbat",
},
},
})
expect(output.bar.baz).toBe("shazbat")
})
it("should be able to return an array", () => {
const output = processJS(`return $("foo")`, {
foo: ["a", "b", "c"],
})
expect(output[2]).toBe("c")
})
it("should be able to return null", () => {
const output = processJS(`return $("foo")`, {
foo: null,
})
expect(output).toBe(null)
})
it("should be able to return undefined", () => {
const output = processJS(`return $("foo")`, {
foo: undefined,
})
expect(output).toBe(undefined)
})
it("should be able to return 0", () => {
const output = processJS(`return $("foo")`, {
foo: 0,
})
expect(output).toBe(0)
})
it("should be able to return an empty string", () => {
const output = processJS(`return $("foo")`, {
foo: "",
})
expect(output).toBe("")
})
it("should be able to use a deep array binding", () => { it("should be able to use a deep array binding", () => {
const output = processJS(`return $("foo.0.bar")`, { const output = processJS(`return $("foo.0.bar")`, {
foo: [ foo: [