2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const Ajv = require("ajv")
|
2020-05-19 03:22:09 +12:00
|
|
|
const newid = require("../../db/newid")
|
2020-05-05 04:13:57 +12:00
|
|
|
|
2020-05-07 21:53:34 +12:00
|
|
|
const ajv = new Ajv()
|
2020-04-08 02:12:08 +12:00
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.save = async function(ctx) {
|
2020-05-07 21:53:34 +12:00
|
|
|
const db = new CouchDB(ctx.params.instanceId)
|
|
|
|
const record = ctx.request.body
|
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-21 03:17:11 +12:00
|
|
|
// validation with ajv
|
2020-04-23 03:35:20 +12:00
|
|
|
const model = await db.get(record.modelId)
|
2020-05-05 04:13:57 +12:00
|
|
|
const validate = ajv.compile({
|
2020-05-07 21:53:34 +12:00
|
|
|
properties: model.schema,
|
|
|
|
})
|
2020-04-23 03:35:20 +12:00
|
|
|
const valid = validate(record)
|
2020-04-09 21:13:19 +12:00
|
|
|
|
2020-04-21 03:17:11 +12:00
|
|
|
if (!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-07 21:53:34 +12:00
|
|
|
errors: validate.errors,
|
|
|
|
}
|
|
|
|
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-05-25 09:54:08 +12:00
|
|
|
ctx.eventPublisher.emit("RECORD_CREATED", record)
|
2020-04-21 03:17:11 +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-04-21 03:17:11 +12:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-04-25 04:28:32 +12:00
|
|
|
const db = new CouchDB(ctx.params.instanceId)
|
2020-05-07 21:53:34 +12:00
|
|
|
const response = await db.query(`database/${ctx.params.viewName}`, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.find = async function(ctx) {
|
2020-04-25 04:28:32 +12:00
|
|
|
const db = new CouchDB(ctx.params.instanceId)
|
2020-04-23 03:35:20 +12:00
|
|
|
ctx.body = await db.get(ctx.params.recordId)
|
2020-04-09 21:13:19 +12:00
|
|
|
}
|
|
|
|
|
2020-04-10 03:53:48 +12:00
|
|
|
exports.destroy = async function(ctx) {
|
2020-05-07 21:53:34 +12:00
|
|
|
const databaseId = ctx.params.instanceId
|
2020-04-25 04:28:32 +12:00
|
|
|
const db = new CouchDB(databaseId)
|
2020-05-07 21:53:34 +12:00
|
|
|
ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId)
|
|
|
|
}
|