|
|
@ -18,16 +18,16 @@ function emitEvent(eventType, ctx, record) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
validateJs.extend(validateJs.validators.datetime, { |
|
|
validateJs.extend(validateJs.validators.datetime, { |
|
|
parse: function(value) { |
|
|
parse: function (value) { |
|
|
return new Date(value).getTime() |
|
|
return new Date(value).getTime() |
|
|
}, |
|
|
}, |
|
|
// Input is a unix timestamp
|
|
|
// Input is a unix timestamp
|
|
|
format: function(value) { |
|
|
format: function (value) { |
|
|
return new Date(value).toISOString() |
|
|
return new Date(value).toISOString() |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
exports.patch = async function(ctx) { |
|
|
exports.patch = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const record = await db.get(ctx.params.id) |
|
|
const record = await db.get(ctx.params.id) |
|
|
const model = await db.get(record.modelId) |
|
|
const model = await db.get(record.modelId) |
|
|
@ -60,77 +60,15 @@ exports.patch = async function(ctx) { |
|
|
ctx.message = `${model.name} updated successfully.` |
|
|
ctx.message = `${model.name} updated successfully.` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.save = async function(ctx) { |
|
|
exports.save = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
if (ctx.request.body.type === 'delete') { |
|
|
const record = ctx.request.body |
|
|
await bulkDelete(ctx) |
|
|
record.modelId = ctx.params.modelId |
|
|
} else { |
|
|
|
|
|
await saveRecords(ctx) |
|
|
if (!record._rev && !record._id) { |
|
|
|
|
|
record._id = newid() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const model = await db.get(record.modelId) |
|
|
|
|
|
|
|
|
|
|
|
const validateResult = await validate({ |
|
|
|
|
|
record, |
|
|
|
|
|
model, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if (!validateResult.valid) { |
|
|
|
|
|
ctx.status = 400 |
|
|
|
|
|
ctx.body = { |
|
|
|
|
|
status: 400, |
|
|
|
|
|
errors: validateResult.errors, |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const existingRecord = record._rev && (await db.get(record._id)) |
|
|
|
|
|
|
|
|
|
|
|
if (existingRecord) { |
|
|
|
|
|
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.` |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
record.type = "record" |
|
|
|
|
|
const response = await db.post(record) |
|
|
|
|
|
record._rev = response.rev |
|
|
|
|
|
|
|
|
|
|
|
// create links in other tables
|
|
|
|
|
|
for (let key in record) { |
|
|
|
|
|
if (model.schema[key] && model.schema[key].type === "link") { |
|
|
|
|
|
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, |
|
|
|
|
|
[model.name]: doc[model.name] |
|
|
|
|
|
? [...doc[model.name], record._id] |
|
|
|
|
|
: [record._id], |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
await db.bulkDocs(linkedDocs) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
emitEvent(`record:save`, ctx, record) |
|
|
|
|
|
ctx.body = record |
|
|
|
|
|
ctx.status = 200 |
|
|
|
|
|
ctx.message = `${model.name} created successfully` |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.fetchView = async function(ctx) { |
|
|
exports.fetchView = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const { stats, group, field } = ctx.query |
|
|
const { stats, group, field } = ctx.query |
|
|
const response = await db.query(`database/${ctx.params.viewName}`, { |
|
|
const response = await db.query(`database/${ctx.params.viewName}`, { |
|
|
@ -152,7 +90,7 @@ exports.fetchView = async function(ctx) { |
|
|
ctx.body = response.rows |
|
|
ctx.body = response.rows |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.fetchModelRecords = async function(ctx) { |
|
|
exports.fetchModelRecords = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const response = await db.query(`database/all_${ctx.params.modelId}`, { |
|
|
const response = await db.query(`database/all_${ctx.params.modelId}`, { |
|
|
include_docs: true, |
|
|
include_docs: true, |
|
|
@ -160,7 +98,7 @@ exports.fetchModelRecords = async function(ctx) { |
|
|
ctx.body = response.rows.map(row => row.doc) |
|
|
ctx.body = response.rows.map(row => row.doc) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.search = async function(ctx) { |
|
|
exports.search = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const response = await db.allDocs({ |
|
|
const response = await db.allDocs({ |
|
|
include_docs: true, |
|
|
include_docs: true, |
|
|
@ -169,7 +107,7 @@ exports.search = async function(ctx) { |
|
|
ctx.body = response.rows.map(row => row.doc) |
|
|
ctx.body = response.rows.map(row => row.doc) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.find = async function(ctx) { |
|
|
exports.find = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const record = await db.get(ctx.params.recordId) |
|
|
const record = await db.get(ctx.params.recordId) |
|
|
if (record.modelId !== ctx.params.modelId) { |
|
|
if (record.modelId !== ctx.params.modelId) { |
|
|
@ -179,7 +117,7 @@ exports.find = async function(ctx) { |
|
|
ctx.body = record |
|
|
ctx.body = record |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) { |
|
|
exports.destroy = async function (ctx) { |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
const record = await db.get(ctx.params.recordId) |
|
|
const record = await db.get(ctx.params.recordId) |
|
|
if (record.modelId !== ctx.params.modelId) { |
|
|
if (record.modelId !== ctx.params.modelId) { |
|
|
@ -193,7 +131,7 @@ exports.destroy = async function(ctx) { |
|
|
emitEvent(`record:delete`, ctx, record) |
|
|
emitEvent(`record:delete`, ctx, record) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.validate = async function(ctx) { |
|
|
exports.validate = async function (ctx) { |
|
|
const errors = await validate({ |
|
|
const errors = await validate({ |
|
|
instanceId: ctx.user.instanceId, |
|
|
instanceId: ctx.user.instanceId, |
|
|
modelId: ctx.params.modelId, |
|
|
modelId: ctx.params.modelId, |
|
|
@ -218,3 +156,95 @@ async function validate({ instanceId, modelId, record, model }) { |
|
|
} |
|
|
} |
|
|
return { valid: Object.keys(errors).length === 0, errors } |
|
|
return { valid: Object.keys(errors).length === 0, errors } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function bulkDelete(ctx) { |
|
|
|
|
|
const { records } = ctx.request.body |
|
|
|
|
|
console.log(records) |
|
|
|
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
|
|
|
|
|
|
|
|
|
await db.bulkDocs( |
|
|
|
|
|
records.map(record => ({ ...record, _deleted: true })), console.log) |
|
|
|
|
|
// await db.bulkDocs(
|
|
|
|
|
|
// records.rows.map(record => ({ _id: record.id, _deleted: true }))
|
|
|
|
|
|
// )
|
|
|
|
|
|
// const record = await db.get(ctx.params.recordId)
|
|
|
|
|
|
// if (record.modelId !== ctx.params.modelId) {
|
|
|
|
|
|
// ctx.throw(400, "Supplied modelId doesn't match the record's modelId")
|
|
|
|
|
|
// return
|
|
|
|
|
|
// }
|
|
|
|
|
|
// ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId)
|
|
|
|
|
|
ctx.status = 200 |
|
|
|
|
|
// // for automations
|
|
|
|
|
|
// ctx.record = record
|
|
|
|
|
|
// emitEvent(`record:delete`, ctx, record)
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function saveRecords(ctx) { |
|
|
|
|
|
const db = new CouchDB(ctx.user.instanceId) |
|
|
|
|
|
const record = ctx.request.body |
|
|
|
|
|
record.modelId = ctx.params.modelId |
|
|
|
|
|
|
|
|
|
|
|
if (!record._rev && !record._id) { |
|
|
|
|
|
record._id = newid() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const model = await db.get(record.modelId) |
|
|
|
|
|
|
|
|
|
|
|
const validateResult = await validate({ |
|
|
|
|
|
record, |
|
|
|
|
|
model, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if (!validateResult.valid) { |
|
|
|
|
|
ctx.status = 400 |
|
|
|
|
|
ctx.body = { |
|
|
|
|
|
status: 400, |
|
|
|
|
|
errors: validateResult.errors, |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const existingRecord = record._rev && (await db.get(record._id)) |
|
|
|
|
|
|
|
|
|
|
|
if (existingRecord) { |
|
|
|
|
|
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.` |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
record.type = "record" |
|
|
|
|
|
const response = await db.post(record) |
|
|
|
|
|
record._rev = response.rev |
|
|
|
|
|
|
|
|
|
|
|
// create links in other tables
|
|
|
|
|
|
for (let key in record) { |
|
|
|
|
|
if (model.schema[key] && model.schema[key].type === "link") { |
|
|
|
|
|
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, |
|
|
|
|
|
[model.name]: doc[model.name] |
|
|
|
|
|
? [...doc[model.name], record._id] |
|
|
|
|
|
: [record._id], |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
await db.bulkDocs(linkedDocs) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
emitEvent(`record:save`, ctx, record) |
|
|
|
|
|
ctx.body = record |
|
|
|
|
|
ctx.status = 200 |
|
|
|
|
|
ctx.message = `${model.name} created successfully` |
|
|
|
|
|
} |