Skip to content

Repository

Cameron Carvalho edited this page Nov 11, 2024 · 2 revisions

Supervisor Repository Wiki

Welcome to the Supervisor Repository Wiki! This guide will walk you through creating and using repositories in Supervisor, allowing you to efficiently handle data persistence for your plugins.

Repositories Overview

In Supervisor, repositories are responsible for managing the data storage layer. They provide an abstraction that allows you to perform CRUD (Create, Read, Update, Delete) operations on your data without worrying about the underlying storage mechanisms. Supervisor provides several pre-built repository types for different data storage systems, such as JSON, MongoDB, and Redis.

Create a Repository Interface

To create a repository for your data, you need to extend the appropriate Supervisor repository interface. In the example below, we create a PlayerDataRepository that uses JSON as the storage medium.

package com.vertmix.supervisor.core.bukkit.dummy.repository;

import com.vertmix.supervisor.core.annotation.Component;
import com.vertmix.supervisor.core.bukkit.dummy.model.PlayerData;
import com.vertmix.supervisor.repository.json.bukkit.BukkitJsonPlayerRepository;

@Component
public interface PlayerDataRepository extends BukkitJsonPlayerRepository<PlayerData> {

}

In this example, PlayerDataRepository is an interface annotated with @Component, which means it will be automatically managed by Supervisor's dependency injection system. The repository extends BukkitJsonPlayerRepository, which allows it to handle PlayerData objects and store them in JSON format.

Available Repository Types

Supervisor provides several repository types that you can use depending on your storage requirements. Here are the available options:

JSON Repositories

  • BukkitJsonPlayerRepository: This repository is specifically designed for managing player-specific data in JSON format.
  • BukkitJsonRepository: This is a general-purpose repository for storing any type of data in JSON format.

JSON is a lightweight, easy-to-read format that is well-suited for structured data and is ideal for small to medium-sized datasets.

MongoDB Repositories

  • BukkitMongoPlayerRepository: This repository is designed for managing player-specific data using MongoDB as the storage backend.
  • BukkitMongoRepository: This is a general-purpose repository for storing any type of data in MongoDB.

MongoDB is a NoSQL database that is suitable for large-scale applications and scenarios where you need more complex querying capabilities.

Redis Repositories

  • BukkitRedisPlayerRepository: This repository is used for managing player-specific data using Redis as the storage backend.
  • BukkitRedisRepository: This is a general-purpose repository for storing any type of data in Redis.

Redis is an in-memory data structure store, commonly used for caching and scenarios where extremely fast access to data is required.

Example Usage

Here's an example of how to create and use a MongoDB repository to store player data in your plugin.

Create a PlayerData Model

First, create a model that represents the data you want to store. In this case, we create a PlayerData model:

package com.vertmix.supervisor.core.bukkit.dummy.model;

import java.util.UUID;

public class PlayerData {

    private final UUID uuid;
    private final String name;

    public PlayerData(UUID uuid, String name) {
        this.uuid = uuid;
        this.name = name;
    }

    public UUID getUuid() {
        return uuid;
    }

    public String getName() {
        return name;
    }
}

Create a MongoDB Repository

Now, create a repository interface that will manage the PlayerData using MongoDB:

package com.vertmix.supervisor.core.bukkit.dummy.repository;

import com.vertmix.supervisor.core.annotation.Component;
import com.vertmix.supervisor.core.bukkit.dummy.model.PlayerData;
import com.vertmix.supervisor.repository.mongo.bukkit.BukkitMongoPlayerRepository;

@Component
public interface PlayerDataRepository extends BukkitMongoPlayerRepository<PlayerData> {

}

Create a Service to Use the Repository

Next, create a service that interacts with the repository to perform data operations.

package com.vertmix.supervisor.core.bukkit.dummy.service;

import com.vertmix.supervisor.core.annotation.Component;
import com.vertmix.supervisor.core.bukkit.dummy.model.PlayerData;
import com.vertmix.supervisor.core.bukkit.dummy.repository.PlayerDataRepository;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;

@Component
public class PlayerDataService implements Listener {

    private final PlayerDataRepository playerDataRepository;

    public PlayerDataService(PlayerDataRepository playerDataRepository) {
        this.playerDataRepository = playerDataRepository;
    }

    @EventHandler
    public void onAsyncPlayerJoin(AsyncPlayerPreLoginEvent event) {
        PlayerData playerData = playerDataRepository.find(event.getUniqueId());
        if (playerData == null) {
            playerDataRepository.save(event.getUniqueId(), new PlayerData(event.getUniqueId(), event.getName()));
        }
    }
}

In this example, the PlayerDataService listens for player login events and uses the PlayerDataRepository to load or create player data. The repository abstracts away the underlying MongoDB operations, making the code cleaner and easier to maintain.

Summary

Repositories in Supervisor provide a convenient way to manage data persistence, allowing you to choose the storage system that best suits your needs. Whether you're using JSON for small-scale data, MongoDB for complex applications, or Redis for fast caching, Supervisor has you covered with a range of repository implementations.

If you need further assistance or have any questions, feel free to contact us or contribute to the project!

Contributing

If you're interested in contributing to the Supervisor project, feel free to submit pull requests or raise issues on GitHub. We welcome suggestions and improvements from the community.

License

The Supervisor project is licensed under the MIT License. See the LICENSE file for more information.

Contact

For further questions or suggestions, contact us at support@vertmix.com.

Thank you for being part of the Supervisor community!

Clone this wiki locally