account for model and view cleanup after model deletion
This commit is contained in:
parent
6d47e0df89
commit
4a77960e76
|
@ -61,7 +61,21 @@ exports.update = async function(ctx) {
|
|||
|
||||
exports.destroy = async function(ctx) {
|
||||
const db = couchdb.db.use(ctx.params.instanceId)
|
||||
|
||||
const model = await db.destroy(ctx.params.modelId, ctx.params.revId);
|
||||
const modelViewId = `all_${model.id}`
|
||||
|
||||
// Delete all records for that model
|
||||
const records = await db.view("database", modelViewId);
|
||||
await db.bulk({
|
||||
docs: records.rows.map(record => ({ id: record.id, _deleted: true }))
|
||||
});
|
||||
|
||||
// delete the "all" view
|
||||
const designDoc = await db.get("_design/database");
|
||||
delete designDoc.views[modelViewId];
|
||||
await db.insert(designDoc, designDoc._id);
|
||||
|
||||
ctx.body = {
|
||||
message: `Model ${model.id} deleted.`,
|
||||
status: 200
|
||||
|
|
|
@ -16,7 +16,20 @@ exports.createModel = async instanceId => {
|
|||
}
|
||||
]
|
||||
}
|
||||
const response = await couchdb.db.use(instanceId).insert(model);
|
||||
const db = couchdb.db.use(instanceId);
|
||||
const response = await db.insert(model);
|
||||
|
||||
const designDoc = await db.get("_design/database");
|
||||
designDoc.views = {
|
||||
...designDoc.views,
|
||||
[`all_${response.id}`]: {
|
||||
map: function(doc) {
|
||||
emit([doc.modelId], doc._id);
|
||||
}
|
||||
}
|
||||
};
|
||||
await db.insert(designDoc, designDoc._id);
|
||||
|
||||
return {
|
||||
...response,
|
||||
...model
|
||||
|
|
|
@ -93,7 +93,7 @@ describe("/models", () => {
|
|||
await destroyDatabase(TEST_INSTANCE_ID);
|
||||
});
|
||||
|
||||
it("returns all the models for that instance in the response body", done => {
|
||||
it("returns a success response when a model is deleted.", done => {
|
||||
request
|
||||
.delete(`/api/${TEST_INSTANCE_ID}/models/${testModel.id}/${testModel.rev}`)
|
||||
.set("Accept", "application/json")
|
||||
|
|
Loading…
Reference in New Issue