|
1 | 1 | # ForestDatabase |
| 2 | + |
| 3 | +ForestDatabase is a Java library for SpigotMC projects which |
| 4 | +allows automated SQL queries generation using custom entity annotations. The library supports database system PostgreSQL. |
| 5 | + |
| 6 | +## Table of contents |
| 7 | + |
| 8 | +* [Getting started](#getting-started) |
| 9 | +* [Setting up the API](#setting-up-the-api) |
| 10 | +* [Annotations](#annotations) |
| 11 | +* [Accessing the database](#accessing-the-database) |
| 12 | +* [License](#license) |
| 13 | + |
| 14 | +## Getting started |
| 15 | + |
| 16 | +[](https://jitpack.io/#ForestTechMC/ForestDatabase) |
| 17 | + |
| 18 | +Replace **VERSION** with the version you want to use. The latest is always recommended. |
| 19 | + |
| 20 | +<details> |
| 21 | + <summary>Maven</summary> |
| 22 | + |
| 23 | +```xml |
| 24 | +<repositories> |
| 25 | + <repository> |
| 26 | + <id>jitpack.io</id> |
| 27 | + <url>https://jitpack.io</url> |
| 28 | + </repository> |
| 29 | +</repositories> |
| 30 | + |
| 31 | +<dependencies> |
| 32 | + <dependency> |
| 33 | + <groupId>com.github.ForestTechMC</groupId> |
| 34 | + <artifactId>ForestDatabase</artifactId> |
| 35 | + <version>1.0.8</version> |
| 36 | + </dependency> |
| 37 | +</dependencies> |
| 38 | +``` |
| 39 | +</details> |
| 40 | + |
| 41 | +<details> |
| 42 | + <summary>Gradle</summary> |
| 43 | + |
| 44 | +```groovy |
| 45 | +allprojects { |
| 46 | + repositories { |
| 47 | + maven { url 'https://jitpack.io' } |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +dependencies { |
| 52 | + implementation 'com.github.ForestTechMC:ForestDatabase:VERSION' |
| 53 | +} |
| 54 | +``` |
| 55 | +</details> |
| 56 | + |
| 57 | +## Setting up the API |
| 58 | + |
| 59 | +To use ForestDatabase, its API needs to be initialized first. It is recommended to do so in `onEnable` method. |
| 60 | + |
| 61 | +```java |
| 62 | +@Override |
| 63 | +public void onEnable() { |
| 64 | + DatabaseAPI databaseAPI = new DatabaseAPI(this); |
| 65 | + databaseAPI.setup(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +After initial setup, database object needs to be registered to the API. |
| 70 | + |
| 71 | +```java |
| 72 | +HikariDatabase hikariDatabase = new HikariDatabase( |
| 73 | + "localhost:5432", |
| 74 | + "my_database", |
| 75 | + "username", |
| 76 | + "password"); |
| 77 | + |
| 78 | +databaseAPI.addDatabase("database_id", hikariDatabase); |
| 79 | +``` |
| 80 | + |
| 81 | +All connections shall be closed using `databaseAPI#closeAll()` call. |
| 82 | + |
| 83 | +## Annotations |
| 84 | + |
| 85 | +To make entity be recognized by the ForestDatabase, it needs to be annotated with special annotations and must include empty constructor. |
| 86 | + |
| 87 | +Each entity needs to be annotated with `@DatabaseEntity` annotation. Each field which shall be recognized by ForestDatabase shall include `@Column` annotation. |
| 88 | + |
| 89 | +```java |
| 90 | +import cz.foresttech.database.annotation.*; |
| 91 | + |
| 92 | +@DatabaseEntity |
| 93 | +public class Car { |
| 94 | + |
| 95 | + @Column |
| 96 | + @PrimaryKey |
| 97 | + @AutoIncrement |
| 98 | + private int id; |
| 99 | + |
| 100 | + @Column |
| 101 | + @Text(customLength = 50) |
| 102 | + private String brandName; |
| 103 | + |
| 104 | + @Column |
| 105 | + private double price; |
| 106 | + |
| 107 | + @Column |
| 108 | + @NullableColumn |
| 109 | + @Text |
| 110 | + private String description; |
| 111 | + |
| 112 | + // Will be ignored by ForestDatabase |
| 113 | + private boolean internalValue; |
| 114 | + |
| 115 | + public Car() { |
| 116 | + } |
| 117 | + |
| 118 | + /* .. getters, setter, another constructors ... */ |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +ForestDatabase will automatically convert class names and field names to snake case, if custom name is not used. |
| 123 | + |
| 124 | +```java |
| 125 | +@DatabaseEntity(table="custom_table_name") |
| 126 | +@Column(key="custom_column_name") |
| 127 | +``` |
| 128 | + |
| 129 | +## Accessing the database |
| 130 | + |
| 131 | +To access the database and load/edit the data, you can use direct methods on DatabaseAPI instance. |
| 132 | + |
| 133 | +```java |
| 134 | +// Creates the table (if not exist) |
| 135 | +databaseAPI.createTable("database_id", Car.class); |
| 136 | + |
| 137 | +// Inserts the object data into the database |
| 138 | +Car car = new Car(); |
| 139 | +databaseAPI.insertOrUpdate("database_id", car); |
| 140 | + |
| 141 | +// Removes the object from the database asynchronously |
| 142 | +databaseAPI.deleteAsync("database_id", car); |
| 143 | + |
| 144 | +// Retrieves all objects from the database |
| 145 | +databaseAPI.findAll("database_id", Car.class).forEach(car -> { |
| 146 | + // ... do stuff |
| 147 | +}); |
| 148 | +``` |
| 149 | + |
| 150 | +## License |
| 151 | +ForestDatabase is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/ForestTechMC/ForestRedisAPI/blob/master/LICENSE.txt) for more information. |
0 commit comments