Improve concurrency and reduce one DB call from document saving
This commit is contained in:
@ -208,7 +208,10 @@ router.post('documents.update', auth(), async ctx => {
|
|||||||
ctx.assertPresent(title || text, 'title or text is required');
|
ctx.assertPresent(title || text, 'title or text is required');
|
||||||
|
|
||||||
const user = ctx.state.user;
|
const user = ctx.state.user;
|
||||||
const document = await Document.findById(id);
|
const document = await Document.findById(id, {
|
||||||
|
include: ['collection'],
|
||||||
|
});
|
||||||
|
const collection = document.collection;
|
||||||
|
|
||||||
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
||||||
|
|
||||||
@ -216,16 +219,18 @@ router.post('documents.update', auth(), async ctx => {
|
|||||||
if (title) document.title = title;
|
if (title) document.title = title;
|
||||||
if (text) document.text = text;
|
if (text) document.text = text;
|
||||||
document.lastModifiedById = user.id;
|
document.lastModifiedById = user.id;
|
||||||
await document.save();
|
|
||||||
|
|
||||||
const collection = await Collection.findById(document.atlasId);
|
const [updatedDocument, updatedCollection] = await Promise.all([
|
||||||
if (collection.type === 'atlas') {
|
document.save(),
|
||||||
await collection.updateDocument(document);
|
collection.type === 'atlas'
|
||||||
document.collection = collection;
|
? await collection.updateDocument(document)
|
||||||
}
|
: Promise.resolve(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
updatedDocument.collection = updatedCollection;
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, document),
|
data: await presentDocument(ctx, updatedDocument),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user