2020-10-10 06:49:23 +13:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const tableController = require("../controllers/table")
|
|
|
|
const authorized = require("../../middleware/authorized")
|
2021-02-09 06:22:07 +13:00
|
|
|
const { paramResource, bodyResource } = require("../../middleware/resourceId")
|
2020-11-12 06:34:15 +13:00
|
|
|
const {
|
|
|
|
BUILDER,
|
|
|
|
PermissionLevels,
|
|
|
|
PermissionTypes,
|
2021-05-15 02:43:41 +12:00
|
|
|
} = require("@budibase/auth/permissions")
|
2021-02-11 05:10:39 +13:00
|
|
|
const joiValidator = require("../../middleware/joi-validator")
|
|
|
|
const Joi = require("joi")
|
2020-10-10 06:49:23 +13:00
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
2021-02-11 05:10:39 +13:00
|
|
|
function generateSaveValidator() {
|
|
|
|
// prettier-ignore
|
|
|
|
return joiValidator.body(Joi.object({
|
|
|
|
_id: Joi.string(),
|
|
|
|
_rev: Joi.string(),
|
2021-06-19 05:05:31 +12:00
|
|
|
type: Joi.string().valid("table", "internal", "external"),
|
2021-02-11 05:10:39 +13:00
|
|
|
primaryDisplay: Joi.string(),
|
|
|
|
schema: Joi.object().required(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
views: Joi.object(),
|
|
|
|
dataImport: Joi.object(),
|
|
|
|
}).unknown(true))
|
|
|
|
}
|
|
|
|
|
2020-10-10 06:49:23 +13:00
|
|
|
router
|
|
|
|
.get("/api/tables", authorized(BUILDER), tableController.fetch)
|
|
|
|
.get(
|
|
|
|
"/api/tables/:id",
|
2021-02-09 06:22:07 +13:00
|
|
|
paramResource("id"),
|
2020-11-12 06:34:15 +13:00
|
|
|
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
|
2020-10-10 06:49:23 +13:00
|
|
|
tableController.find
|
|
|
|
)
|
2021-02-09 06:22:07 +13:00
|
|
|
.post(
|
|
|
|
"/api/tables",
|
|
|
|
// allows control over updating a table
|
|
|
|
bodyResource("_id"),
|
|
|
|
authorized(BUILDER),
|
2021-02-11 05:10:39 +13:00
|
|
|
generateSaveValidator(),
|
2021-02-09 06:22:07 +13:00
|
|
|
tableController.save
|
|
|
|
)
|
2020-10-10 06:49:23 +13:00
|
|
|
.post(
|
|
|
|
"/api/tables/csv/validate",
|
|
|
|
authorized(BUILDER),
|
|
|
|
tableController.validateCSVSchema
|
|
|
|
)
|
|
|
|
.delete(
|
|
|
|
"/api/tables/:tableId/:revId",
|
2021-02-09 06:22:07 +13:00
|
|
|
paramResource("tableId"),
|
2020-10-10 06:49:23 +13:00
|
|
|
authorized(BUILDER),
|
|
|
|
tableController.destroy
|
|
|
|
)
|
2021-11-13 07:26:57 +13:00
|
|
|
// this is currently builder only, but in the future
|
|
|
|
// it could be carried out by an end user in app,
|
|
|
|
// however some thought will need to be had about
|
|
|
|
// implications for automations (triggers)
|
|
|
|
// new trigger type, bulk rows created
|
|
|
|
.post(
|
|
|
|
"/api/tables/:tableId/import",
|
|
|
|
paramResource("tableId"),
|
|
|
|
authorized(BUILDER),
|
|
|
|
tableController.bulkImport
|
|
|
|
)
|
2020-10-10 06:49:23 +13:00
|
|
|
|
|
|
|
module.exports = router
|