2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2020-10-10 07:10:28 +13:00
|
|
|
const linkRows = require("../../db/linkedRows")
|
2020-10-03 04:16:06 +13:00
|
|
|
const csvParser = require("../../utilities/csvParser")
|
2020-10-02 05:22:08 +13:00
|
|
|
const {
|
2020-10-10 07:10:28 +13:00
|
|
|
getRowParams,
|
2020-10-10 06:49:23 +13:00
|
|
|
getTableParams,
|
|
|
|
generateTableID,
|
2020-10-10 07:10:28 +13:00
|
|
|
generateRowID,
|
2020-10-02 05:22:08 +13:00
|
|
|
} = require("../../db/utils")
|
2020-04-09 03:57:27 +12:00
|
|
|
|
2020-11-11 02:59:46 +13:00
|
|
|
async function checkForColumnUpdates(db, oldTable, updatedTable) {
|
|
|
|
let updatedRows
|
|
|
|
const rename = updatedTable._rename
|
2020-11-11 04:09:54 +13:00
|
|
|
let deletedColumns = []
|
|
|
|
if (oldTable && oldTable.schema && updatedTable.schema) {
|
|
|
|
deletedColumns = Object.keys(oldTable.schema).filter(
|
|
|
|
colName => updatedTable.schema[colName] == null
|
|
|
|
)
|
|
|
|
}
|
2020-11-11 02:59:46 +13:00
|
|
|
// check for renaming of columns or deleted columns
|
|
|
|
if (rename || deletedColumns.length !== 0) {
|
|
|
|
const rows = await db.allDocs(
|
|
|
|
getRowParams(updatedTable._id, null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
updatedRows = rows.rows.map(({ doc }) => {
|
|
|
|
if (rename) {
|
|
|
|
doc[rename.updated] = doc[rename.old]
|
|
|
|
delete doc[rename.old]
|
|
|
|
} else if (deletedColumns.length !== 0) {
|
|
|
|
deletedColumns.forEach(colName => delete doc[colName])
|
|
|
|
}
|
|
|
|
return doc
|
|
|
|
})
|
|
|
|
delete updatedTable._rename
|
|
|
|
}
|
|
|
|
return updatedRows
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:47:53 +12:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-10-02 05:22:08 +13:00
|
|
|
const body = await db.allDocs(
|
2020-10-10 06:49:23 +13:00
|
|
|
getTableParams(null, {
|
2020-10-02 05:22:08 +13:00
|
|
|
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-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-09-26 05:05:26 +12:00
|
|
|
ctx.body = await db.get(ctx.params.id)
|
2020-06-04 03:10:03 +12:00
|
|
|
}
|
|
|
|
|
2020-06-24 10:26:54 +12:00
|
|
|
exports.save = async function(ctx) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const appId = ctx.user.appId
|
|
|
|
const db = new CouchDB(appId)
|
2020-10-03 04:16:06 +13:00
|
|
|
const { dataImport, ...rest } = ctx.request.body
|
2020-10-10 06:49:23 +13:00
|
|
|
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
|
|
|
}
|
2020-10-07 07:13:41 +13:00
|
|
|
|
2020-10-10 06:49:23 +13:00
|
|
|
// if the table obj had an _id then it will have been retrieved
|
2020-10-15 08:05:13 +13:00
|
|
|
let oldTable
|
|
|
|
if (ctx.request.body && ctx.request.body._id) {
|
|
|
|
oldTable = await db.get(ctx.request.body._id)
|
|
|
|
}
|
2020-05-15 02:12:30 +12:00
|
|
|
|
2020-10-16 06:09:19 +13:00
|
|
|
// 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 column = tableToSave.schema[propKey]
|
|
|
|
let oldColumn = oldTable.schema[propKey]
|
|
|
|
if (oldColumn && oldColumn.type !== column.type) {
|
|
|
|
ctx.throw(400, "Cannot change the type of a column")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 07:40:27 +13:00
|
|
|
// Don't rename if the name is the same
|
|
|
|
let { _rename } = tableToSave
|
|
|
|
if (_rename && _rename.old === _rename.updated) {
|
|
|
|
_rename = null
|
|
|
|
delete tableToSave._rename
|
|
|
|
}
|
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// rename row fields when table column is renamed
|
2020-10-10 06:49:23 +13:00
|
|
|
if (_rename && tableToSave.schema[_rename.updated].type === "link") {
|
2020-10-16 06:09:19 +13:00
|
|
|
ctx.throw(400, "Cannot rename a linked column.")
|
2020-10-10 06:49:23 +13:00
|
|
|
} else if (_rename && tableToSave.primaryDisplay === _rename.old) {
|
2020-10-16 06:09:19 +13:00
|
|
|
ctx.throw(400, "Cannot rename the display column.")
|
2020-06-26 09:34:38 +12:00
|
|
|
}
|
|
|
|
|
2020-11-11 02:59:46 +13:00
|
|
|
let updatedRows = await checkForColumnUpdates(db, oldTable, tableToSave)
|
|
|
|
|
2020-09-15 02:41:20 +12:00
|
|
|
// update schema of non-statistics views when new columns are added
|
2020-10-10 06:49:23 +13:00
|
|
|
for (let view in tableToSave.views) {
|
|
|
|
const tableView = tableToSave.views[view]
|
|
|
|
if (!tableView) continue
|
2020-09-15 02:40:45 +12:00
|
|
|
|
2020-10-10 06:49:23 +13:00
|
|
|
if (tableView.schema.group || tableView.schema.field) continue
|
|
|
|
tableView.schema = tableToSave.schema
|
2020-09-15 02:40:45 +12:00
|
|
|
}
|
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// update linked rows
|
|
|
|
await linkRows.updateLinks({
|
2020-10-29 23:28:27 +13:00
|
|
|
appId,
|
2020-10-10 06:49:23 +13:00
|
|
|
eventType: oldTable
|
2020-10-10 07:10:28 +13:00
|
|
|
? linkRows.EventType.TABLE_UPDATED
|
|
|
|
: linkRows.EventType.TABLE_SAVE,
|
2020-10-10 06:49:23 +13:00
|
|
|
table: tableToSave,
|
|
|
|
oldTable: oldTable,
|
2020-09-29 23:55:18 +13:00
|
|
|
})
|
2020-04-15 02:14:57 +12:00
|
|
|
|
2020-10-13 05:45:11 +13:00
|
|
|
// don't perform any updates until relationships have been
|
|
|
|
// checked by the updateLinks function
|
2020-11-11 02:59:46 +13:00
|
|
|
if (updatedRows && updatedRows.length !== 0) {
|
|
|
|
await db.bulkDocs(updatedRows)
|
2020-10-13 05:45:11 +13:00
|
|
|
}
|
|
|
|
const result = await db.post(tableToSave)
|
|
|
|
tableToSave._rev = result.rev
|
|
|
|
|
2020-09-26 05:05:26 +12:00
|
|
|
ctx.eventEmitter &&
|
2020-10-29 23:28:27 +13:00
|
|
|
ctx.eventEmitter.emitTable(`table:save`, appId, tableToSave)
|
2020-06-12 04:24:09 +12:00
|
|
|
|
2020-10-20 07:24:05 +13:00
|
|
|
if (dataImport && dataImport.csvString) {
|
2020-10-10 07:10:28 +13:00
|
|
|
// 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) {
|
2020-10-10 07:10:28 +13:00
|
|
|
row._id = generateRowID(tableToSave._id)
|
2020-10-10 06:49:23 +13:00
|
|
|
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
|
2020-10-10 06:49:23 +13:00
|
|
|
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) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const appId = ctx.user.appId
|
|
|
|
const db = new CouchDB(appId)
|
2020-10-10 06:49:23 +13:00
|
|
|
const tableToDelete = await db.get(ctx.params.tableId)
|
2020-06-12 04:24:09 +12:00
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// Delete all rows for that table
|
|
|
|
const rows = await db.allDocs(
|
|
|
|
getRowParams(ctx.params.tableId, null, {
|
2020-10-02 05:22:08 +13:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-10-13 05:56:40 +13:00
|
|
|
await db.bulkDocs(rows.rows.map(row => ({ ...row.doc, _deleted: true })))
|
2020-04-14 04:04:51 +12:00
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// update linked rows
|
|
|
|
await linkRows.updateLinks({
|
2020-10-29 23:28:27 +13:00
|
|
|
appId,
|
2020-10-10 07:10:28 +13:00
|
|
|
eventType: linkRows.EventType.TABLE_DELETE,
|
2020-10-10 06:49:23 +13:00
|
|
|
table: tableToDelete,
|
2020-09-29 23:55:18 +13:00
|
|
|
})
|
2020-04-14 04:04:51 +12:00
|
|
|
|
2020-10-13 05:45:11 +13:00
|
|
|
// don't remove the table itself until very end
|
|
|
|
await db.remove(tableToDelete)
|
|
|
|
|
2020-09-26 05:05:26 +12:00
|
|
|
ctx.eventEmitter &&
|
2020-10-29 23:28:27 +13:00
|
|
|
ctx.eventEmitter.emitTable(`table:delete`, appId, tableToDelete)
|
2020-05-15 02:12:30 +12:00
|
|
|
ctx.status = 200
|
2020-10-10 06:49:23 +13:00
|
|
|
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) {
|
2020-10-20 07:24:05 +13:00
|
|
|
const { csvString, schema = {} } = ctx.request.body
|
|
|
|
const result = await csvParser.parse(csvString, schema)
|
|
|
|
ctx.body = { schema: result }
|
2020-10-03 04:16:06 +13:00
|
|
|
}
|