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:
mike12345567 2021-02-05 16:45:23 +00:00
parent 45b65c6a11
commit 98b23c999d
3 changed files with 40 additions and 10 deletions

View File

@ -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)

View File

@ -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),

View File

@ -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",
}