Allowing query backend to attempt to convert types in returned JSON to build a better base schema.

This commit is contained in:
mike12345567 2022-07-13 10:53:50 +01:00
parent c50fa65e08
commit d6f92c7039
4 changed files with 51 additions and 12 deletions

View File

@ -90,8 +90,8 @@ export function createQueriesStore() {
// Assume all the fields are strings and create a basic schema from the
// unique fields returned by the server
const schema = {}
for (let field of result.schemaFields) {
schema[field] = "string"
for (let [field, type] of Object.entries(result.schemaFields)) {
schema[field] = type || "string"
}
return { ...result, schema, rows: result.rows || [] }
},

View File

@ -170,10 +170,7 @@ export const runLuceneQuery = (docs, query) => {
const filters = Object.entries(query[type] || {})
for (let i = 0; i < filters.length; i++) {
const [key, testValue] = filters[i]
let docValue = Helpers.deepGet(doc, key)
if (typeof docValue !== "string" && typeof testValue === "string") {
docValue = docValue.toString()
}
const docValue = Helpers.deepGet(doc, key)
if (failFn(docValue, testValue)) {
return false
}

View File

@ -1,5 +1,5 @@
import { generateQueryID, getQueryParams, isProdAppID } from "../../../db/utils"
import { BaseQueryVerbs } from "../../../constants"
import { BaseQueryVerbs, FieldTypes } from "../../../constants"
import { Thread, ThreadType } from "../../../threads"
import { save as saveDatasource } from "../datasource"
import { RestImporter } from "./import"
@ -154,10 +154,37 @@ export async function preview(ctx: any) {
},
})
const { rows, keys, info, extra } = await quotas.addQuery(runFn)
const schemaFields: any = {}
if (rows?.length > 0) {
for (let key of [...new Set(keys)] as string[]) {
const field = rows[0][key]
let type = typeof field,
fieldType = FieldTypes.STRING
if (field)
switch (type) {
case "boolean":
schemaFields[key] = FieldTypes.BOOLEAN
break
case "object":
if (field instanceof Date) {
fieldType = FieldTypes.DATETIME
} else if (Array.isArray(field)) {
fieldType = FieldTypes.ARRAY
} else {
fieldType = FieldTypes.JSON
}
break
case "number":
fieldType = FieldTypes.NUMBER
break
}
schemaFields[key] = fieldType
}
}
await events.query.previewed(datasource, query)
ctx.body = {
rows,
schemaFields: [...new Set(keys)],
schemaFields,
info,
extra,
}

View File

@ -215,7 +215,10 @@ describe("/queries", () => {
.expect("Content-Type", /json/)
.expect(200)
// these responses come from the mock
expect(res.body.schemaFields).toEqual(["a", "b"])
expect(res.body.schemaFields).toEqual({
"a": "string",
"b": "number",
})
expect(res.body.rows.length).toEqual(1)
expect(events.query.previewed).toBeCalledTimes(1)
expect(events.query.previewed).toBeCalledWith(datasource, query)
@ -289,7 +292,11 @@ describe("/queries", () => {
queryString: "test={{ variable2 }}",
})
// these responses come from the mock
expect(res.body.schemaFields).toEqual(["url", "opts", "value"])
expect(res.body.schemaFields).toEqual({
"opts": "json",
"url": "string",
"value": "string",
})
expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1")
})
@ -299,7 +306,11 @@ describe("/queries", () => {
path: "www.google.com",
queryString: "test={{ variable3 }}",
})
expect(res.body.schemaFields).toEqual(["url", "opts", "value"])
expect(res.body.schemaFields).toEqual({
"opts": "json",
"url": "string",
"value": "string"
})
expect(res.body.rows[0].url).toContain("doctype html")
})
@ -318,7 +329,11 @@ describe("/queries", () => {
path: "www.failonce.com",
queryString: "test={{ variable3 }}",
})
expect(res.body.schemaFields).toEqual(["fails", "url", "opts"])
expect(res.body.schemaFields).toEqual({
"fails": "number",
"opts": "json",
"url": "string"
})
expect(res.body.rows[0].fails).toEqual(1)
})