Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use full documentKey from Mongo Change Event as query filter #63

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pl.allegro.tech.mongomigrationstream.core.synchronization

import com.mongodb.DBRefCodecProvider
import com.mongodb.client.model.DeleteOneModel
import com.mongodb.client.model.Filters
import com.mongodb.client.model.ReplaceOneModel
import com.mongodb.client.model.ReplaceOptions
import com.mongodb.client.model.UpdateOneModel
Expand Down Expand Up @@ -42,7 +41,6 @@ internal sealed class ChangeEvent(
null
}

protected fun idFilter() = Filters.eq("_id", documentKey.getValue("_id"))
protected abstract fun toWriteModelImpl(): WriteModel<BsonDocument>

companion object {
Expand Down Expand Up @@ -89,7 +87,7 @@ internal data class InsertReplaceChangeEvent(
}

override fun toWriteModelImpl(): WriteModel<BsonDocument> = ReplaceOneModel(
idFilter(),
documentKey,
document!!,
ReplaceOptions().upsert(true)
)
Expand All @@ -107,7 +105,7 @@ internal data class DeleteChangeEvent(
)
}

override fun toWriteModelImpl(): WriteModel<BsonDocument> = DeleteOneModel(idFilter())
override fun toWriteModelImpl(): WriteModel<BsonDocument> = DeleteOneModel(documentKey)
}

internal data class UpdateChangeEvent(
Expand All @@ -132,7 +130,7 @@ internal data class UpdateChangeEvent(
}

override fun toWriteModelImpl(): WriteModel<BsonDocument> = UpdateOneModel(
idFilter(),
documentKey,
Updates.combine(
*updatedFields.entries.map { Updates.set(it.key, it.value) }.toTypedArray(),
*removedFields.map { Updates.unset(it) }.toTypedArray()
Expand Down