1.15.2 What is the best, most elegant, design patten for saving plugin data to databases and flat files?

Discussion in 'Spigot Plugin Development' started by Joeldesante, Feb 21, 2020.

  1. As a programmer, one of the biggest things I struggle with while making plugins is saving data to the disk. For me, I often get all caught up in the structure and design of my classes or I'll get tripped up by having to implement connection pools, validation, etc, etc...

    Long story short, saving data has ALWAYS been a headache for me.

    More often than not people will recommend that I use an external library to handle this kind of stuff. But from my perspective this behavior doesn't teach me anything.

    What is the best, most elegant, design patten for saving plugin data to databases and flat files?
    What is the best way to think about saving data?

    Thank you!
  2. I am not sure what oyu mean by design pattern?
    I will just answer what methods for storing data and reading/accessing it again.
    For me, it depends on the plugin.
    If it is a public plugin that I will release, I'd rather not use a database, I would be leaking the database credentials(ofcourse you can force the players to use their own database and add their credentials, but not everyone can get a database or afford it or whatever)
    If your plugin is very large, saving very important data, I'd recommend a database.
    If the data is not that important, store it in a config.
    What I like doing is, when storing somewhat important things in a config, and I really want it in a config.
    Let's say I want to store important strings in a config.
    I don't store the raw strings, I kind of make my own system( I don't want to go in detail)
    to convert these string's into some non human-readable strings and make my own system to convert it back.
    #2 retrooper, Feb 21, 2020
    Last edited: Feb 21, 2020
  3. I appreciate the response but this does not answer the question.

    Here is an in-depth explanation on what a design pattern is.

    On the note of databases, typically I'll use a SQLite database, so the user doesn't need to have a hosted database like OracleDB or MySQL.
  4. It really just depends on the use case. If it’s a plugin meant for users, give multiple options. My basic three are MongoDB, MySQL, and flatfiles. I believe the java mysql drivers provide support for a variety of mysql-based databases.
    If you want to save player data, use persistent data if it is a small amount. For larger amounts go straight to databases.
    Also about software patterns, nobody here is gonna go make one for a broad question like this, cause there is no one solution. Data saving is very much case by case, and it helps if you provide what data you are trying to save.
  5. Interface: IBackend
    Implementations: FlatFileBackend, MySQLBackend, MongoBackend

    And a PlayerData class
  6. Look into the DAO pattern.
    • Agree Agree x 1
  7. Thank you!
  8. Use Facade and hide the details of underlying data storage method from your plugin.
    • Agree Agree x 1
  9. Personnaly I'm using MongoDB for that database storage and Morphia as an easy way to save my data.
  10. exactly what i said bruh
  11. The response that you are referencing was also not answering the question.

    I'll probably go with DAO or Facade.
    • Informative Informative x 1
  12. Strategy or Decorator Patter could be used for that.