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-05-19 03:22:09 +12:00
|
|
|
const newid = require("../../db/newid")
|
2020-05-05 04:13:57 +12:00
|
|
|
|
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-05-19 03:22:09 +12:00
|
|
|
record._id = newid()
|
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
|
|
|
|
|
|
|
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-06-26 07:04:58 +12:00
|
|
|
[model.name]: doc[model.name]
|
|
|
|
? [...doc[model.name], record._id]
|
2020-06-24 03:20:06 +12:00
|
|
|
: [record._id],
|
2020-06-23 08:30:23 +12:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.bulkDocs(linkedDocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:08:53 +12:00
|
|
|
ctx.eventEmitter &&
|
|
|
|
ctx.eventEmitter.emit(`record:save`, {
|
2020-08-06 02:18:28 +12:00
|
|
|
args: {
|
|
|
|
record,
|
|
|
|
},
|
2020-06-19 03:59:31 +12:00
|
|
|
instanceId: ctx.user.instanceId,
|
2020-06-02 22:08:53 +12:00
|
|
|
})
|
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-05-07 21:53:34 +12:00
|
|
|
const response = await db.query(`database/${ctx.params.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-05-28 04:23:01 +12:00
|
|
|
const response = await db.query(`database/all_${ctx.params.modelId}`, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
ctx.throw(400, "Supplied modelId doe not match the record's modelId")
|
|
|
|
return
|
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId)
|
2020-06-02 22:08:53 +12:00
|
|
|
ctx.eventEmitter && ctx.eventEmitter.emit(`record:delete`, 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) {
|
|
|
|
const res = validateJs.single(
|
|
|
|
record[fieldName],
|
|
|
|
model.schema[fieldName].constraints
|
|
|
|
)
|
|
|
|
if (res) errors[fieldName] = res
|
|
|
|
}
|
|
|
|
return { valid: Object.keys(errors).length === 0, errors }
|
|
|
|
}
|