@Override public <S extends T> Mono<S> insert(S entity) { Assert.notNull(entity, "Entity must not be null!"); return mongoOperations.insert(entity, entityInformation.getCollectionName()); }
@Override public <S extends T> Flux<S> insert(Publisher<S> entities) { Assert.notNull(entities, "The given Publisher of entities must not be null!"); return Flux.from(entities).flatMap(entity -> mongoOperations.insert(entity, entityInformation.getCollectionName())); }
@Override public <S extends T> Flux<S> saveAll(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities must not be null!"); Streamable<S> source = Streamable.of(entities); return source.stream().allMatch(entityInformation::isNew) ? // mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : // Flux.fromIterable(entities).flatMap(this::save); }
@Override public <S extends T> Mono<S> save(S entity) { Assert.notNull(entity, "Entity must not be null!"); if (entityInformation.isNew(entity)) { return mongoOperations.insert(entity, entityInformation.getCollectionName()); } return mongoOperations.save(entity, entityInformation.getCollectionName()); }
@Override public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) { Assert.notNull(entityStream, "The given Publisher of entities must not be null!"); return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? // mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : // mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity))); }
@Override public <S extends T> Mono<S> insert(S entity) { Assert.notNull(entity, "Entity must not be null!"); return mongoOperations.insert(entity, entityInformation.getCollectionName()); }
@Override public <S extends T> Flux<S> insert(Publisher<S> entities) { Assert.notNull(entities, "The given Publisher of entities must not be null!"); return Flux.from(entities).flatMap(entity -> mongoOperations.insert(entity, entityInformation.getCollectionName())); }
@Override public <S extends T> Flux<S> saveAll(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities must not be null!"); Streamable<S> source = Streamable.of(entities); return source.stream().allMatch(entityInformation::isNew) ? // mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : // Flux.fromIterable(entities).flatMap(this::save); }
@Override public <S extends T> Mono<S> save(S entity) { Assert.notNull(entity, "Entity must not be null!"); if (entityInformation.isNew(entity)) { return mongoOperations.insert(entity, entityInformation.getCollectionName()); } return mongoOperations.save(entity, entityInformation.getCollectionName()); }
@Override public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) { Assert.notNull(entityStream, "The given Publisher of entities must not be null!"); return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? // mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : // mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity))); }
@EventListener(value = ContextRefreshedEvent.class) public void init() { log.info("start data initialization ..."); this.mongoTemplate.inTransaction() .execute( s -> Flux .just("Post one", "Post two") .flatMap( title -> s.insert(Post.builder().title(title).content("content of " + title).build()) ) ) .subscribe( v -> log.info("Ok"), e -> log.error("error:" + e), () -> log.info("done data initialization...") ); }