Resolving issues with deleting a linked column, should now be possible.

This commit is contained in:
Michael Drury 2020-10-01 11:33:37 +01:00
parent 7aa040d314
commit 3c8c5461d7
2 changed files with 19 additions and 20 deletions

View File

@ -38,12 +38,13 @@ function LinkDocument(
}
class LinkController {
constructor({ instanceId, modelId, record, model }) {
constructor({ instanceId, modelId, record, model, oldModel }) {
this._instanceId = instanceId
this._db = new CouchDB(instanceId)
this._modelId = modelId
this._record = record
this._model = model
this._oldModel = oldModel
}
/**
@ -192,12 +193,12 @@ class LinkController {
* @returns {Promise<void>} The model has now been updated.
*/
async removeFieldFromModel(fieldName) {
let model = await this.model()
let field = model.schema[fieldName]
let oldModel = this._oldModel
let field = oldModel.schema[fieldName]
const linkDocs = await this.getModelLinkDocs(IncludeDocs.INCLUDE)
let toDelete = linkDocs.filter(linkDoc => {
let correctFieldName =
linkDoc.doc1.modelId === model._id
linkDoc.doc1.modelId === oldModel._id
? linkDoc.doc1.fieldName
: linkDoc.doc2.fieldName
return correctFieldName === fieldName
@ -211,8 +212,8 @@ class LinkController {
})
)
// remove schema from other model
let linkedModel = this._db.get(field.modelId)
delete linkedModel[field.fieldName]
let linkedModel = await this._db.get(field.modelId)
delete linkedModel.schema[field.fieldName]
this._db.put(linkedModel)
}
@ -246,10 +247,10 @@ class LinkController {
/**
* Update a model, this means if a field is removed need to handle removing from other table and removing
* any link docs that pertained to it.
* @param {object} oldModel The model before it was updated which can be used for differencing.
* @returns {Promise<Object>} The model which has been saved, same response as with the modelSaved function.
*/
async modelUpdated(oldModel) {
async modelUpdated() {
const oldModel = this._oldModel
// first start by checking if any link columns have been deleted
const newModel = await this.model()
for (let fieldName of Object.keys(oldModel.schema)) {

View File

@ -33,24 +33,22 @@ exports.createLinkView = createLinkView
* @returns {Promise<object>} When the update is complete this will respond successfully. Returns the record for
* record operations and the model for model operations.
*/
exports.updateLinks = async ({
exports.updateLinks = async function({
eventType,
instanceId,
record,
modelId,
model,
oldModel,
}) => {
// make sure model ID is set
if (model != null) {
modelId = model._id
}) {
if (instanceId == null) {
throw "Cannot operate without an instance ID."
}
let linkController = new LinkController({
instanceId,
modelId,
model,
record,
})
// make sure model ID is set
if (modelId == null && model != null) {
arguments[0].modelId = model._id
}
let linkController = new LinkController(arguments[0])
if (
!(await linkController.doesModelHaveLinkedFields()) &&
(oldModel == null ||
@ -67,7 +65,7 @@ exports.updateLinks = async ({
case EventType.MODEL_SAVE:
return await linkController.modelSaved()
case EventType.MODEL_UPDATED:
return await linkController.modelUpdated(oldModel)
return await linkController.modelUpdated()
case EventType.MODEL_DELETE:
return await linkController.modelDeleted()
default: