From dd69d5480bc20bc6915a90cfb3909217d55837c5 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Fri, 4 Jun 2021 10:50:02 +0100 Subject: [PATCH] Joi schema validation for plus datasources --- .../server/src/api/controllers/datasource.js | 9 +++--- packages/server/src/api/routes/datasource.js | 28 ++++++++++++++++++- packages/server/src/db/utils.js | 7 +++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 346a15ab89..43510d092e 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -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) diff --git a/packages/server/src/api/routes/datasource.js b/packages/server/src/api/routes/datasource.js index 791118b336..928fdcfca9 100644 --- a/packages/server/src/api/routes/datasource.js +++ b/packages/server/src/api/routes/datasource.js @@ -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", diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index 29617a7dff..43a6056b9f 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -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()}` } /**