-
Notifications
You must be signed in to change notification settings - Fork 10
Storage Structure
Fairy has built-in a very powerful tool for Storage/Database
It all based of the Repository and Repository Provider structures
PlayerStorage
on the top of Repository
a Repository Provider is the handler to handle connection to each type of databases
you can either adding a Repository Provider in config /plugin/fairy/storage.yml
or programmatically registering it with API
Storage.registerRepositoryProvider(new MyRepositoryProvider());
-
H2
- a local Flat File database based on SQL structure -
Hikari (MySQL, MariaDB, PostgreDB)
- MySQL database, commonly used on a lot of plugins and servers -
MongoDB
- Effective json based database
a Repository is the front item that let you access data, It normally being Collection
for MongoDB or Table
for SQL
And Repository is POJO (stands for Plain Old Java Object)
based
Which means you can put less effort to how your object should be structured in the database, just simply throw the object in and it will be serialized (tho you will still need to take care of what objects can be serialized, but it's much easier)
Repositories requires a Repository Provider that provides database to work
So you might want to add a Repository Provider first
After that, you can get the Repository very easy with following code
Repository<MyData, UUID> myRepo = Storage.createRepository("myRepo", MyData.class);
Repository has 2 generic, first is your Data Class, second is Key Type
- Data Class - as it name, It's the class that stores every data
- Key Type - the Key of the repository, if you are using it for Bukkit Player Data, UUID is recommended
WIP Serializable Objects
- How to make a serializable object that repository accepts
after you get the repository, you can now start using it, here is some example
MyData myData = new MyData(player.getUniqueId());
myData.setKills(1);
// Save my Data
myRepo.save(myData);
// Find my Data by Key
Optional<MyData> byKey = myRepo.findById(player.getUniqueId());
System.out.println(optional.get().getKills());
// Find my Data by query
Optional<MyData> byKills = myRepo.findByQuery("kills", 1);
System.out.println(optional.get().getUUID());
// Delete my Data
myRepo.deleteById(player.getUniqueId());
Optional<MyData> findAttempt = myRepo.findById(player.getUniqueId());
System.out.println(findAttempt.isPresent());
// Delete All my Data
myRepo.deleteAll();
There is a lot more usable function awaiting your explore!