Update to add readable prop to every 'read' query to follow a consistent pattern as well as adding a get endpoint for a single query.
This commit is contained in:
parent
8eeda82511
commit
d5293e299f
|
@ -2,6 +2,20 @@ const { processString } = require("@budibase/string-templates")
|
||||||
const CouchDB = require("../../db")
|
const CouchDB = require("../../db")
|
||||||
const { generateQueryID, getQueryParams } = require("../../db/utils")
|
const { generateQueryID, getQueryParams } = require("../../db/utils")
|
||||||
const { integrations } = require("../../integrations")
|
const { integrations } = require("../../integrations")
|
||||||
|
const { BaseQueryVerbs } = require("../../constants")
|
||||||
|
const env = require("../../environment")
|
||||||
|
|
||||||
|
// simple function to append "readable" to all read queries
|
||||||
|
function enrichQueries(input) {
|
||||||
|
const wasArray = Array.isArray(input)
|
||||||
|
const queries = wasArray ? input : [input]
|
||||||
|
for (let query of queries) {
|
||||||
|
if (query.queryVerb === BaseQueryVerbs.READ) {
|
||||||
|
query.readable = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wasArray ? queries : queries[0]
|
||||||
|
}
|
||||||
|
|
||||||
function formatResponse(resp) {
|
function formatResponse(resp) {
|
||||||
if (typeof resp === "string") {
|
if (typeof resp === "string") {
|
||||||
|
@ -21,7 +35,7 @@ exports.fetch = async function(ctx) {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
ctx.body = body.rows.map(row => row.doc)
|
ctx.body = enrichQueries(body.rows.map(row => row.doc))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.save = async function(ctx) {
|
exports.save = async function(ctx) {
|
||||||
|
@ -61,6 +75,18 @@ async function enrichQueryFields(fields, parameters) {
|
||||||
return enrichedQuery
|
return enrichedQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.find = async function(ctx) {
|
||||||
|
const db = new CouchDB(ctx.user.appId)
|
||||||
|
const query = enrichQueries(await db.get(ctx.params.queryId))
|
||||||
|
// remove properties that could be dangerous in real app
|
||||||
|
if (env.CLOUD) {
|
||||||
|
delete query.fields
|
||||||
|
delete query.parameters
|
||||||
|
delete query.schema
|
||||||
|
}
|
||||||
|
ctx.body = query
|
||||||
|
}
|
||||||
|
|
||||||
exports.preview = async function(ctx) {
|
exports.preview = async function(ctx) {
|
||||||
const db = new CouchDB(ctx.user.appId)
|
const db = new CouchDB(ctx.user.appId)
|
||||||
|
|
||||||
|
|
|
@ -11,13 +11,6 @@ const joiValidator = require("../../middleware/joi-validator")
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
const QueryVerb = {
|
|
||||||
Create: "create",
|
|
||||||
Read: "read",
|
|
||||||
Update: "update",
|
|
||||||
Delete: "delete",
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateQueryValidation() {
|
function generateQueryValidation() {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return joiValidator.body(Joi.object({
|
return joiValidator.body(Joi.object({
|
||||||
|
@ -31,7 +24,7 @@ function generateQueryValidation() {
|
||||||
name: Joi.string(),
|
name: Joi.string(),
|
||||||
default: Joi.string()
|
default: Joi.string()
|
||||||
})),
|
})),
|
||||||
queryVerb: Joi.string().allow(...Object.values(QueryVerb)).required(),
|
queryVerb: Joi.string().allow().required(),
|
||||||
schema: Joi.object({}).required().unknown(true)
|
schema: Joi.object({}).required().unknown(true)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -40,7 +33,7 @@ function generateQueryPreviewValidation() {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return joiValidator.body(Joi.object({
|
return joiValidator.body(Joi.object({
|
||||||
fields: Joi.object().required(),
|
fields: Joi.object().required(),
|
||||||
queryVerb: Joi.string().allow(...Object.values(QueryVerb)).required(),
|
queryVerb: Joi.string().allow().required(),
|
||||||
datasourceId: Joi.string().required(),
|
datasourceId: Joi.string().required(),
|
||||||
parameters: Joi.object({}).required().unknown(true)
|
parameters: Joi.object({}).required().unknown(true)
|
||||||
}))
|
}))
|
||||||
|
@ -60,6 +53,11 @@ router
|
||||||
generateQueryPreviewValidation(),
|
generateQueryPreviewValidation(),
|
||||||
queryController.preview
|
queryController.preview
|
||||||
)
|
)
|
||||||
|
.get(
|
||||||
|
"/api/queries/:queryId",
|
||||||
|
authorized(PermissionTypes.QUERY, PermissionLevels.READ),
|
||||||
|
queryController.find
|
||||||
|
)
|
||||||
.post(
|
.post(
|
||||||
"/api/queries/:queryId",
|
"/api/queries/:queryId",
|
||||||
authorized(PermissionTypes.QUERY, PermissionLevels.WRITE),
|
authorized(PermissionTypes.QUERY, PermissionLevels.WRITE),
|
||||||
|
|
|
@ -44,3 +44,9 @@ exports.USERS_TABLE_SCHEMA = USERS_TABLE_SCHEMA
|
||||||
exports.BUILDER_CONFIG_DB = "builder-config-db"
|
exports.BUILDER_CONFIG_DB = "builder-config-db"
|
||||||
exports.HOSTING_DOC = "hosting-doc"
|
exports.HOSTING_DOC = "hosting-doc"
|
||||||
exports.OBJ_STORE_DIRECTORY = "/app-assets/assets"
|
exports.OBJ_STORE_DIRECTORY = "/app-assets/assets"
|
||||||
|
exports.BaseQueryVerbs = {
|
||||||
|
CREATE: "create",
|
||||||
|
READ: "read",
|
||||||
|
UPDATE: "update",
|
||||||
|
DELETE: "delete",
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue