Joi schema validation for plus datasources

This commit is contained in:
Martin McKeaveney 2021-06-04 10:50:02 +01:00
parent 51e2346389
commit 7ecdb60cc6
3 changed files with 36 additions and 8 deletions

View File

@ -6,7 +6,6 @@ const {
} = require("../../db/utils")
const { integrations } = require("../../integrations")
const plusIntegrations = require("../../integrations/plus")
const PostgresConnector = require("../../integrations/plus/Postgres")
exports.fetch = async function (ctx) {
const database = new CouchDB(ctx.appId)
@ -65,7 +64,7 @@ exports.find = async function (ctx) {
// TODO: merge endpoint with main datasource endpoint
exports.plus = async function (ctx) {
const db = new CouchDB(ctx.appId)
const db = new CouchDB("testapp123")
const PlusConnector = plusIntegrations[ctx.request.body.source]
@ -73,11 +72,11 @@ exports.plus = async function (ctx) {
await connector.init()
const datasource = {
_id: generateDatasourceID(),
type: "datasource:plus",
_id: generateDatasourceID({ plus: true }),
type: "datasource_plus",
relationships: [],
...ctx.request.body,
entities: connector.tables,
relationships: {},
}
const response = await db.post(datasource)

View File

@ -1,14 +1,36 @@
const Router = require("@koa/router")
const datasourceController = require("../controllers/datasource")
const authorized = require("../../middleware/authorized")
const joiValidator = require("../../middleware/joi-validator")
const {
BUILDER,
PermissionLevels,
PermissionTypes,
} = require("@budibase/auth/permissions")
const Joi = require("joi")
const { FieldTypes } = require("../../constants")
const router = Router()
function generatePlusDatasourceSchema() {
// prettier-ignore
return joiValidator.body(Joi.object({
_id: Joi.string(),
_rev: Joi.string(),
source: Joi.string().valid("postgres"),
type: Joi.string().valid("datasource_plus"),
relationships: Joi.array().required().items(Joi.object({
from: Joi.string().required(),
to: Joi.string().required(),
cardinality: Joi.valid("1:N", "1:1", "N:N").required()
})),
entities: Joi.array().required().items(Joi.object({
type: Joi.string().valid(...Object.values(FieldTypes)).required(),
name: Joi.string().required(),
})),
}).unknown(true))
}
router
.get("/api/datasources", authorized(BUILDER), datasourceController.fetch)
.get(
@ -16,7 +38,11 @@ router
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
datasourceController.find
)
.post("/api/datasources/plus", datasourceController.plus)
.post(
"/api/datasources/plus",
generatePlusDatasourceSchema(),
datasourceController.plus
)
.post("/api/datasources", authorized(BUILDER), datasourceController.save)
.delete(
"/api/datasources/:datasourceId/:revId",

View File

@ -38,6 +38,7 @@ const DocumentTypes = {
LAYOUT: "layout",
SCREEN: "screen",
DATASOURCE: "datasource",
DATASOURCE_PLUS: "datasource_plus",
QUERY: "query",
}
@ -280,8 +281,10 @@ exports.getWebhookParams = (webhookId = null, otherProps = {}) => {
* Generates a new datasource ID.
* @returns {string} The new datasource ID which the webhook doc can be stored under.
*/
exports.generateDatasourceID = () => {
return `${DocumentTypes.DATASOURCE}${SEPARATOR}${newid()}`
exports.generateDatasourceID = ({ plus = false }) => {
return `${
plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE
}${SEPARATOR}${newid()}`
}
/**