Created
January 22, 2020 00:41
-
-
Save ghahramani/c3c1f99097ceabb766a0060a6f96ab64 to your computer and use it in GitHub Desktop.
Spring data extending default methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NoRepositoryBean | |
interface BaseGenericReactiveMongoRepository<T> : ReactiveMongoRepository<T, String> { | |
fun patch(id: String, fields: Map<String, Any>): Mono<T> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BookRepository : ReactiveMongoRepository<Book, String> { | |
findByNameContainingIgnoreCaseAndVisibileIsTrue(name:String): Flux<Book> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableReactiveMongoRepositories( | |
basePackages = ["**.repository"], | |
repositoryBaseClass = SimpleBaseGenericReactiveMongoRepository::class | |
) | |
class MongoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleBaseGenericReactiveMongoRepository<ENTITY>( | |
private val entityInformation: MappingMongoEntityInformation<ENTITY, String>, | |
private val template: ReactiveMongoTemplate | |
) : SimpleReactiveMongoRepository<ENTITY, String>(entityInformation, template), | |
BaseGenericReactiveMongoRepository<ENTITY> { | |
private val eventPublisher: ApplicationEventPublisher? | |
init { | |
val context = template.converter.mappingContext as MongoMappingContext | |
val indexCreator = MongoPersistentEntityIndexCreator(context) { collectionName -> | |
IndexOperationsAdapter.blocking(template.indexOps(collectionName)) | |
} | |
eventPublisher = MongoMappingEventPublisher(indexCreator) | |
} | |
override fun patch(id: String, fields: Map<String, Any>): Mono<ENTITY> { | |
val collection = entityInformation.collectionName | |
val query = Query(Criteria.where("_id").`is`(id)) | |
val document = Document() | |
return findById(id) | |
.flatMap { entity -> | |
maybeEmitEvent(BeforeConvertEvent<ENTITY>(entity, collection)) | |
document.putAll(fields) | |
val update = Update() | |
fields | |
.filter { entry -> | |
!hashSetOf("_id", "createdAt", "createdBy", "modifiedAt", "modifiedBy").contains(entry.key) | |
} | |
.forEach { entry -> update.set(entry.key, entry.value) } | |
maybeEmitEvent(BeforeSaveEvent<ENTITY>(entity, document, collection)) | |
template.updateFirst(query, update, collection) | |
} | |
.then(findById(id)).map { entity -> | |
maybeEmitEvent(AfterSaveEvent<ENTITY>(entity, document, collection)) | |
entity | |
} | |
} | |
private fun <T> maybeEmitEvent(event: MongoMappingEvent<T>) { | |
eventPublisher?.publishEvent(event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment