diff --git a/packages/builder/src/components/integration/RestQueryViewer.svelte b/packages/builder/src/components/integration/RestQueryViewer.svelte index 64834c1b0b..6f8a0e13dc 100644 --- a/packages/builder/src/components/integration/RestQueryViewer.svelte +++ b/packages/builder/src/components/integration/RestQueryViewer.svelte @@ -231,6 +231,7 @@ notifications.info("Request did not return any data") } else { response.info = response.info || { code: 200 } + console.log(response) schema = response.schema notifications.success("Request sent successfully") } diff --git a/packages/builder/src/stores/backend/queries.js b/packages/builder/src/stores/backend/queries.js index 1ec23b300c..7f5f83a792 100644 --- a/packages/builder/src/stores/backend/queries.js +++ b/packages/builder/src/stores/backend/queries.js @@ -89,8 +89,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, type] of Object.entries(result.schemaFields)) { - schema[field] = type || "string" + for (let [field, metadata] of Object.entries(result.schema)) { + schema[field] = metadata || { type: "string" } } return { ...result, schema, rows: result.rows || [] } } diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 7f5adbb1f6..5a3d6db796 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -169,7 +169,7 @@ export async function preview(ctx: UserCtx) { } const { rows, keys, info, extra } = (await Runner.run(inputs)) as any - const schemaFields: Record = {} + const previewSchema: Record = {} const makeQuerySchema = (type: FieldType, name: string): QuerySchema => ({ type, name, @@ -178,33 +178,33 @@ export async function preview(ctx: UserCtx) { for (let key of [...new Set(keys)] as string[]) { const field = rows[0][key] let type = typeof field, - fieldType = makeQuerySchema(FieldType.STRING, key) + fieldMetadata = makeQuerySchema(FieldType.STRING, key) if (field) switch (type) { case "boolean": - schemaFields[key] = makeQuerySchema(FieldType.BOOLEAN, key) + fieldMetadata = makeQuerySchema(FieldType.BOOLEAN, key) break case "object": if (field instanceof Date) { - fieldType = makeQuerySchema(FieldType.DATETIME, key) + fieldMetadata = makeQuerySchema(FieldType.DATETIME, key) } else if (Array.isArray(field)) { - fieldType = makeQuerySchema(FieldType.ARRAY, key) + fieldMetadata = makeQuerySchema(FieldType.ARRAY, key) } else { - fieldType = makeQuerySchema(FieldType.JSON, key) + fieldMetadata = makeQuerySchema(FieldType.JSON, key) } break case "number": - fieldType = makeQuerySchema(FieldType.NUMBER, key) + fieldMetadata = makeQuerySchema(FieldType.NUMBER, key) break } - schemaFields[key] = fieldType + previewSchema[key] = fieldMetadata } } // if existing schema, update to include any previous schema keys if (existingSchema) { - for (let key of Object.keys(schemaFields)) { + for (let key of Object.keys(previewSchema)) { if (existingSchema[key]?.type) { - schemaFields[key] = existingSchema[key].type + previewSchema[key] = existingSchema[key].type } } } @@ -213,7 +213,7 @@ export async function preview(ctx: UserCtx) { await events.query.previewed(datasource, query) ctx.body = { rows, - schemaFields, + schema: previewSchema, info, extra, } diff --git a/packages/server/src/api/routes/tests/query.seq.spec.js b/packages/server/src/api/routes/tests/query.seq.spec.js index 05ca5f7cbb..b15a5f4d6d 100644 --- a/packages/server/src/api/routes/tests/query.seq.spec.js +++ b/packages/server/src/api/routes/tests/query.seq.spec.js @@ -235,9 +235,9 @@ describe("/queries", () => { .expect("Content-Type", /json/) .expect(200) // these responses come from the mock - expect(res.body.schemaFields).toEqual({ - a: "string", - b: "number", + expect(res.body.schema).toEqual({ + a: { type: "string", name: "a" }, + b: { type: "number", name: "b" }, }) expect(res.body.rows.length).toEqual(1) expect(events.query.previewed).toBeCalledTimes(1) @@ -300,10 +300,10 @@ describe("/queries", () => { queryString: "test={{ variable2 }}", }) // these responses come from the mock - expect(res.body.schemaFields).toEqual({ - opts: "json", - url: "string", - value: "string", + expect(res.body.schema).toEqual({ + opts: { type: "json", name: "opts" }, + url: { type: "string", name: "url" }, + value: { type: "string", name: "value" }, }) expect(res.body.rows[0].url).toEqual("http://www.google.com?test=1") }) @@ -314,10 +314,10 @@ describe("/queries", () => { path: "www.google.com", queryString: "test={{ variable3 }}", }) - expect(res.body.schemaFields).toEqual({ - opts: "json", - url: "string", - value: "string", + expect(res.body.schema).toEqual({ + opts: { type: "json", name: "opts" }, + url: { type: "string", name: "url" }, + value: { type: "string", name: "value" }, }) expect(res.body.rows[0].url).toContain("doctype%20html") }) @@ -337,10 +337,10 @@ describe("/queries", () => { path: "www.failonce.com", queryString: "test={{ variable3 }}", }) - expect(res.body.schemaFields).toEqual({ - fails: "number", - opts: "json", - url: "string", + expect(res.body.schema).toEqual({ + fails: { type: "number", name: "fails" }, + opts: { type: "json", name: "opts" }, + url: { type: "string", name: "url" }, }) expect(res.body.rows[0].fails).toEqual(1) }) diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index 102dfe2935..91966658a6 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -376,8 +376,8 @@ export function checkExternalTables( errors[name] = "Table must have a primary key." } - const schemaFields = Object.keys(table.schema) - if (schemaFields.find(f => invalidColumns.includes(f))) { + const columnNames = Object.keys(table.schema) + if (columnNames.find(f => invalidColumns.includes(f))) { errors[name] = "Table contains invalid columns." } }