Merge pull request #123 from jorilallo/jori/speedup-saving

Improve concurrency and reduce one DB call from document saving
This commit is contained in:
Jori Lallo
2017-07-09 20:39:56 -07:00
committed by GitHub

View File

@@ -209,6 +209,7 @@ router.post('documents.update', auth(), async ctx => {
const user = ctx.state.user;
const document = await Document.findById(id);
const collection = document.collection;
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
@@ -216,16 +217,18 @@ router.post('documents.update', auth(), async ctx => {
if (title) document.title = title;
if (text) document.text = text;
document.lastModifiedById = user.id;
await document.save();
const collection = await Collection.findById(document.atlasId);
if (collection.type === 'atlas') {
await collection.updateDocument(document);
document.collection = collection;
}
const [updatedDocument, updatedCollection] = await Promise.all([
document.save(),
collection.type === 'atlas'
? await collection.updateDocument(document)
: Promise.resolve(),
]);
updatedDocument.collection = updatedCollection;
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(ctx, updatedDocument),
};
});