diff --git a/src/main/java/me/braydon/feather/database/impl/mongodb/MongoRepository.java b/src/main/java/me/braydon/feather/database/impl/mongodb/MongoRepository.java index c10fc81..c1934ae 100644 --- a/src/main/java/me/braydon/feather/database/impl/mongodb/MongoRepository.java +++ b/src/main/java/me/braydon/feather/database/impl/mongodb/MongoRepository.java @@ -12,16 +12,16 @@ import com.mongodb.client.model.Indexes; import com.mongodb.client.model.UpdateOneModel; import com.mongodb.client.model.UpdateOptions; import lombok.NonNull; +import lombok.SneakyThrows; import me.braydon.feather.common.Tuple; import me.braydon.feather.database.Repository; +import me.braydon.feather.database.impl.mongodb.annotation.CustomDocument; import me.braydon.feather.database.impl.mongodb.annotation.Index; import org.bson.Document; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.lang.reflect.Method; +import java.util.*; /** * The {@link MongoDB} {@link Repository} implementation. @@ -90,17 +90,29 @@ public class MongoRepository extends Repository { * @param entities the entities to save * @see E for entity */ - @Override + @Override @SneakyThrows public void saveAll(@NonNull E... entities) { List> updateModels = new ArrayList<>(); // The update models to bulk write for (E entity : entities) { me.braydon.feather.data.Document document = new me.braydon.feather.data.Document<>(entity); // Create a document from the entity + Document bsonDocument; // The Bson document to save + + Method customDocumentMethod = Arrays.stream(entity.getClass().getDeclaredMethods()) + .filter(method -> method.isAnnotationPresent(CustomDocument.class)) + .findFirst().orElse(null); // Get the @CustomDocument method + + // We have a custom document method + if (customDocumentMethod != null && (customDocumentMethod.getReturnType() == Document.class)) { + bsonDocument = (Document) customDocumentMethod.invoke(entity); // Get our custom document + } else { // Otherwise, use our mapped data + bsonDocument = new Document(document.toMappedData()); + } // Add our update model to the list updateModels.add(new UpdateOneModel<>( Filters.eq(document.getIdKey(), document.getKey()), - new Document("$set", new Document(document.toMappedData())), + new Document("$set", bsonDocument), new UpdateOptions().upsert(true) )); diff --git a/src/main/java/me/braydon/feather/database/impl/mongodb/annotation/CustomDocument.java b/src/main/java/me/braydon/feather/database/impl/mongodb/annotation/CustomDocument.java new file mode 100644 index 0000000..bf06f49 --- /dev/null +++ b/src/main/java/me/braydon/feather/database/impl/mongodb/annotation/CustomDocument.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Braydon (Rainnny). All rights reserved. + * + * For inquiries, please contact braydonrainnny@gmail.com + */ +package me.braydon.feather.database.impl.mongodb.annotation; + +import java.lang.annotation.*; + +/** + * Methods tagged with this annotation will be invoked + * when a document is being saved to the database, this + * allows is to save a custom document, rather than + * parsing the entity and creating one that way. + * + * @author Braydon + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Documented @Inherited +public @interface CustomDocument { } \ No newline at end of file