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-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)
|
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-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
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-09-26 05:05:26 +12:00
|
|
|
const instanceId = ctx.user.instanceId
|
|
|
|
const db = new CouchDB(instanceId)
|
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
|
|
|
|
const oldTable = ctx.preExisting
|
2020-05-15 02:12:30 +12:00
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// rename row fields when table column is renamed
|
2020-10-10 06:49:23 +13:00
|
|
|
const { _rename } = tableToSave
|
|
|
|
if (_rename && tableToSave.schema[_rename.updated].type === "link") {
|
2020-10-01 23:49:49 +13:00
|
|
|
throw "Cannot rename a linked field."
|
2020-10-10 06:49:23 +13:00
|
|
|
} else if (_rename && tableToSave.primaryDisplay === _rename.old) {
|
2020-10-01 23:49:49 +13:00
|
|
|
throw "Cannot rename the primary display field."
|
|
|
|
} else if (_rename) {
|
2020-10-10 07:10:28 +13:00
|
|
|
const rows = await db.allDocs(
|
|
|
|
getRowParams(tableToSave._id, null, {
|
2020-10-02 05:22:08 +13:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-10-03 02:49:23 +13:00
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
const docs = rows.rows.map(({ doc }) => {
|
2020-06-26 09:34:38 +12:00
|
|
|
doc[_rename.updated] = doc[_rename.old]
|
|
|
|
delete doc[_rename.old]
|
|
|
|
return doc
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.bulkDocs(docs)
|
2020-10-10 06:49:23 +13:00
|
|
|
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
|
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 06:49:23 +13:00
|
|
|
const result = await db.post(tableToSave)
|
|
|
|
tableToSave._rev = result.rev
|
2020-04-15 02:14:57 +12:00
|
|
|
|
2020-10-10 07:10:28 +13:00
|
|
|
// update linked rows
|
|
|
|
await linkRows.updateLinks({
|
2020-09-29 23:55:18 +13:00
|
|
|
instanceId,
|
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-09-26 05:05:26 +12:00
|
|
|
ctx.eventEmitter &&
|
2020-10-10 06:49:23 +13:00
|
|
|
ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave)
|
2020-06-12 04:24:09 +12:00
|
|
|
|
2020-10-03 04:16:06 +13:00
|
|
|
if (dataImport && dataImport.path) {
|
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-09-26 05:05:26 +12:00
|
|
|
const instanceId = ctx.user.instanceId
|
|
|
|
const db = new CouchDB(instanceId)
|
2020-04-14 04:04:51 +12:00
|
|
|
|
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 06:49:23 +13:00
|
|
|
await db.remove(tableToDelete)
|
2020-06-12 05:11:56 +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-10 07:30:50 +13:00
|
|
|
await db.bulkDocs(rows.rows.map(row => ({ _id: row.id, _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-09-29 23:55:18 +13:00
|
|
|
instanceId,
|
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-09-26 05:05:26 +12:00
|
|
|
ctx.eventEmitter &&
|
2020-10-10 06:49:23 +13:00
|
|
|
ctx.eventEmitter.emitTable(`table:delete`, instanceId, 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) {
|
|
|
|
const { file, schema = {} } = ctx.request.body
|
|
|
|
const result = await csvParser.parse(file.path, schema)
|
|
|
|
ctx.body = {
|
|
|
|
schema: result,
|
|
|
|
path: file.path,
|
|
|
|
}
|
|
|
|
}
|