2020-04-23 15:51:35 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-04-22 17:35:20 +02:00
|
|
|
const { schemaValidator } = require("@budibase/common")
|
2020-04-07 16:12:08 +02:00
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.save = async function(ctx) {
|
2020-04-23 15:37:08 +02:00
|
|
|
const db = new CouchDB(ctx.params.instanceId);
|
2020-04-20 17:17:11 +02:00
|
|
|
const record = ctx.request.body;
|
2020-04-08 17:57:27 +02:00
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
// validation with ajv
|
2020-04-22 17:35:20 +02:00
|
|
|
const model = await db.get(record.modelId)
|
|
|
|
const validate = schemaValidator.compile(model.schema)
|
|
|
|
const valid = validate(record)
|
2020-04-09 11:13:19 +02:00
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
if (!valid) {
|
2020-04-22 17:35:20 +02:00
|
|
|
ctx.status = 400
|
2020-04-20 17:17:11 +02:00
|
|
|
ctx.body = {
|
|
|
|
status: 400,
|
|
|
|
errors: validate.errors
|
|
|
|
};
|
|
|
|
return;
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
const existingRecord = record._id && await db.get(record._id);
|
|
|
|
|
|
|
|
if (existingRecord) {
|
2020-04-23 15:37:08 +02:00
|
|
|
const response = await db.put({ ...record, _id: existingRecord._id });
|
2020-04-20 17:17:11 +02:00
|
|
|
ctx.body = {
|
|
|
|
message: "Record updated successfully.",
|
|
|
|
status: 200,
|
|
|
|
record: response
|
|
|
|
}
|
2020-04-22 17:35:20 +02:00
|
|
|
return
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:35:20 +02:00
|
|
|
record.type = "record"
|
2020-04-23 15:42:26 +02:00
|
|
|
const response = await db.post(record)
|
2020-04-22 17:35:20 +02:00
|
|
|
record._rev = response.rev
|
2020-04-20 17:17:11 +02:00
|
|
|
// await ctx.publish(events.recordApi.save.onRecordCreated, {
|
|
|
|
// record: record,
|
|
|
|
// })
|
|
|
|
|
2020-04-22 17:35:20 +02:00
|
|
|
ctx.body = record
|
|
|
|
ctx.status = 200
|
2020-04-23 15:42:26 +02:00
|
|
|
ctx.message = `${model.name} ${record._rev ? "updated" : "created"} successfully`
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-04-24 10:54:20 +02:00
|
|
|
const db = new CouchDB(ctx.config)(ctx.params.instanceId)
|
2020-04-23 15:37:08 +02:00
|
|
|
const response = await db.query(
|
|
|
|
`database/${ctx.params.viewName}`,
|
2020-04-09 17:42:55 +02:00
|
|
|
{
|
2020-04-20 17:17:11 +02:00
|
|
|
include_docs: true
|
2020-04-09 17:42:55 +02:00
|
|
|
}
|
|
|
|
)
|
2020-04-20 17:17:11 +02:00
|
|
|
ctx.body = response.rows.map(row => row.doc);
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.find = async function(ctx) {
|
2020-04-24 10:54:20 +02:00
|
|
|
const db = new CouchDB(ctx.config)(ctx.params.instanceId)
|
2020-04-22 17:35:20 +02:00
|
|
|
ctx.body = await db.get(ctx.params.recordId)
|
2020-04-09 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:53:48 +02:00
|
|
|
exports.destroy = async function(ctx) {
|
2020-04-20 17:17:11 +02:00
|
|
|
const databaseId = ctx.params.instanceId;
|
2020-04-24 10:54:20 +02:00
|
|
|
const db = new CouchDB(ctx.config)(databaseId)
|
2020-04-20 17:17:11 +02:00
|
|
|
ctx.body = await db.destroy(ctx.params.recordId, ctx.params.revId);
|
2020-04-23 15:42:26 +02:00
|
|
|
};
|