2022-02-22 16:06:08 +01:00
|
|
|
const Resource = require("./utils/Resource")
|
2022-03-01 19:35:08 +01:00
|
|
|
const { object } = require("./utils")
|
2022-12-15 13:13:42 +01:00
|
|
|
const { BaseQueryVerbs } = require("../../dist/constants")
|
2022-02-22 16:06:08 +01:00
|
|
|
|
|
|
|
const query = {
|
|
|
|
_id: "query_datasource_plus_4d8be0c506b9465daf4bf84d890fdab6_454854487c574d45bc4029b1e153219e",
|
|
|
|
datasourceId: "datasource_plus_4d8be0c506b9465daf4bf84d890fdab6",
|
|
|
|
parameters: [],
|
|
|
|
fields: {
|
|
|
|
sql: "select * from persons",
|
|
|
|
},
|
|
|
|
queryVerb: "read",
|
|
|
|
name: "Help",
|
|
|
|
schema: {
|
|
|
|
personid: {
|
|
|
|
name: "personid",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
lastname: {
|
|
|
|
name: "lastname",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
firstname: {
|
|
|
|
name: "firstname",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
address: {
|
|
|
|
name: "address",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
city: {
|
|
|
|
name: "city",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
transformer: "return data",
|
|
|
|
readable: true,
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:31:32 +01:00
|
|
|
const restResponse = {
|
|
|
|
value: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
value: "<html lang='en-GB'></html>",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
pagination: {
|
|
|
|
cursor: "2",
|
|
|
|
},
|
|
|
|
raw: "<html lang='en-GB'></html>",
|
|
|
|
headers: {
|
|
|
|
"content-type": "text/html; charset=ISO-8859-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const sqlResponse = {
|
|
|
|
value: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
personid: 1,
|
|
|
|
lastname: "Hughes",
|
|
|
|
firstname: "Mike",
|
|
|
|
address: "123 Fake Street",
|
|
|
|
city: "Belfast",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
personid: 2,
|
|
|
|
lastname: "Smith",
|
|
|
|
firstname: "John",
|
|
|
|
address: "64 Updown Road",
|
|
|
|
city: "Dublin",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
const querySchema = object(
|
|
|
|
{
|
|
|
|
_id: {
|
|
|
|
description: "The ID of the query.",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
datasourceId: {
|
2022-09-14 13:11:20 +02:00
|
|
|
description: "The ID of the datasource the query belongs to.",
|
2022-03-01 19:35:08 +01:00
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
parameters: {
|
|
|
|
description: "The bindings which are required to perform this query.",
|
|
|
|
type: "array",
|
|
|
|
items: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
description:
|
|
|
|
"The fields that are used to perform this query, e.g. the sql statement",
|
|
|
|
type: "object",
|
|
|
|
},
|
|
|
|
queryVerb: {
|
|
|
|
description: "The verb that describes this query.",
|
|
|
|
enum: Object.values(BaseQueryVerbs),
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
description: "The name of the query.",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
schema: {
|
|
|
|
description:
|
|
|
|
"The schema of the data returned when the query is executed.",
|
|
|
|
type: "object",
|
|
|
|
},
|
|
|
|
transformer: {
|
|
|
|
description:
|
|
|
|
"The JavaScript transformer function, applied after the query responds with data.",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
readable: {
|
|
|
|
description: "Whether the query has readable data.",
|
|
|
|
type: "boolean",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ required: ["name", "schema", "_id"] }
|
|
|
|
)
|
|
|
|
|
|
|
|
const executeQuerySchema = {
|
2022-03-28 19:33:54 +02:00
|
|
|
description: "The parameters required for executing a query.",
|
2022-02-25 20:26:19 +01:00
|
|
|
type: "object",
|
2022-03-28 19:33:54 +02:00
|
|
|
properties: {
|
|
|
|
parameters: {
|
|
|
|
type: "object",
|
|
|
|
description:
|
|
|
|
"This contains the required parameters for the query, this depends on query type, setup and bindings.",
|
|
|
|
additionalProperties: {
|
|
|
|
description:
|
|
|
|
"Key value properties of any type, depending on the query output schema.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pagination: {
|
|
|
|
type: "object",
|
|
|
|
description:
|
|
|
|
"For supported query types (currently on REST) pagination can be performed using these properties.",
|
|
|
|
properties: {
|
|
|
|
page: {
|
|
|
|
type: "string",
|
|
|
|
description:
|
|
|
|
"The page which has been returned from a previous query.",
|
|
|
|
},
|
|
|
|
limit: {
|
|
|
|
type: "number",
|
|
|
|
description: "The number of rows to return per page.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 20:26:19 +01:00
|
|
|
},
|
|
|
|
}
|
2022-02-22 16:06:08 +01:00
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
const executeQueryOutputSchema = object(
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
description: "The data response from the query.",
|
|
|
|
type: "array",
|
|
|
|
items: {
|
|
|
|
type: "object",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extra: {
|
|
|
|
description:
|
|
|
|
"Extra information that is not part of the main data, e.g. headers.",
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
headers: {
|
|
|
|
description:
|
|
|
|
"If carrying out a REST request, this will contain the response headers.",
|
|
|
|
type: "object",
|
|
|
|
},
|
|
|
|
raw: {
|
|
|
|
description: "The raw query response, as a string.",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pagination: {
|
|
|
|
description:
|
|
|
|
"If pagination is supported, this will contain the bookmark/anchor information for it.",
|
|
|
|
type: "object",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ required: ["data"] }
|
|
|
|
)
|
|
|
|
|
2022-02-22 16:06:08 +01:00
|
|
|
module.exports = new Resource()
|
|
|
|
.setExamples({
|
|
|
|
query: {
|
|
|
|
value: {
|
2022-03-01 15:37:35 +01:00
|
|
|
data: query,
|
2022-02-22 16:06:08 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
queries: {
|
|
|
|
value: {
|
2022-03-01 15:37:35 +01:00
|
|
|
data: [query],
|
2022-02-22 16:06:08 +01:00
|
|
|
},
|
|
|
|
},
|
2022-02-23 19:31:32 +01:00
|
|
|
restResponse,
|
|
|
|
sqlResponse,
|
2022-02-22 16:06:08 +01:00
|
|
|
})
|
|
|
|
.setSchemas({
|
2022-03-01 19:35:08 +01:00
|
|
|
executeQuery: executeQuerySchema,
|
|
|
|
executeQueryOutput: executeQueryOutputSchema,
|
2022-02-22 16:06:08 +01:00
|
|
|
query: querySchema,
|
2022-03-08 18:42:26 +01:00
|
|
|
querySearch: object({
|
|
|
|
data: {
|
|
|
|
type: "array",
|
|
|
|
items: querySchema,
|
|
|
|
},
|
|
|
|
}),
|
2022-02-22 16:06:08 +01:00
|
|
|
})
|