Skip to content

Commit

Permalink
feat: add DeleteFields to async
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbolgarin committed Dec 17, 2024
1 parent b68803c commit fed241b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions async.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,21 @@ func (ac *AsyncCollection) UpdateOneFromDiff(queueKey, taskName string, filter M
})
}

// DeleteOne deletes a document in the collection asynchronously without waiting for it to complete.
// DeleteFields deletes fields in a document in the collection asynchronously without waiting for it to complete.
// For example: [key1, key2] becomes {$unset: {key1: "", key2: ""}}.
// It start retrying in case of error for DefaultAsyncRetries times.
// It filters errors and won't retry in case of ErrNotFound, ErrInvalidArgument and some other errors.
// Tasks in different queues will be executed in parallel.
func (ac *AsyncCollection) DeleteFields(queueKey, taskName string, filter M, fields ...string) {
ac.push(queueKey, taskName, "delete_fields", func(ctx context.Context) error {
return ac.coll.DeleteFields(ctx, filter, fields...)
})
}

// DeleteOne deletes a document in the collection asynchronously without waiting for it to complete.
// It start retrying in case of error for DefaultAsyncRetries times.
// It filters errors and won't retry in case of ErrNotFound, ErrInvalidArgument and some other errors.
// Tasks in different queues will be executed in parallel.
func (ac *AsyncCollection) DeleteOne(queueKey, taskName string, filter M) {
ac.push(queueKey, taskName, "delete_one", func(ctx context.Context) error {
return ac.coll.DeleteOne(ctx, filter)
Expand Down Expand Up @@ -345,10 +355,17 @@ func (qc *QueueCollection) UpdateOneFromDiff(filter M, diff any) {
qc.AsyncCollection.UpdateOneFromDiff(qc.name, "", filter, diff)
}

// DeleteOne deletes a document in the collection asynchronously without waiting for it to complete.
// DeleteFields deletes fields in a document in the collection asynchronously without waiting for it to complete.
// For example: [key1, key2] becomes {$unset: {key1: "", key2: ""}}.
// It start retrying in case of error for DefaultAsyncRetries times.
// It filters errors and won't retry in case of ErrNotFound, ErrInvalidArgument and some other errors.
func (qc *QueueCollection) DeleteFields(filter M, fields ...string) {
qc.AsyncCollection.DeleteFields(qc.name, "", filter, fields...)
}

// DeleteOne deletes a document in the collection asynchronously without waiting for it to complete.
// It start retrying in case of error for DefaultAsyncRetries times.
// It filters errors and won't retry in case of ErrNotFound, ErrInvalidArgument and some other errors.
func (qc *QueueCollection) DeleteOne(filter M) {
qc.AsyncCollection.DeleteOne(qc.name, "", filter)
}
Expand Down
2 changes: 2 additions & 0 deletions mongox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ func TestAsync(t *testing.T) {
queueColl.UpdateOneFromDiff(mongox.M{"id": "3"}, diff)

queueColl.UpdateMany(mongox.M{"id": "1"}, mongox.M{mongox.Inc: mongox.M{"number": 1}})
queueColl.DeleteFields(mongox.M{"id": "3"}, "struct.name")
queueColl.DeleteOne(mongox.M{"number": entity.Number + 1})
queueColl.Insert(entity, entity, entity)
queueColl.DeleteMany(mongox.M{"number": entity.Number})
Expand Down Expand Up @@ -1388,6 +1389,7 @@ func TestAsync(t *testing.T) {
entity3.Name = "new-name"
entity3.Number++
entity3.Bool = !entity3.Bool
entity3.Struct.Name = ""
testAsync(t, ctx, db, entity3, mongox.M{"id": "3"})
})
}
Expand Down

0 comments on commit fed241b

Please sign in to comment.