Skip to content

Commit

Permalink
fix: Insert a value on first increment call (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
floydya authored Jan 21, 2025
1 parent 8e8f194 commit 50e647c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/server/document/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ export async function useGlobal<T extends Object = Object>(identifier: string) {
*/
async function increment(fieldName: string, value: number = 1): Promise<number> {
const client = await db.getClient();
const updatedValue = await client
.collection(CollectionNames.Global)
.findOneAndUpdate(
{ _id: data[identifier]._id },
{ $inc: { [fieldName]: value } },
{ upsert: true, returnDocument: 'after' },
);
data[identifier][fieldName] = updatedValue.value[fieldName];
if (data[identifier][fieldName] !== undefined) {
const updatedValue = await client
.collection(CollectionNames.Global)
.findOneAndUpdate({ identifier }, { $inc: { [fieldName]: value } }, { returnDocument: 'after' });
data[identifier][fieldName] = updatedValue[fieldName];
} else {
const updatedValue = await client
.collection(CollectionNames.Global)
.findOneAndUpdate({ identifier }, { $set: { [fieldName]: value } }, { returnDocument: 'after' });
data[identifier][fieldName] = updatedValue[fieldName];
}
return data[identifier][fieldName];
}

Expand Down

0 comments on commit 50e647c

Please sign in to comment.