1
0
Fork 0
mirror of synced 2024-10-01 01:28:51 +13:00
budibase/packages/server/src/api/controllers/table.js

145 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const linkRows = require("../../db/linkedRows")
2020-10-03 04:16:06 +13:00
const csvParser = require("../../utilities/csvParser")
const {
getRowParams,
getTableParams,
generateTableID,
generateRowID,
} = require("../../db/utils")
2020-04-09 03:57:27 +12:00
2020-04-13 22:47:53 +12:00
exports.fetch = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
const body = await db.allDocs(
getTableParams(null, {
include_docs: true,
})
)
2020-05-07 21:53:34 +12:00
ctx.body = body.rows.map(row => row.doc)
2020-04-13 22:47:53 +12:00
}
2020-06-04 03:10:03 +12:00
exports.find = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
ctx.body = await db.get(ctx.params.id)
2020-06-04 03:10:03 +12:00
}
exports.save = async function(ctx) {
const instanceId = ctx.user.instanceId
const db = new CouchDB(instanceId)
2020-10-03 04:16:06 +13:00
const { dataImport, ...rest } = ctx.request.body
const tableToSave = {
type: "table",
_id: generateTableID(),
2020-08-19 04:14:26 +12:00
views: {},
2020-10-02 02:48:07 +13:00
...rest,
2020-05-15 02:12:30 +12:00
}
let renameDocs = []
// if the table obj had an _id then it will have been retrieved
const oldTable = ctx.preExisting
2020-05-15 02:12:30 +12:00
// rename row fields when table column is renamed
const { _rename } = tableToSave
if (_rename && tableToSave.schema[_rename.updated].type === "link") {
throw "Cannot rename a linked field."
} else if (_rename && tableToSave.primaryDisplay === _rename.old) {
throw "Cannot rename the primary display field."
} else if (_rename) {
const rows = await db.allDocs(
getRowParams(tableToSave._id, null, {
include_docs: true,
})
)
renameDocs = rows.rows.map(({ doc }) => {
2020-06-26 09:34:38 +12:00
doc[_rename.updated] = doc[_rename.old]
delete doc[_rename.old]
return doc
})
delete tableToSave._rename
2020-06-26 09:34:38 +12:00
}
2020-09-15 02:41:20 +12:00
// 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
2020-09-15 02:40:45 +12:00
if (tableView.schema.group || tableView.schema.field) continue
tableView.schema = tableToSave.schema
2020-09-15 02:40:45 +12:00
}
// update linked rows
await linkRows.updateLinks({
instanceId,
eventType: oldTable
? linkRows.EventType.TABLE_UPDATED
: linkRows.EventType.TABLE_SAVE,
table: tableToSave,
oldTable: oldTable,
})
// don't perform any updates until relationships have been
// checked by the updateLinks function
if (renameDocs.length !== 0) {
await db.bulkDocs(renameDocs)
}
const result = await db.post(tableToSave)
tableToSave._rev = result.rev
ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave)
2020-10-03 04:16:06 +13:00
if (dataImport && dataImport.path) {
// Populate the table with rows imported from CSV in a bulk update
2020-10-03 04:16:06 +13:00
const data = await csvParser.transform(dataImport)
2020-10-06 07:21:51 +13:00
for (let row of data) {
row._id = generateRowID(tableToSave._id)
row.tableId = tableToSave._id
2020-10-06 07:21:51 +13:00
}
2020-10-03 04:16:06 +13:00
await db.bulkDocs(data)
2020-10-02 02:48:07 +13:00
}
2020-05-15 02:12:30 +12:00
ctx.status = 200
ctx.message = `Table ${ctx.request.body.name} saved successfully.`
ctx.body = tableToSave
2020-04-09 03:57:27 +12:00
}
2020-04-10 03:53:48 +12:00
exports.destroy = async function(ctx) {
const instanceId = ctx.user.instanceId
const db = new CouchDB(instanceId)
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,
})
)
2020-10-13 05:56:40 +13:00
await db.bulkDocs(rows.rows.map(row => ({ ...row.doc, _deleted: true })))
// update linked rows
await linkRows.updateLinks({
instanceId,
eventType: linkRows.EventType.TABLE_DELETE,
table: tableToDelete,
})
// don't remove the table itself until very end
await db.remove(tableToDelete)
ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:delete`, instanceId, tableToDelete)
2020-05-15 02:12:30 +12:00
ctx.status = 200
ctx.message = `Table ${ctx.params.tableId} deleted.`
2020-04-10 03:53:48 +12:00
}
2020-10-03 04:16:06 +13:00
exports.validateCSVSchema = async function(ctx) {
const { file, schema = {} } = ctx.request.body
const result = await csvParser.parse(file.path, schema)
ctx.body = {
schema: result,
path: file.path,
}
}