2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2020-05-29 02:39:29 +12:00
|
|
|
const validateJs = require("validate.js")
|
2020-10-02 05:22:08 +13:00
|
|
|
const { getRecordParams, generateRecordID } = require("../../db/utils")
|
|
|
|
|
|
|
|
const MODEL_VIEW_BEGINS_WITH = "all_model:"
|
2020-05-05 04:13:57 +12:00
|
|
|
|
2020-09-14 21:30:35 +12:00
|
|
|
function emitEvent(eventType, ctx, record) {
|
2020-09-23 23:29:20 +12:00
|
|
|
let event = {
|
|
|
|
record,
|
|
|
|
instanceId: ctx.user.instanceId,
|
|
|
|
}
|
2020-09-18 03:16:05 +12:00
|
|
|
// add syntactic sugar for mustache later
|
|
|
|
if (record._id) {
|
2020-09-23 23:29:20 +12:00
|
|
|
event.id = record._id
|
2020-09-18 03:16:05 +12:00
|
|
|
}
|
|
|
|
if (record._rev) {
|
2020-09-23 23:29:20 +12:00
|
|
|
event.revision = record._rev
|
2020-09-18 03:16:05 +12:00
|
|
|
}
|
2020-09-23 23:29:20 +12:00
|
|
|
ctx.eventEmitter && ctx.eventEmitter.emit(eventType, event)
|
2020-09-14 21:30:35 +12:00
|
|
|
}
|
|
|
|
|
2020-09-09 05:03:41 +12:00
|
|
|
validateJs.extend(validateJs.validators.datetime, {
|
2020-09-10 03:27:46 +12:00
|
|
|
parse: function(value) {
|
2020-09-09 05:03:41 +12:00
|
|
|
return new Date(value).getTime()
|
|
|
|
},
|
|
|
|
// Input is a unix timestamp
|
2020-09-10 03:27:46 +12:00
|
|
|
format: function(value) {
|
2020-09-09 05:03:41 +12:00
|
|
|
return new Date(value).toISOString()
|
2020-09-10 03:27:46 +12:00
|
|
|
},
|
|
|
|
})
|
2020-09-09 05:03:41 +12:00
|
|
|
|
2020-09-10 20:36:14 +12:00
|
|
|
exports.patch = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-11 08:11:05 +12:00
|
|
|
const record = await db.get(ctx.params.id)
|
2020-09-10 20:36:14 +12:00
|
|
|
const model = await db.get(record.modelId)
|
|
|
|
const patchfields = ctx.request.body
|
|
|
|
|
2020-10-06 05:28:23 +13:00
|
|
|
coerceRecordValues(record, model)
|
2020-10-03 02:14:58 +13:00
|
|
|
|
2020-09-10 20:36:14 +12:00
|
|
|
for (let key in patchfields) {
|
|
|
|
if (!model.schema[key]) continue
|
|
|
|
record[key] = patchfields[key]
|
|
|
|
}
|
2020-09-11 08:11:05 +12:00
|
|
|
|
2020-09-10 20:36:14 +12:00
|
|
|
const validateResult = await validate({
|
|
|
|
record,
|
|
|
|
model,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!validateResult.valid) {
|
|
|
|
ctx.status = 400
|
|
|
|
ctx.body = {
|
|
|
|
status: 400,
|
|
|
|
errors: validateResult.errors,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await db.put(record)
|
|
|
|
record._rev = response.rev
|
|
|
|
record.type = "record"
|
|
|
|
ctx.body = record
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `${model.name} updated successfully.`
|
|
|
|
}
|
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.save = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-07 21:53:34 +12:00
|
|
|
const record = ctx.request.body
|
2020-05-28 04:23:01 +12:00
|
|
|
record.modelId = ctx.params.modelId
|
2020-04-09 03:57:27 +12:00
|
|
|
|
2020-05-15 02:12:30 +12:00
|
|
|
if (!record._rev && !record._id) {
|
2020-10-03 00:37:46 +13:00
|
|
|
record._id = generateRecordID(record.modelId)
|
2020-05-15 02:12:30 +12:00
|
|
|
}
|
|
|
|
|
2020-04-23 03:35:20 +12:00
|
|
|
const model = await db.get(record.modelId)
|
2020-05-29 02:39:29 +12:00
|
|
|
|
2020-10-06 05:28:23 +13:00
|
|
|
coerceRecordValues(record, model)
|
2020-10-03 02:14:58 +13:00
|
|
|
|
2020-05-29 02:39:29 +12:00
|
|
|
const validateResult = await validate({
|
|
|
|
record,
|
|
|
|
model,
|
2020-05-07 21:53:34 +12:00
|
|
|
})
|
2020-04-09 21:13:19 +12:00
|
|
|
|
2020-05-29 02:39:29 +12:00
|
|
|
if (!validateResult.valid) {
|
2020-04-23 03:35:20 +12:00
|
|
|
ctx.status = 400
|
2020-04-21 03:17:11 +12:00
|
|
|
ctx.body = {
|
|
|
|
status: 400,
|
2020-05-29 02:39:29 +12:00
|
|
|
errors: validateResult.errors,
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
|
|
|
return
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-05-15 02:12:30 +12:00
|
|
|
const existingRecord = record._rev && (await db.get(record._id))
|
2020-04-21 03:17:11 +12:00
|
|
|
|
|
|
|
if (existingRecord) {
|
2020-05-15 02:12:30 +12:00
|
|
|
const response = await db.put(record)
|
|
|
|
record._rev = response.rev
|
|
|
|
record.type = "record"
|
|
|
|
ctx.body = record
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `${model.name} updated successfully.`
|
2020-04-23 03:35:20 +12:00
|
|
|
return
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-04-23 03:35:20 +12:00
|
|
|
record.type = "record"
|
2020-04-24 01:42:26 +12:00
|
|
|
const response = await db.post(record)
|
2020-04-23 03:35:20 +12:00
|
|
|
record._rev = response.rev
|
2020-06-01 04:12:52 +12:00
|
|
|
|
2020-06-23 08:30:23 +12:00
|
|
|
// create links in other tables
|
|
|
|
for (let key in record) {
|
2020-06-25 04:26:14 +12:00
|
|
|
if (model.schema[key] && model.schema[key].type === "link") {
|
2020-06-23 08:30:23 +12:00
|
|
|
const linked = await db.allDocs({
|
|
|
|
include_docs: true,
|
|
|
|
keys: record[key],
|
|
|
|
})
|
|
|
|
|
|
|
|
// add this record to the linked records in attached models
|
|
|
|
const linkedDocs = linked.rows.map(row => {
|
|
|
|
const doc = row.doc
|
|
|
|
return {
|
|
|
|
...doc,
|
2020-09-15 01:35:03 +12:00
|
|
|
[model.name]: doc[model.name]
|
|
|
|
? [...doc[model.name], record._id]
|
|
|
|
: [record._id],
|
2020-06-23 08:30:23 +12:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.bulkDocs(linkedDocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 21:30:35 +12:00
|
|
|
emitEvent(`record:save`, ctx, record)
|
2020-05-15 02:12:30 +12:00
|
|
|
ctx.body = record
|
2020-04-23 03:35:20 +12:00
|
|
|
ctx.status = 200
|
2020-04-25 05:02:51 +12:00
|
|
|
ctx.message = `${model.name} created successfully`
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-05-28 04:23:01 +12:00
|
|
|
exports.fetchView = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-02 22:52:32 +12:00
|
|
|
const { stats, group, field } = ctx.query
|
2020-10-02 05:22:08 +13:00
|
|
|
const viewName = ctx.params.viewName
|
|
|
|
|
|
|
|
// if this is a model view being looked for just transfer to that
|
|
|
|
if (viewName.indexOf(MODEL_VIEW_BEGINS_WITH) === 0) {
|
|
|
|
ctx.params.modelId = viewName.substring(4)
|
|
|
|
await exports.fetchModelRecords(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await db.query(`database/${viewName}`, {
|
2020-08-18 08:01:43 +12:00
|
|
|
include_docs: !stats,
|
2020-08-19 04:14:26 +12:00
|
|
|
group,
|
2020-05-07 21:53:34 +12:00
|
|
|
})
|
2020-08-18 08:01:43 +12:00
|
|
|
|
|
|
|
if (stats) {
|
2020-08-24 22:46:28 +12:00
|
|
|
response.rows = response.rows.map(row => ({
|
|
|
|
group: row.key,
|
2020-09-02 22:52:32 +12:00
|
|
|
field,
|
2020-08-24 22:46:28 +12:00
|
|
|
...row.value,
|
|
|
|
avg: row.value.sum / row.value.count,
|
|
|
|
}))
|
2020-08-18 08:01:43 +12:00
|
|
|
} else {
|
|
|
|
response.rows = response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = response.rows
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-06-12 01:35:45 +12:00
|
|
|
exports.fetchModelRecords = 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 response = await db.allDocs(
|
|
|
|
getRecordParams(ctx.params.modelId, null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-05-28 04:23:01 +12:00
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
2020-06-12 01:35:45 +12:00
|
|
|
exports.search = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-06-12 01:35:45 +12:00
|
|
|
const response = await db.allDocs({
|
|
|
|
include_docs: true,
|
|
|
|
...ctx.request.body,
|
|
|
|
})
|
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.find = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-28 04:23:01 +12:00
|
|
|
const record = await db.get(ctx.params.recordId)
|
|
|
|
if (record.modelId !== ctx.params.modelId) {
|
2020-06-24 03:20:06 +12:00
|
|
|
ctx.throw(400, "Supplied modelId does not match the records modelId")
|
2020-05-28 04:23:01 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.body = record
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.destroy = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-28 04:23:01 +12:00
|
|
|
const record = await db.get(ctx.params.recordId)
|
|
|
|
if (record.modelId !== ctx.params.modelId) {
|
2020-09-18 03:28:48 +12:00
|
|
|
ctx.throw(400, "Supplied modelId doesn't match the record's modelId")
|
2020-05-28 04:23:01 +12:00
|
|
|
return
|
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId)
|
2020-09-18 00:36:19 +12:00
|
|
|
ctx.status = 200
|
2020-09-22 00:49:34 +12:00
|
|
|
// for automations
|
2020-09-18 03:28:48 +12:00
|
|
|
ctx.record = record
|
2020-09-14 21:30:35 +12:00
|
|
|
emitEvent(`record:delete`, ctx, record)
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-05-29 02:39:29 +12:00
|
|
|
|
|
|
|
exports.validate = async function(ctx) {
|
|
|
|
const errors = await validate({
|
2020-06-19 03:59:31 +12:00
|
|
|
instanceId: ctx.user.instanceId,
|
2020-05-29 02:39:29 +12:00
|
|
|
modelId: ctx.params.modelId,
|
|
|
|
record: ctx.request.body,
|
|
|
|
})
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = errors
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validate({ instanceId, modelId, record, model }) {
|
|
|
|
if (!model) {
|
|
|
|
const db = new CouchDB(instanceId)
|
|
|
|
model = await db.get(modelId)
|
|
|
|
}
|
|
|
|
const errors = {}
|
|
|
|
for (let fieldName in model.schema) {
|
2020-09-15 01:35:03 +12:00
|
|
|
const res = validateJs.single(
|
|
|
|
record[fieldName],
|
|
|
|
model.schema[fieldName].constraints
|
|
|
|
)
|
2020-05-29 02:39:29 +12:00
|
|
|
if (res) errors[fieldName] = res
|
|
|
|
}
|
|
|
|
return { valid: Object.keys(errors).length === 0, errors }
|
|
|
|
}
|
2020-10-03 02:14:58 +13:00
|
|
|
|
2020-10-06 05:28:23 +13:00
|
|
|
function coerceRecordValues(record, model) {
|
2020-10-03 02:14:58 +13:00
|
|
|
for (let [key, value] of Object.entries(record)) {
|
|
|
|
const field = model.schema[key]
|
|
|
|
if (!field) continue
|
|
|
|
const mapping = Object.prototype.hasOwnProperty.call(
|
2020-10-06 05:28:23 +13:00
|
|
|
TYPE_TRANSFORM_MAP[field.type],
|
2020-10-03 02:14:58 +13:00
|
|
|
value
|
|
|
|
)
|
|
|
|
? TYPE_TRANSFORM_MAP[field.type][value]
|
|
|
|
: TYPE_TRANSFORM_MAP[field.type].parse
|
|
|
|
|
|
|
|
record[key] = typeof mapping === "function" ? mapping(value) : mapping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const TYPE_TRANSFORM_MAP = {
|
|
|
|
string: {
|
|
|
|
"": "",
|
|
|
|
[null]: "",
|
|
|
|
[undefined]: undefined,
|
2020-10-06 05:28:23 +13:00
|
|
|
parse: s => s,
|
2020-10-03 02:14:58 +13:00
|
|
|
},
|
|
|
|
number: {
|
|
|
|
"": null,
|
2020-10-06 05:28:23 +13:00
|
|
|
[null]: null,
|
2020-10-03 02:14:58 +13:00
|
|
|
[undefined]: undefined,
|
|
|
|
parse: n => parseFloat(n),
|
|
|
|
},
|
|
|
|
datetime: {
|
|
|
|
"": null,
|
|
|
|
[undefined]: undefined,
|
|
|
|
[null]: null,
|
2020-10-06 05:28:23 +13:00
|
|
|
parse: d => d,
|
2020-10-03 02:14:58 +13:00
|
|
|
},
|
2020-10-06 05:28:23 +13:00
|
|
|
attachment: {
|
|
|
|
"": [],
|
2020-10-03 02:14:58 +13:00
|
|
|
[null]: [],
|
|
|
|
[undefined]: undefined,
|
|
|
|
parse: a => a,
|
|
|
|
},
|
|
|
|
boolean: {
|
2020-10-06 05:28:23 +13:00
|
|
|
"": null,
|
2020-10-03 02:14:58 +13:00
|
|
|
[null]: null,
|
|
|
|
[undefined]: undefined,
|
|
|
|
parse: b => {
|
|
|
|
if (b === "true") return true
|
2020-10-06 05:28:23 +13:00
|
|
|
if (b === "false") return false
|
2020-10-03 02:14:58 +13:00
|
|
|
return b
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|