Quick Start

Setup Build Tools (Maven or Gradle)

<repositories>
    <repository>
        <id>minecodes-repository</id>
        <url>https://repository.minecodes.pl/releases</url>
     </repository>
</repositories>
    
<dependencies>
    <dependency>
        <groupId>pl.szczurowsky</groupId>
        <artifactId>rat-orm-mongodb</artifactId>
        <version>1.4.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Connect to data source

public class Example {
    
    Database database;
    
    public void connect() {
        // Replace MongoDB() with your database type
        this.database = new MongoDB();
        Map<String, String> credentials = new HashMap<>();
        credentials.put("name", "name of db");
        credentials.put("username", "username");
        credentials.put("password", "password");
        credentials.put("host", "DNS or IP");
        credentials.put("port", "port");
        this.database.connect(credentials);
    }
    
}

Create model

ExampleModel.class
@Model(tableName="example-table")
public class ExampleModel extends BaseModel {
    @ModelField(isPrimaryKey = true)
    int id;
    @ModelField
    String username = "default value";
    // Custom table name
    @ModelField(name="test")
    String oneName;
}

Now to initialize model

this.database.initModel(Arrays.asList(
                ExampleModel.class
        ));

CRUD

Create

Model is being saved to database and also to local cache

Now we want to create object basing on our model

ExampleModel exampleModel = new ExampleModel();
this.database.save(exampleModel, ExampleModel.class);

Read

List<ExampleModel> data = this.database.fetchAll(ExampleModel.class);

Update

Update works same as saving - Just save updated object and it will be replaced by updated

ExampleModel exampleModel = new ExampleModel();
exampleModel.oneName = "Test";
this.database.save(exampleModel, ExampleModel.class);

Delete

ExampleModel exampleModel = new ExampleModel();
this.database.delete(ExampleModel.class, exampleModel);

Last updated