2021-10-28 20:39:42 +02:00
|
|
|
const linkRows = require("../../../db/linkedRows")
|
|
|
|
const { getRowParams, generateTableID } = require("../../../db/utils")
|
|
|
|
const { FieldTypes } = require("../../../constants")
|
2021-11-12 19:26:57 +01:00
|
|
|
const {
|
|
|
|
TableSaveFunctions,
|
|
|
|
hasTypeChanged,
|
|
|
|
getTable,
|
|
|
|
handleDataImport,
|
|
|
|
} = require("./utils")
|
2022-03-08 15:21:41 +01:00
|
|
|
const { quotas, StaticQuotaName, QuotaUsageType } = require("@budibase/pro")
|
2022-01-28 01:05:39 +01:00
|
|
|
const { getAppDB } = require("@budibase/backend-core/context")
|
2022-01-28 19:52:34 +01:00
|
|
|
const env = require("../../../environment")
|
2022-01-24 17:32:41 +01:00
|
|
|
const { cleanupAttachments } = require("../../../utilities/rowProcessor")
|
2022-01-24 19:22:59 +01:00
|
|
|
const { runStaticFormulaChecks } = require("./bulkFormula")
|
2021-10-28 20:39:42 +02:00
|
|
|
|
|
|
|
exports.save = async function (ctx) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
2021-10-28 20:39:42 +02:00
|
|
|
const { dataImport, ...rest } = ctx.request.body
|
|
|
|
let tableToSave = {
|
|
|
|
type: "table",
|
|
|
|
_id: generateTableID(),
|
|
|
|
views: {},
|
|
|
|
...rest,
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the table obj had an _id then it will have been retrieved
|
|
|
|
let oldTable
|
|
|
|
if (ctx.request.body && ctx.request.body._id) {
|
|
|
|
oldTable = await db.get(ctx.request.body._id)
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:01:44 +01:00
|
|
|
if (hasTypeChanged(tableToSave, oldTable)) {
|
|
|
|
ctx.throw(400, "A column type has changed.")
|
|
|
|
}
|
|
|
|
|
2021-10-28 20:39:42 +02:00
|
|
|
// saving a table is a complex operation, involving many different steps, this
|
|
|
|
// has been broken out into a utility to make it more obvious/easier to manipulate
|
|
|
|
const tableSaveFunctions = new TableSaveFunctions({
|
2022-01-27 19:18:31 +01:00
|
|
|
user: ctx.user,
|
2021-10-28 20:39:42 +02:00
|
|
|
oldTable,
|
|
|
|
dataImport,
|
|
|
|
})
|
|
|
|
tableToSave = await tableSaveFunctions.before(tableToSave)
|
|
|
|
|
|
|
|
// make sure that types don't change of a column, have to remove
|
|
|
|
// the column if you want to change the type
|
|
|
|
if (oldTable && oldTable.schema) {
|
|
|
|
for (let propKey of Object.keys(tableToSave.schema)) {
|
|
|
|
let oldColumn = oldTable.schema[propKey]
|
|
|
|
if (oldColumn && oldColumn.type === "internal") {
|
|
|
|
oldColumn.type = "auto"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't rename if the name is the same
|
|
|
|
let { _rename } = tableToSave
|
|
|
|
/* istanbul ignore next */
|
|
|
|
if (_rename && _rename.old === _rename.updated) {
|
|
|
|
_rename = null
|
|
|
|
delete tableToSave._rename
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename row fields when table column is renamed
|
|
|
|
/* istanbul ignore next */
|
|
|
|
if (_rename && tableToSave.schema[_rename.updated].type === FieldTypes.LINK) {
|
|
|
|
ctx.throw(400, "Cannot rename a linked column.")
|
|
|
|
}
|
|
|
|
|
|
|
|
tableToSave = await tableSaveFunctions.mid(tableToSave)
|
|
|
|
|
|
|
|
// update schema of non-statistics views when new columns are added
|
|
|
|
for (let view in tableToSave.views) {
|
|
|
|
const tableView = tableToSave.views[view]
|
|
|
|
if (!tableView) continue
|
|
|
|
|
|
|
|
if (tableView.schema.group || tableView.schema.field) continue
|
|
|
|
tableView.schema = tableToSave.schema
|
|
|
|
}
|
|
|
|
|
|
|
|
// update linked rows
|
|
|
|
try {
|
|
|
|
const linkResp = await linkRows.updateLinks({
|
|
|
|
eventType: oldTable
|
|
|
|
? linkRows.EventType.TABLE_UPDATED
|
|
|
|
: linkRows.EventType.TABLE_SAVE,
|
|
|
|
table: tableToSave,
|
|
|
|
oldTable: oldTable,
|
|
|
|
})
|
|
|
|
if (linkResp != null && linkResp._rev) {
|
|
|
|
tableToSave._rev = linkResp._rev
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(400, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't perform any updates until relationships have been
|
|
|
|
// checked by the updateLinks function
|
|
|
|
const updatedRows = tableSaveFunctions.getUpdatedRows()
|
|
|
|
if (updatedRows && updatedRows.length !== 0) {
|
|
|
|
await db.bulkDocs(updatedRows)
|
|
|
|
}
|
|
|
|
const result = await db.put(tableToSave)
|
|
|
|
tableToSave._rev = result.rev
|
|
|
|
|
|
|
|
tableToSave = await tableSaveFunctions.after(tableToSave)
|
2022-01-21 17:24:24 +01:00
|
|
|
// has to run after, make sure it has _id
|
2022-01-31 18:00:22 +01:00
|
|
|
await runStaticFormulaChecks(tableToSave, { oldTable })
|
2021-10-28 20:39:42 +02:00
|
|
|
return tableToSave
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async function (ctx) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
2021-10-28 20:39:42 +02:00
|
|
|
const tableToDelete = await db.get(ctx.params.tableId)
|
|
|
|
|
|
|
|
// Delete all rows for that table
|
|
|
|
const rows = await db.allDocs(
|
|
|
|
getRowParams(ctx.params.tableId, null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
await db.bulkDocs(rows.rows.map(row => ({ ...row.doc, _deleted: true })))
|
2022-03-08 15:21:41 +01:00
|
|
|
await quotas.updateUsage(
|
|
|
|
-rows.rows.length,
|
|
|
|
StaticQuotaName.ROWS,
|
|
|
|
QuotaUsageType.STATIC
|
|
|
|
)
|
2021-10-28 20:39:42 +02:00
|
|
|
|
|
|
|
// update linked rows
|
|
|
|
await linkRows.updateLinks({
|
|
|
|
eventType: linkRows.EventType.TABLE_DELETE,
|
|
|
|
table: tableToDelete,
|
|
|
|
})
|
|
|
|
|
|
|
|
// don't remove the table itself until very end
|
|
|
|
await db.remove(tableToDelete)
|
|
|
|
|
|
|
|
// remove table search index
|
2022-01-28 19:52:34 +01:00
|
|
|
if (!env.isTest()) {
|
|
|
|
const currentIndexes = await db.getIndexes()
|
|
|
|
const existingIndex = currentIndexes.indexes.find(
|
|
|
|
existing => existing.name === `search:${ctx.params.tableId}`
|
|
|
|
)
|
|
|
|
if (existingIndex) {
|
|
|
|
await db.deleteIndex(existingIndex)
|
|
|
|
}
|
2021-10-28 20:39:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 18:45:24 +01:00
|
|
|
// has to run after, make sure it has _id
|
2022-01-31 18:00:22 +01:00
|
|
|
await runStaticFormulaChecks(tableToDelete, { deletion: true })
|
|
|
|
await cleanupAttachments(tableToDelete, { rows })
|
2021-10-28 20:39:42 +02:00
|
|
|
return tableToDelete
|
|
|
|
}
|
2021-11-12 19:26:57 +01:00
|
|
|
|
|
|
|
exports.bulkImport = async function (ctx) {
|
2022-01-27 19:18:31 +01:00
|
|
|
const table = await getTable(ctx.params.tableId)
|
2021-11-12 19:26:57 +01:00
|
|
|
const { dataImport } = ctx.request.body
|
2022-01-27 19:18:31 +01:00
|
|
|
await handleDataImport(ctx.user, table, dataImport)
|
2021-11-12 19:39:33 +01:00
|
|
|
return table
|
2021-11-12 19:26:57 +01:00
|
|
|
}
|