Operation

What is operation?

Operation is organised type of more complex database query. It's really important when you executing an atomic query or you've got types of query which are executed multiple times in diffrent places.

When should I use operation

Every time when you executing an atomic query. For example if you are bank institution and user is transfering money. If anything in that action goes wrong we don't want to execute any query. That's why we're using transactions option in our operations.

Another example is when you've type of query which is executed multiple times in diffrent places. Then you can create one operation and execute it in only one line across your app

How i am supposed to create operation?

Creating model is just creating new runnable class

Example

TestOperation.java
public class TestOperation<T extends BaseModel> extends MongoOperation {


    private final Collection<T> models;
    private final Class<T> tClass;
    Database mongodb;

    public TestOperation(Database mongodb, Collection<T> models, Class<T> tClass) throws PassedWrongDriverToOperationException {
        super(mongodb);
        this.mongodb = mongodb;
        this.models = models;
        this.tClass = tClass;
        // Do we want to use transaction?
        this.useTransactions = true;
    }

    @Override
    public void execute(HashMap<String, Object> options) {
        for (T model : models) {
            mongodb.save(model, tClass, options);
            System.out.println("Saved: " + model.toString());
        }
        System.out.println("DONE");
    }
}RT

Last updated