Skip to content

Commit

Permalink
Use single write instead of bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Dec 19, 2023
1 parent 6ae9eb3 commit 675a764
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions src/api/db/models/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,16 @@ export class ImageModel {
}

static async createComment(input, context) {
const operation = async (input) => {
return await retry(async (bail, attempt) => {
if (attempt > 1) console.log(`Retrying createComment operation! Try #: ${attempt}`);

// find images, add comment, and bulk write
return await Image.bulkWrite([{
updateOne: {
filter: { _id: input.imageId },
update: { $push: { comments: {
author: context.user['cognito:username'],
comment: input.comment
} } }
}
}]);
}, { retries: 2 });
};

try {
await operation(input);
const image = await ImageModel.queryById(input.imageId, context);

if (!image.comments) image.comments = [];
image.comments.push({
author: context.user['cognito:username'],
comment: input.comment
});
await image.save();

return { comments: image.comments };
} catch (err) {
// if error is uncontrolled, throw new ApolloError
Expand Down
2 changes: 1 addition & 1 deletion src/api/db/schemas/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Schema = mongoose.Schema;
*/

const ImageCommentSchema = new Schema({
_id: { type: String, required: true, default: Schema.Types.ObjectId, },
_id: { type: String, required: true, default: Schema.Types.ObjectId },
author: { type: String, required: true },
created: { type: Date, default: Date.now, required: true },
comment: { type: String, required: true }
Expand Down

0 comments on commit 675a764

Please sign in to comment.