From 4490fc785161c02fec241e21ed210f89b8f0a833 Mon Sep 17 00:00:00 2001 From: Niklas <57962465+niklasnieberler@users.noreply.github.com> Date: Mon, 18 Mar 2024 16:25:42 +0100 Subject: [PATCH] Create README.md --- README.md | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..3438236 --- /dev/null +++ b/README.md @@ -0,0 +1,159 @@ +# Database Simplified 🤙 +Here you can easily set up and use Morphia and Jedis. + +## Using Database Simplified in your plugin + +### Maven +```xml + + + github + https://maven.pkg.github.com/avionik-world/database-simplified + + +``` + +```xml + + + world.avionik + database-simplified + 1.0.1 + provided + + +``` + +### Gradle +```groovy +repositories { + maven { url = 'https://maven.pkg.github.com/avionik-world/database-simplified' } +} +``` + +```groovy +dependencies { + compileOnly 'world.avionik:database-simplified:1.0.1' +} +``` + +## How to create a new Configuration +There are two different ways to enter your sensitive data during setup. You can either use a **config file** in which you configure your database or simply use the **system environments**. + +``` kotlin +DatabaseSimplifiedKit.createConfiguration() + .withMorphia(MorphiaSettings.fromConfig()) // Of course you can also use fromEnv() here + .withJedis(JedisSettings.fromEnv()) // Of course you can also use fromConfig() here + .start() +``` + +### +If you are wondering how to create a new Morphia database, then you have come to the right place. Here are also two examples of how to create such a database. + +```kotlin +val morphiaDatastore = DatabaseSimplifiedKit.instance.createMorphiaDatastore("dbName") + +// Create a new database with kotlin extensions +val morphiaDatastore = createMorphiaDatastore("dbName") +``` + + +## How to use the MorphiaRepository +First of all, you need an entity class. Let's take this class as an example: +``` kotlin +@Entity("test_entity") +class TestEntity( + @Id val uniqueId: UUID, + val firstString: String +) +``` + +This is what a new repository could look like now. The UUID serves as an **identifier** for the entity. +``` kotlin +class TestEntityRepository( + datastore: Datastore +) : AbstractMorphiaRepository( + datastore, + TestEntity::class.java +) +``` + +You now have various options for using your entity. You can retrieve all entities or create and delete new ones. + +``` kotlin +class TestEntityRepository( + datastore: Datastore +) : AbstractMorphiaRepository( + datastore, + TestEntity::class.java +) { + + init { + val entityUniqueId = UUID.randomUUID() + + // Gets you all entities from this repository + findAll().thenAccept { + listOf(it.toList()) + } + + // Would you like to get your entity class with an identifier? Here's how: + findOrNull(entityUniqueId).thenAccept { + println(it?.uniqueId) + } + + // You can save a new entity to the repository as follows + val newEntity = TestEntity(entityUniqueId, "second") + save(newEntity) + + // You can use this to remove an entity. entityUniqueId is the identifier of the class + remove(entityUniqueId) + } + +} +``` + + +## How to use the JedisRepository +Here, too, you need an entity class. Here is the example: +``` kotlin +class TestEntity( + val firstString: String, + val secondString: String +) +``` + +The repository looks almost exactly like the Morphia repository. The only difference is that there is a **database pattern** instead of an identifier. This pattern is placed in front of the Jedis key. +``` kotlin +class TestEntityRepository : AbstractJedisRepository( + TestEntity::class.java, + "test_entity" // Here is the database pattern +) +``` + +Just like with the Morphia repository, you can let off steam here. You can retrieve all entities or create and delete new ones. + +``` kotlin +class TestEntityRepository : AbstractJedisRepository( + TestEntity::class.java, + "test_entity" // Here is the database pattern +) { + + init { + // Gets you all entities from this repository + findAll().forEach { + listOf(it.firstString) + } + + // Would you like to get your entity class with an key? Here's how: + val entity = find("jedis_key") + println(entity?.firstString) + + val newEntity = TestEntity("Hello", "Second") + insert("jedis_key", newEntity) + + // You can use this to remove an entity. jedis_key is the jedis key. + remove("jedis_key") + } + +} +```