2021-06-11 19:56:30 +02:00
|
|
|
const CouchDB = require("../../../db")
|
|
|
|
const linkRows = require("../../../db/linkedRows")
|
2020-10-09 10:47:37 +02:00
|
|
|
const {
|
2020-10-09 20:10:28 +02:00
|
|
|
getRowParams,
|
|
|
|
generateRowID,
|
2020-10-09 10:47:37 +02:00
|
|
|
DocumentTypes,
|
|
|
|
SEPARATOR,
|
2021-04-09 16:11:49 +02:00
|
|
|
InternalTables,
|
2021-06-11 19:56:30 +02:00
|
|
|
} = require("../../../db/utils")
|
|
|
|
const userController = require("../user")
|
2021-02-10 17:10:39 +01:00
|
|
|
const {
|
|
|
|
inputProcessing,
|
|
|
|
outputProcessing,
|
2021-06-11 19:56:30 +02:00
|
|
|
} = require("../../../utilities/rowProcessor")
|
|
|
|
const { FieldTypes } = require("../../../constants")
|
2021-02-18 14:38:57 +01:00
|
|
|
const { isEqual } = require("lodash")
|
2021-06-11 19:56:30 +02:00
|
|
|
const { validate, findRow } = require("./utils")
|
2021-06-14 20:05:39 +02:00
|
|
|
const { fullSearch, paginatedSearch } = require("./internalSearch")
|
2020-10-01 18:22:08 +02:00
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}`
|
2020-05-04 18:13:57 +02:00
|
|
|
|
2020-10-15 13:09:41 +02:00
|
|
|
const CALCULATION_TYPES = {
|
|
|
|
SUM: "sum",
|
|
|
|
COUNT: "count",
|
2020-10-15 18:05:09 +02:00
|
|
|
STATS: "stats",
|
2020-10-15 13:09:41 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 19:56:30 +02:00
|
|
|
exports.patch = async ctx => {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2021-05-19 16:55:00 +02:00
|
|
|
const inputs = ctx.request.body
|
|
|
|
const tableId = inputs.tableId
|
|
|
|
const isUserTable = tableId === InternalTables.USER_METADATA
|
|
|
|
let dbRow
|
|
|
|
try {
|
2021-06-14 20:05:39 +02:00
|
|
|
dbRow = await db.get(inputs._id)
|
2021-05-19 16:55:00 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (isUserTable) {
|
|
|
|
// don't include the rev, it'll be the global rev
|
|
|
|
// this time
|
|
|
|
dbRow = {
|
|
|
|
_id: inputs._id,
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-11 19:56:30 +02:00
|
|
|
throw "Row does not exist"
|
2021-05-19 16:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let dbTable = await db.get(tableId)
|
2021-02-05 13:23:29 +01:00
|
|
|
// need to build up full patch fields before coerce
|
2021-05-19 16:55:00 +02:00
|
|
|
for (let key of Object.keys(inputs)) {
|
2021-02-15 18:47:14 +01:00
|
|
|
if (!dbTable.schema[key]) continue
|
2021-05-19 16:55:00 +02:00
|
|
|
dbRow[key] = inputs[key]
|
2020-09-10 10:36:14 +02:00
|
|
|
}
|
2020-09-10 22:11:05 +02:00
|
|
|
|
2021-02-15 18:47:14 +01:00
|
|
|
// this returns the table and row incase they have been updated
|
2021-02-18 14:38:57 +01:00
|
|
|
let { table, row } = inputProcessing(ctx.user, dbTable, dbRow)
|
2020-09-10 10:36:14 +02:00
|
|
|
const validateResult = await validate({
|
2020-10-09 20:10:28 +02:00
|
|
|
row,
|
2020-10-09 19:49:23 +02:00
|
|
|
table,
|
2020-09-10 10:36:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!validateResult.valid) {
|
2021-06-11 19:56:30 +02:00
|
|
|
throw validateResult.errors
|
2020-09-10 10:36:14 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
// returned row is cleaned and prepared for writing to DB
|
|
|
|
row = await linkRows.updateLinks({
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-09 20:10:28 +02:00
|
|
|
eventType: linkRows.EventType.ROW_UPDATE,
|
|
|
|
row,
|
|
|
|
tableId: row.tableId,
|
2020-10-09 19:49:23 +02:00
|
|
|
table,
|
2020-09-29 12:55:18 +02:00
|
|
|
})
|
2020-12-08 18:33:08 +01:00
|
|
|
|
2021-05-19 16:55:00 +02:00
|
|
|
if (isUserTable) {
|
2021-02-05 13:23:29 +01:00
|
|
|
// the row has been updated, need to put it into the ctx
|
2021-05-19 16:55:00 +02:00
|
|
|
ctx.request.body = row
|
2021-04-09 17:55:56 +02:00
|
|
|
await userController.updateMetadata(ctx)
|
2021-06-11 19:56:30 +02:00
|
|
|
return { row: ctx.body, table }
|
2020-12-08 18:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
const response = await db.put(row)
|
2021-02-18 14:38:57 +01:00
|
|
|
// don't worry about rev, tables handle rev/lastID updates
|
|
|
|
if (!isEqual(dbTable, table)) {
|
|
|
|
await db.put(table)
|
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
row._rev = response.rev
|
|
|
|
row.type = "row"
|
2021-06-11 19:56:30 +02:00
|
|
|
return { row, table }
|
2020-09-10 10:36:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.save = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2021-02-15 18:47:14 +01:00
|
|
|
let inputs = ctx.request.body
|
|
|
|
inputs.tableId = ctx.params.tableId
|
2020-04-08 17:57:27 +02:00
|
|
|
|
2021-02-15 18:47:14 +01:00
|
|
|
if (!inputs._rev && !inputs._id) {
|
2021-04-19 17:26:33 +02:00
|
|
|
inputs._id = generateRowID(inputs.tableId)
|
2020-05-14 16:12:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-15 18:47:14 +01:00
|
|
|
// this returns the table and row incase they have been updated
|
2021-02-18 14:38:57 +01:00
|
|
|
const dbTable = await db.get(inputs.tableId)
|
2021-04-16 00:14:10 +02:00
|
|
|
let { table, row } = inputProcessing(ctx.user, dbTable, inputs)
|
2020-05-28 16:39:29 +02:00
|
|
|
const validateResult = await validate({
|
2020-10-09 20:10:28 +02:00
|
|
|
row,
|
2020-10-09 19:49:23 +02:00
|
|
|
table,
|
2020-05-07 11:53:34 +02:00
|
|
|
})
|
2020-04-09 11:13:19 +02:00
|
|
|
|
2020-05-28 16:39:29 +02:00
|
|
|
if (!validateResult.valid) {
|
2021-06-11 19:56:30 +02:00
|
|
|
throw validateResult.errors
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
// make sure link rows are up to date
|
|
|
|
row = await linkRows.updateLinks({
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-14 16:06:48 +02:00
|
|
|
eventType: linkRows.EventType.ROW_SAVE,
|
2020-10-09 20:10:28 +02:00
|
|
|
row,
|
|
|
|
tableId: row.tableId,
|
2020-10-09 19:49:23 +02:00
|
|
|
table,
|
2020-09-30 16:41:52 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
row.type = "row"
|
2020-12-08 18:33:08 +01:00
|
|
|
const response = await db.put(row)
|
2021-02-18 14:38:57 +01:00
|
|
|
// don't worry about rev, tables handle rev/lastID updates
|
|
|
|
if (!isEqual(dbTable, table)) {
|
|
|
|
await db.put(table)
|
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
row._rev = response.rev
|
2021-06-11 19:56:30 +02:00
|
|
|
return { row, table }
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchView = async ctx => {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-01 18:22:08 +02:00
|
|
|
const viewName = ctx.params.viewName
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
// if this is a table view being looked for just transfer to that
|
2020-11-26 15:43:56 +01:00
|
|
|
if (viewName.startsWith(TABLE_VIEW_BEGINS_WITH)) {
|
2020-10-09 19:49:23 +02:00
|
|
|
ctx.params.tableId = viewName.substring(4)
|
2021-06-15 14:03:55 +02:00
|
|
|
return exports.fetch(ctx)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 18:33:08 +01:00
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const { calculation, group, field } = ctx.query
|
2021-02-24 17:55:06 +01:00
|
|
|
const designDoc = await db.get("_design/database")
|
|
|
|
const viewInfo = designDoc.views[viewName]
|
|
|
|
if (!viewInfo) {
|
2021-06-11 19:56:30 +02:00
|
|
|
throw "View does not exist."
|
2021-02-24 17:55:06 +01:00
|
|
|
}
|
2020-10-01 18:22:08 +02:00
|
|
|
const response = await db.query(`database/${viewName}`, {
|
2020-10-15 11:48:57 +02:00
|
|
|
include_docs: !calculation,
|
2021-04-07 14:08:28 +02:00
|
|
|
group: !!group,
|
2020-05-07 11:53:34 +02:00
|
|
|
})
|
2020-08-17 22:01:43 +02:00
|
|
|
|
2021-06-11 19:56:30 +02:00
|
|
|
let rows
|
2020-10-15 11:48:57 +02:00
|
|
|
if (!calculation) {
|
2021-05-04 12:32:22 +02:00
|
|
|
response.rows = response.rows.map(row => row.doc)
|
2021-02-02 15:55:52 +01:00
|
|
|
let table
|
|
|
|
try {
|
2021-02-24 17:55:06 +01:00
|
|
|
table = await db.get(viewInfo.meta.tableId)
|
2021-02-02 15:55:52 +01:00
|
|
|
} catch (err) {
|
2021-03-10 18:55:42 +01:00
|
|
|
/* istanbul ignore next */
|
2021-02-02 15:55:52 +01:00
|
|
|
table = {
|
|
|
|
schema: {},
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
rows = await outputProcessing(appId, table, response.rows)
|
2020-10-15 11:48:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 13:09:41 +02:00
|
|
|
if (calculation === CALCULATION_TYPES.STATS) {
|
2021-05-04 12:32:22 +02:00
|
|
|
response.rows = response.rows.map(row => ({
|
2020-08-24 12:46:28 +02:00
|
|
|
group: row.key,
|
2020-09-02 12:52:32 +02:00
|
|
|
field,
|
2020-08-24 12:46:28 +02:00
|
|
|
...row.value,
|
|
|
|
avg: row.value.sum / row.value.count,
|
|
|
|
}))
|
2021-06-11 19:56:30 +02:00
|
|
|
rows = response.rows
|
2020-08-17 22:01:43 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 13:09:41 +02:00
|
|
|
if (
|
|
|
|
calculation === CALCULATION_TYPES.COUNT ||
|
|
|
|
calculation === CALCULATION_TYPES.SUM
|
|
|
|
) {
|
2021-06-11 19:56:30 +02:00
|
|
|
rows = response.rows.map(row => ({
|
2020-10-15 13:09:41 +02:00
|
|
|
group: row.key,
|
|
|
|
field,
|
|
|
|
value: row.value,
|
|
|
|
}))
|
2020-10-15 11:48:57 +02:00
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
return rows
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:03:55 +02:00
|
|
|
exports.fetch = async ctx => {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2021-02-01 19:08:06 +01:00
|
|
|
const db = new CouchDB(appId)
|
2020-11-26 15:43:56 +01:00
|
|
|
|
2021-02-02 12:46:10 +01:00
|
|
|
let rows,
|
|
|
|
table = await db.get(ctx.params.tableId)
|
2021-04-09 16:11:49 +02:00
|
|
|
if (ctx.params.tableId === InternalTables.USER_METADATA) {
|
2021-04-09 17:55:56 +02:00
|
|
|
await userController.fetchMetadata(ctx)
|
2020-12-08 18:33:08 +01:00
|
|
|
rows = ctx.body
|
|
|
|
} else {
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getRowParams(ctx.params.tableId, null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
rows = response.rows.map(row => row.doc)
|
2020-11-26 15:43:56 +01:00
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
return outputProcessing(appId, table, rows)
|
2020-06-11 15:35:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.find = async ctx => {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2021-06-11 19:56:30 +02:00
|
|
|
const table = await db.get(ctx.params.tableId)
|
|
|
|
let row = await findRow(ctx, db, ctx.params.tableId, ctx.params.rowId)
|
|
|
|
row = await outputProcessing(appId, table, row)
|
|
|
|
return row
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.destroy = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2021-06-14 14:52:06 +02:00
|
|
|
const { _id, _rev } = ctx.request.body
|
|
|
|
const row = await db.get(_id)
|
2021-06-11 19:56:30 +02:00
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
if (row.tableId !== ctx.params.tableId) {
|
2021-06-11 19:56:30 +02:00
|
|
|
throw "Supplied tableId doesn't match the row's tableId"
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
await linkRows.updateLinks({
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-09 20:10:28 +02:00
|
|
|
eventType: linkRows.EventType.ROW_DELETE,
|
|
|
|
row,
|
|
|
|
tableId: row.tableId,
|
2020-09-29 12:55:18 +02:00
|
|
|
})
|
2021-04-09 17:55:56 +02:00
|
|
|
if (ctx.params.tableId === InternalTables.USER_METADATA) {
|
|
|
|
ctx.params = {
|
2021-06-14 14:52:06 +02:00
|
|
|
id: _id,
|
2021-04-09 17:55:56 +02:00
|
|
|
}
|
|
|
|
await userController.destroyMetadata(ctx)
|
2021-06-11 19:56:30 +02:00
|
|
|
return { response: ctx.body, row }
|
2021-04-09 17:55:56 +02:00
|
|
|
} else {
|
2021-06-14 20:07:13 +02:00
|
|
|
const response = await db.remove(_id, _rev)
|
2021-06-11 19:56:30 +02:00
|
|
|
return { response, row }
|
2021-04-09 17:55:56 +02:00
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
2020-09-29 12:55:18 +02:00
|
|
|
|
2021-06-11 19:56:30 +02:00
|
|
|
exports.bulkDestroy = async ctx => {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const { rows } = ctx.request.body
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
|
|
|
|
let updates = rows.map(row =>
|
|
|
|
linkRows.updateLinks({
|
|
|
|
appId,
|
|
|
|
eventType: linkRows.EventType.ROW_DELETE,
|
|
|
|
row,
|
|
|
|
tableId: row.tableId,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
// TODO remove special user case in future
|
|
|
|
if (ctx.params.tableId === InternalTables.USER_METADATA) {
|
|
|
|
updates = updates.concat(
|
|
|
|
rows.map(row => {
|
|
|
|
ctx.params = {
|
|
|
|
id: row._id,
|
|
|
|
}
|
|
|
|
return userController.destroyMetadata(ctx)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
await db.bulkDocs(rows.map(row => ({ ...row, _deleted: true })))
|
|
|
|
}
|
|
|
|
await Promise.all(updates)
|
|
|
|
return { response: { ok: true }, rows }
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-28 16:39:29 +02:00
|
|
|
|
2021-06-14 20:05:39 +02:00
|
|
|
exports.search = async ctx => {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const { tableId } = ctx.params
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const { paginate, query, ...params } = ctx.request.body
|
|
|
|
params.tableId = tableId
|
|
|
|
|
|
|
|
let response
|
|
|
|
if (paginate) {
|
|
|
|
response = await paginatedSearch(appId, query, params)
|
|
|
|
} else {
|
|
|
|
response = await fullSearch(appId, query, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enrich search results with relationships
|
|
|
|
if (response.rows && response.rows.length) {
|
|
|
|
const table = await db.get(tableId)
|
|
|
|
response.rows = await outputProcessing(appId, table, response.rows)
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:27:33 +02:00
|
|
|
return response
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.validate = async ctx => {
|
2021-06-11 19:56:30 +02:00
|
|
|
return validate({
|
2021-03-29 18:32:05 +02:00
|
|
|
appId: ctx.appId,
|
2020-10-09 19:49:23 +02:00
|
|
|
tableId: ctx.params.tableId,
|
2020-10-09 20:10:28 +02:00
|
|
|
row: ctx.request.body,
|
2020-05-28 16:39:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchEnrichedRow = async ctx => {
|
2021-03-29 18:32:05 +02:00
|
|
|
const appId = ctx.appId
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2020-10-09 19:49:23 +02:00
|
|
|
const tableId = ctx.params.tableId
|
2020-10-09 20:10:28 +02:00
|
|
|
const rowId = ctx.params.rowId
|
|
|
|
// need table to work out where links go in row
|
2020-12-09 11:52:18 +01:00
|
|
|
let [table, row] = await Promise.all([
|
|
|
|
db.get(tableId),
|
2021-04-09 17:55:56 +02:00
|
|
|
findRow(ctx, db, tableId, rowId),
|
2020-12-09 11:52:18 +01:00
|
|
|
])
|
2020-09-29 17:40:59 +02:00
|
|
|
// get the link docs
|
2020-10-09 20:10:28 +02:00
|
|
|
const linkVals = await linkRows.getLinkDocuments({
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-09 19:49:23 +02:00
|
|
|
tableId,
|
2020-10-09 20:10:28 +02:00
|
|
|
rowId,
|
2020-09-29 12:55:18 +02:00
|
|
|
})
|
2020-10-09 20:10:28 +02:00
|
|
|
// look up the actual rows based on the ids
|
2020-09-30 13:00:56 +02:00
|
|
|
const response = await db.allDocs({
|
2020-09-29 17:40:59 +02:00
|
|
|
include_docs: true,
|
2021-05-04 12:32:22 +02:00
|
|
|
keys: linkVals.map(linkVal => linkVal.id),
|
2020-09-29 17:40:59 +02:00
|
|
|
})
|
2020-10-09 20:10:28 +02:00
|
|
|
// need to include the IDs in these rows for any links they may have
|
2021-02-10 17:10:39 +01:00
|
|
|
let linkedRows = await outputProcessing(
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2021-02-01 19:08:06 +01:00
|
|
|
table,
|
2021-05-04 12:32:22 +02:00
|
|
|
response.rows.map(row => row.doc)
|
2020-09-30 16:37:38 +02:00
|
|
|
)
|
2020-10-09 20:10:28 +02:00
|
|
|
// insert the link rows in the correct place throughout the main row
|
2020-10-09 19:49:23 +02:00
|
|
|
for (let fieldName of Object.keys(table.schema)) {
|
|
|
|
let field = table.schema[fieldName]
|
2021-02-10 18:55:19 +01:00
|
|
|
if (field.type === FieldTypes.LINK) {
|
2021-02-16 18:21:34 +01:00
|
|
|
// find the links that pertain to this field, get their indexes
|
|
|
|
const linkIndexes = linkVals
|
2021-05-04 12:32:22 +02:00
|
|
|
.filter(link => link.fieldName === fieldName)
|
|
|
|
.map(link => linkVals.indexOf(link))
|
2021-02-16 18:21:34 +01:00
|
|
|
// find the rows that the links state are linked to this field
|
|
|
|
row[fieldName] = linkedRows.filter((linkRow, index) =>
|
|
|
|
linkIndexes.includes(index)
|
2020-09-30 16:37:38 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
return row
|
2020-10-13 17:17:07 +02:00
|
|
|
}
|