Allowing query backend to attempt to convert types in returned JSON to build a better base schema.
This commit is contained in:
parent
c50fa65e08
commit
d6f92c7039
|
@ -90,8 +90,8 @@ export function createQueriesStore() {
|
||||||
// Assume all the fields are strings and create a basic schema from the
|
// Assume all the fields are strings and create a basic schema from the
|
||||||
// unique fields returned by the server
|
// unique fields returned by the server
|
||||||
const schema = {}
|
const schema = {}
|
||||||
for (let field of result.schemaFields) {
|
for (let [field, type] of Object.entries(result.schemaFields)) {
|
||||||
schema[field] = "string"
|
schema[field] = type || "string"
|
||||||
}
|
}
|
||||||
return { ...result, schema, rows: result.rows || [] }
|
return { ...result, schema, rows: result.rows || [] }
|
||||||
},
|
},
|
||||||
|
|
|
@ -170,10 +170,7 @@ export const runLuceneQuery = (docs, query) => {
|
||||||
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]
|
||||||
let docValue = Helpers.deepGet(doc, key)
|
const docValue = Helpers.deepGet(doc, key)
|
||||||
if (typeof docValue !== "string" && typeof testValue === "string") {
|
|
||||||
docValue = docValue.toString()
|
|
||||||
}
|
|
||||||
if (failFn(docValue, testValue)) {
|
if (failFn(docValue, testValue)) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { generateQueryID, getQueryParams, isProdAppID } from "../../../db/utils"
|
import { generateQueryID, getQueryParams, isProdAppID } from "../../../db/utils"
|
||||||
import { BaseQueryVerbs } from "../../../constants"
|
import { BaseQueryVerbs, FieldTypes } from "../../../constants"
|
||||||
import { Thread, ThreadType } from "../../../threads"
|
import { Thread, ThreadType } from "../../../threads"
|
||||||
import { save as saveDatasource } from "../datasource"
|
import { save as saveDatasource } from "../datasource"
|
||||||
import { RestImporter } from "./import"
|
import { RestImporter } from "./import"
|
||||||
|
@ -154,10 +154,37 @@ export async function preview(ctx: any) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const { rows, keys, info, extra } = await quotas.addQuery(runFn)
|
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)
|
await events.query.previewed(datasource, query)
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
rows,
|
rows,
|
||||||
schemaFields: [...new Set(keys)],
|
schemaFields,
|
||||||
info,
|
info,
|
||||||
extra,
|
extra,
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,7 +215,10 @@ describe("/queries", () => {
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
// these responses come from the mock
|
// 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(res.body.rows.length).toEqual(1)
|
||||||
expect(events.query.previewed).toBeCalledTimes(1)
|
expect(events.query.previewed).toBeCalledTimes(1)
|
||||||
expect(events.query.previewed).toBeCalledWith(datasource, query)
|
expect(events.query.previewed).toBeCalledWith(datasource, query)
|
||||||
|
@ -289,7 +292,11 @@ describe("/queries", () => {
|
||||||
queryString: "test={{ variable2 }}",
|
queryString: "test={{ variable2 }}",
|
||||||
})
|
})
|
||||||
// these responses come from the mock
|
// 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")
|
expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -299,7 +306,11 @@ describe("/queries", () => {
|
||||||
path: "www.google.com",
|
path: "www.google.com",
|
||||||
queryString: "test={{ variable3 }}",
|
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")
|
expect(res.body.rows[0].url).toContain("doctype html")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -318,7 +329,11 @@ describe("/queries", () => {
|
||||||
path: "www.failonce.com",
|
path: "www.failonce.com",
|
||||||
queryString: "test={{ variable3 }}",
|
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)
|
expect(res.body.rows[0].fails).toEqual(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue