Joi schema validation for plus datasources
This commit is contained in:
parent
51e2346389
commit
7ecdb60cc6
|
@ -6,7 +6,6 @@ const {
|
||||||
} = require("../../db/utils")
|
} = require("../../db/utils")
|
||||||
const { integrations } = require("../../integrations")
|
const { integrations } = require("../../integrations")
|
||||||
const plusIntegrations = require("../../integrations/plus")
|
const plusIntegrations = require("../../integrations/plus")
|
||||||
const PostgresConnector = require("../../integrations/plus/Postgres")
|
|
||||||
|
|
||||||
exports.fetch = async function (ctx) {
|
exports.fetch = async function (ctx) {
|
||||||
const database = new CouchDB(ctx.appId)
|
const database = new CouchDB(ctx.appId)
|
||||||
|
@ -65,7 +64,7 @@ exports.find = async function (ctx) {
|
||||||
|
|
||||||
// TODO: merge endpoint with main datasource endpoint
|
// TODO: merge endpoint with main datasource endpoint
|
||||||
exports.plus = async function (ctx) {
|
exports.plus = async function (ctx) {
|
||||||
const db = new CouchDB(ctx.appId)
|
const db = new CouchDB("testapp123")
|
||||||
|
|
||||||
const PlusConnector = plusIntegrations[ctx.request.body.source]
|
const PlusConnector = plusIntegrations[ctx.request.body.source]
|
||||||
|
|
||||||
|
@ -73,11 +72,11 @@ exports.plus = async function (ctx) {
|
||||||
await connector.init()
|
await connector.init()
|
||||||
|
|
||||||
const datasource = {
|
const datasource = {
|
||||||
_id: generateDatasourceID(),
|
_id: generateDatasourceID({ plus: true }),
|
||||||
type: "datasource:plus",
|
type: "datasource_plus",
|
||||||
|
relationships: [],
|
||||||
...ctx.request.body,
|
...ctx.request.body,
|
||||||
entities: connector.tables,
|
entities: connector.tables,
|
||||||
relationships: {},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await db.post(datasource)
|
const response = await db.post(datasource)
|
||||||
|
|
|
@ -1,14 +1,36 @@
|
||||||
const Router = require("@koa/router")
|
const Router = require("@koa/router")
|
||||||
const datasourceController = require("../controllers/datasource")
|
const datasourceController = require("../controllers/datasource")
|
||||||
const authorized = require("../../middleware/authorized")
|
const authorized = require("../../middleware/authorized")
|
||||||
|
const joiValidator = require("../../middleware/joi-validator")
|
||||||
const {
|
const {
|
||||||
BUILDER,
|
BUILDER,
|
||||||
PermissionLevels,
|
PermissionLevels,
|
||||||
PermissionTypes,
|
PermissionTypes,
|
||||||
} = require("@budibase/auth/permissions")
|
} = require("@budibase/auth/permissions")
|
||||||
|
const Joi = require("joi")
|
||||||
|
const { FieldTypes } = require("../../constants")
|
||||||
|
|
||||||
const router = Router()
|
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
|
router
|
||||||
.get("/api/datasources", authorized(BUILDER), datasourceController.fetch)
|
.get("/api/datasources", authorized(BUILDER), datasourceController.fetch)
|
||||||
.get(
|
.get(
|
||||||
|
@ -16,7 +38,11 @@ router
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
|
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
|
||||||
datasourceController.find
|
datasourceController.find
|
||||||
)
|
)
|
||||||
.post("/api/datasources/plus", datasourceController.plus)
|
.post(
|
||||||
|
"/api/datasources/plus",
|
||||||
|
generatePlusDatasourceSchema(),
|
||||||
|
datasourceController.plus
|
||||||
|
)
|
||||||
.post("/api/datasources", authorized(BUILDER), datasourceController.save)
|
.post("/api/datasources", authorized(BUILDER), datasourceController.save)
|
||||||
.delete(
|
.delete(
|
||||||
"/api/datasources/:datasourceId/:revId",
|
"/api/datasources/:datasourceId/:revId",
|
||||||
|
|
|
@ -38,6 +38,7 @@ const DocumentTypes = {
|
||||||
LAYOUT: "layout",
|
LAYOUT: "layout",
|
||||||
SCREEN: "screen",
|
SCREEN: "screen",
|
||||||
DATASOURCE: "datasource",
|
DATASOURCE: "datasource",
|
||||||
|
DATASOURCE_PLUS: "datasource_plus",
|
||||||
QUERY: "query",
|
QUERY: "query",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,8 +281,10 @@ exports.getWebhookParams = (webhookId = null, otherProps = {}) => {
|
||||||
* Generates a new datasource ID.
|
* Generates a new datasource ID.
|
||||||
* @returns {string} The new datasource ID which the webhook doc can be stored under.
|
* @returns {string} The new datasource ID which the webhook doc can be stored under.
|
||||||
*/
|
*/
|
||||||
exports.generateDatasourceID = () => {
|
exports.generateDatasourceID = ({ plus = false }) => {
|
||||||
return `${DocumentTypes.DATASOURCE}${SEPARATOR}${newid()}`
|
return `${
|
||||||
|
plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE
|
||||||
|
}${SEPARATOR}${newid()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue