2 Home
Braydon edited this page 2023-12-14 23:26:45 -08:00

Feather

Feather is an efficient lightweight database library.

Dependency

First off, you need to get the dependency into your project so you can use it.

Maven

<dependency>
  <groupId>me.braydon</groupId>
  <artifactId>Feather</artifactId>
  <version>1.0-dev</version>
</dependency>

Gradle (Kotlin DSL)

implementation("me.braydon:Feather:1.0-dev")

Connecting

Connecting to a database is very simple, all you have to do is construct the instance of the database you would like to connect to, and then invoke it's #connect(credentials) method.

MongoDB mongo = new MongoDB(); // Create the database instance
mongo.connect(new ConnectionString("mongodb://admin:p4$$w0rd@localhost:27017/feather?authSource=admin")); // Connect to the MongoDB server

// Close the database connection after our app is finished
mongo.close();

Examples

Saving & Finding an entity

public static void main(@NonNull String[] args) {
    MongoDB mongo = new MongoDB(); // Create the database instance
    mongo.connect(new ConnectionString("mongodb://admin:p4$$w0rd@localhost:27017/feather?authSource=admin")); // Connect to the MongoDB server

    // Create a new player repository, and add a player to it
    MongoRepository<UUID, Player> repository = mongo.newRepository("players", Player.class);
    repository.save(new Player(uuid, "Rainnny")); // Save our player

    // Get the player from the repository that we just saved
    Player found = repository.find(uuid);
    System.out.println("player = " + (found == null ? "n/a" : found));
    
    // Close the database connection after our app is finished
    mongo.close();
}

/**
 * A simple player entity.
 */
@NoArgsConstructor @AllArgsConstructor @Getter @ToString
public static class Player {
    /**
    * The uuid of this player.
    */
    @Id @Field private UUID uuid;
    
    /**
     * The name of this player.
     */
    @Field(key = "name") @Index private String username;
}