Move document improvements (#927)

* Show all collections in UI

* Introduce command pattern

* Actually remove from previous collection

* Stash

* Fixes: Promises resolved outside of response lifecycle

* 💚

* 💚

* documentMover tests

* Transaction

* Perf. More in transactions
This commit is contained in:
Tom Moor
2019-04-08 21:25:13 -07:00
committed by GitHub
parent 16066c0b24
commit 763f57a3dc
16 changed files with 313 additions and 146 deletions

View File

@ -156,8 +156,12 @@ Collection.prototype.addDocumentToStructure = async function(
) {
if (!this.documentStructure) return;
let unlock;
// documentStructure can only be updated by one request at a time
const unlock = await asyncLock(`collection-${this.id}`);
if (options.save !== false) {
unlock = await asyncLock(`collection-${this.id}`);
}
// If moving existing document with children, use existing structure
const documentJson = {
@ -195,8 +199,11 @@ Collection.prototype.addDocumentToStructure = async function(
// Sequelize doesn't seem to set the value with splice on JSONB field
this.documentStructure = this.documentStructure;
await this.save(options);
unlock();
if (options.save !== false) {
await this.save(options);
if (unlock) unlock();
}
return this;
};
@ -234,34 +241,21 @@ Collection.prototype.updateDocument = async function(
return this;
};
/**
* moveDocument is combination of removing the document from the structure
* and placing it back the the new location with the existing children.
*/
Collection.prototype.moveDocument = async function(document, index) {
if (!this.documentStructure) return;
const documentJson = await this.removeDocumentInStructure(document);
await this.addDocumentToStructure(document, index, { documentJson });
};
Collection.prototype.deleteDocument = async function(document) {
await this.removeDocumentInStructure(document, { save: true });
await this.removeDocumentInStructure(document);
await document.deleteWithChildren();
};
Collection.prototype.removeDocumentInStructure = async function(
document,
options?: { save?: boolean }
options
) {
if (!this.documentStructure) return;
let returnValue;
let unlock;
if (options && options.save) {
// documentStructure can only be updated by one request at the time
unlock = await asyncLock(`collection-${this.id}`);
}
// documentStructure can only be updated by one request at the time
unlock = await asyncLock(`collection-${this.id}`);
const removeFromChildren = async (children, id) => {
children = await Promise.all(
@ -287,10 +281,8 @@ Collection.prototype.removeDocumentInStructure = async function(
document.id
);
if (options && options.save) {
await this.save(options);
if (unlock) await unlock();
}
await this.save(options);
if (unlock) await unlock();
return returnValue;
};