remove other link fields when you delete a model
This commit is contained in:
parent
0634c5718a
commit
1c1ac8f1a3
|
@ -27,6 +27,23 @@ exports.create = async function(ctx) {
|
||||||
const result = await db.post(newModel)
|
const result = await db.post(newModel)
|
||||||
newModel._rev = result.rev
|
newModel._rev = result.rev
|
||||||
|
|
||||||
|
const { schema } = ctx.request.body
|
||||||
|
for (let key in schema) {
|
||||||
|
// model has a linked record
|
||||||
|
if (schema[key].type === "link") {
|
||||||
|
// create the link field in the other model
|
||||||
|
const linkedModel = await db.get(schema[key].modelId);
|
||||||
|
linkedModel.schema[newModel.name] = {
|
||||||
|
type: "link",
|
||||||
|
modelId: newModel._id,
|
||||||
|
constraints: {
|
||||||
|
type: "array"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await db.put(linkedModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const designDoc = await db.get("_design/database")
|
const designDoc = await db.get("_design/database")
|
||||||
designDoc.views = {
|
designDoc.views = {
|
||||||
...designDoc.views,
|
...designDoc.views,
|
||||||
|
@ -50,7 +67,9 @@ exports.update = async function() {}
|
||||||
exports.destroy = async function(ctx) {
|
exports.destroy = async function(ctx) {
|
||||||
const db = new CouchDB(ctx.params.instanceId)
|
const db = new CouchDB(ctx.params.instanceId)
|
||||||
|
|
||||||
await db.remove(ctx.params.modelId, ctx.params.revId)
|
const modelToDelete = await db.get(ctx.params.modelId);
|
||||||
|
|
||||||
|
await db.remove(modelToDelete)
|
||||||
const modelViewId = `all_${ctx.params.modelId}`
|
const modelViewId = `all_${ctx.params.modelId}`
|
||||||
|
|
||||||
// Delete all records for that model
|
// Delete all records for that model
|
||||||
|
@ -59,6 +78,17 @@ exports.destroy = async function(ctx) {
|
||||||
records.rows.map(record => ({ id: record.id, _deleted: true }))
|
records.rows.map(record => ({ id: record.id, _deleted: true }))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Delete linked record fields in dependent models
|
||||||
|
for (let key in modelToDelete.schema) {
|
||||||
|
const { type, modelId } = modelToDelete.schema[key];
|
||||||
|
if (type === "link") {
|
||||||
|
const linkedModel = await db.get(modelId);
|
||||||
|
delete linkedModel.schema[modelToDelete.name]
|
||||||
|
await db.put(linkedModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// delete the "all" view
|
// delete the "all" view
|
||||||
const designDoc = await db.get("_design/database")
|
const designDoc = await db.get("_design/database")
|
||||||
delete designDoc.views[modelViewId]
|
delete designDoc.views[modelViewId]
|
||||||
|
|
|
@ -253,3 +253,7 @@ exports.insertDocument = async (databaseId, document) => {
|
||||||
exports.destroyDocument = async (databaseId, documentId) => {
|
exports.destroyDocument = async (databaseId, documentId) => {
|
||||||
return await new CouchDB(databaseId).destroy(documentId)
|
return await new CouchDB(databaseId).destroy(documentId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.getDocument = async (databaseId, documentId) => {
|
||||||
|
return await new CouchDB(databaseId).get(documentId)
|
||||||
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@ const {
|
||||||
createModel,
|
createModel,
|
||||||
supertest,
|
supertest,
|
||||||
createClientDatabase,
|
createClientDatabase,
|
||||||
createApplication ,
|
createApplication,
|
||||||
defaultHeaders,
|
defaultHeaders,
|
||||||
builderEndpointShouldBlockNormalUsers
|
builderEndpointShouldBlockNormalUsers,
|
||||||
|
getDocument
|
||||||
} = require("./couchTestUtils")
|
} = require("./couchTestUtils")
|
||||||
|
|
||||||
describe("/models", () => {
|
describe("/models", () => {
|
||||||
|
@ -119,6 +120,41 @@ describe("/models", () => {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("deletes linked references to the model after deletion", async done => {
|
||||||
|
const linkedModel = await createModel(request, instance._id, {
|
||||||
|
name: "LinkedModel",
|
||||||
|
type: "model",
|
||||||
|
key: "name",
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
type: "text",
|
||||||
|
constraints: {
|
||||||
|
type: "string",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
TestModel: {
|
||||||
|
type: "link",
|
||||||
|
modelId: testModel._id,
|
||||||
|
constraints: {
|
||||||
|
type: "array"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
request
|
||||||
|
.delete(`/api/${instance._id}/models/${testModel._id}/${testModel._rev}`)
|
||||||
|
.set(defaultHeaders)
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(200)
|
||||||
|
.end(async (_, res) => {
|
||||||
|
expect(res.res.statusMessage).toEqual(`Model ${testModel._id} deleted.`);
|
||||||
|
const dependentModel = await getDocument(instance._id, linkedModel._id)
|
||||||
|
expect(dependentModel.schema.TestModel).not.toBeDefined();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
it("should apply authorization to endpoint", async () => {
|
it("should apply authorization to endpoint", async () => {
|
||||||
await builderEndpointShouldBlockNormalUsers({
|
await builderEndpointShouldBlockNormalUsers({
|
||||||
request,
|
request,
|
||||||
|
|
Loading…
Reference in New Issue