1
0
Fork 0
mirror of synced 2024-06-29 19:41:03 +12:00

account for model and view cleanup after model deletion

This commit is contained in:
Martin McKeaveney 2020-04-13 17:04:51 +01:00
parent 6d47e0df89
commit 4a77960e76
3 changed files with 29 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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")