From ceeac9b982aebdcc036ac86c691e64f832630599 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 7 Sep 2020 13:41:17 -0700 Subject: [PATCH] fix: Deeply nested document titles not updated in collection documentStructure cache closes #1508 --- server/models/Collection.js | 16 +++++++++++----- server/models/Collection.test.js | 10 +++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/server/models/Collection.js b/server/models/Collection.js index aae4f7a7fd..bfd67ed436 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -304,7 +304,12 @@ Collection.prototype.updateDocument = async function ( }; this.documentStructure = updateChildren(this.documentStructure); - await this.save({ transaction }); + + // Sequelize doesn't seem to set the value with splice on JSONB field + // https://github.com/sequelize/sequelize/blob/e1446837196c07b8ff0c23359b958d68af40fd6d/src/model.js#L3937 + this.changed("documentStructure", true); + + await this.save({ fields: ["documentStructure"], transaction }); await transaction.commit(); } catch (err) { if (transaction) { @@ -357,10 +362,11 @@ Collection.prototype.removeDocumentInStructure = async function ( document.id ); - await this.save({ - ...options, - transaction, - }); + // Sequelize doesn't seem to set the value with splice on JSONB field + // https://github.com/sequelize/sequelize/blob/e1446837196c07b8ff0c23359b958d68af40fd6d/src/model.js#L3937 + this.changed("documentStructure", true); + + await this.save({ ...options, fields: ["documentStructure"], transaction }); await transaction.commit(); } catch (err) { if (transaction) { diff --git a/server/models/Collection.test.js b/server/models/Collection.test.js index cad4831990..17e8663595 100644 --- a/server/models/Collection.test.js +++ b/server/models/Collection.test.js @@ -144,7 +144,9 @@ describe("#updateDocument", () => { await collection.updateDocument(newDocument); - expect(collection.documentStructure[0].children[0].title).toBe( + const reloaded = await Collection.findByPk(collection.id); + + expect(reloaded.documentStructure[0].children[0].title).toBe( "Updated title" ); }); @@ -224,8 +226,10 @@ describe("#removeDocument", () => { // Remove the document await collection.deleteDocument(newDocument); - expect(collection.documentStructure.length).toBe(1); - expect(collection.documentStructure[0].children.length).toBe(0); + const reloaded = await Collection.findByPk(collection.id); + + expect(reloaded.documentStructure.length).toBe(1); + expect(reloaded.documentStructure[0].children.length).toBe(0); const collectionDocuments = await Document.findAndCountAll({ where: {