Skip to content

Commit

Permalink
Merge pull request #4 from vandie/fix---stop-duplication
Browse files Browse the repository at this point in the history
fix(storeMentionForPage): #3 - use update with upsert for new mentions
  • Loading branch information
vandie authored Aug 29, 2022
2 parents 933310e + f5ed1c6 commit b99f8a7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [vandie]
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webmention-handler-mongodb",
"version": "0.2.0",
"version": "0.2.1",
"description": "A Mongo DB Storage Handler for the Webmention Handler Project",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions src/mongo-webmention-storage.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ export class MongoWebMentionStorage implements IWebMentionStorage {

async deleteMention(mention: SimpleMention): Promise<null> {
const collection = this.fetchCollection<Mention>(this.mentionCollection);
await collection.deleteMany({ source: mention.source, target: mention.target });
await collection.deleteOne({ source: mention.source, target: mention.target });
return null;
}

async storeMentionForPage(page: string, mention: Mention): Promise<WithId<Mention>> {
const collection = this.fetchCollection<Mention>(this.mentionCollection);
const storedMention = await collection.insertOne(mention);
return {...mention, _id: storedMention.insertedId};
await collection.updateOne({target: mention.target, source: mention.source}, {$set: mention }, { upsert: true });
const storedMention = await collection.findOne<WithId<Mention>>({target: mention.target, source: mention.source});
if(!storedMention) throw new Error(`Could not find Mention after insertion, source ${mention.source} & target ${mention.target}`);
return storedMention;
}

async getMentionsForPage(page: string, type?: string): Promise<WithId<Mention>[]> {
Expand Down

0 comments on commit b99f8a7

Please sign in to comment.