public class YourRecordName implements Record<YourRecordName> {
Primary Fields = Primary Keys
Primary fields represent a unique identifier for the record.
They are declared with the @Primary annotation.
public static final @NotNull String IDENTIFIER_KEY = "identifier";
private final @Field(IDENTIFIER_KEY) @Primary @NotNull String identifier;
public static final @NotNull String STRING_KEY = "value";
public static final @NotNull String BOOL_KEY = "bool";
public static final @NotNull String OBJECT_KEY = "object";
private @Field(STRING_KEY) String string;
private @Field(BOOL_KEY) boolean bool;
private @Field(OBJECT_KEY) ExampleObject object;
public class ExampleObject {
private final @NotNull String testString = "test";
private int testInt = 123;
}
Foreign Fields = Foreign Keys
Foreign Fields are used to connect records. In SQL this is called a relational database.
public static final @NotNull String OTHER_TABLE_IDENTIFIER
= "other_table_identifier";
@Field(OTHER_TABLE_IDENTIFIER)
@Foreign(
table = OtherTable.TABLE_NAME,
tableField = OtherRecord.IDENTIFIER_KEY
)
private String foreign;
public TestRecord(@NotNull String identifier) {
this.identifier = identifier;
this.string = "The default value.";
this.bool = true;
this.object = new ObjectTest();
this.foreign = "The default value.";
}
To read and write the record we fill in the convert methods. This is how it is "sterilized".
@Override
public @NotNull ConfigurationSection convert() {
ConfigurationSection section = new MemoryConfigurationSection();
section.set(IDENTIFIER_KEY, this.identifier);
section.set(TestRecord.STRING_KEY, this.string);
section.set(TestRecord.BOOL_KEY, this.bool);
section.set(TestRecord.OBJECT_KEY, this.object);
section.set(TestRecord.OTHER_TABLE_IDENTIFIER, this.foreign);
return section;
}
@Override
public @NotNull TestRecord convert(@NotNull ConfigurationSection section) {
this.string = section.getString(TestRecord.STRING_KEY);
this.bool = section.getBoolean(TestRecord.BOOL_KEY);
this.object = section.getClass(TestRecord.OBJECT_KEY, ObjectTest.class);
this.foreign = section.getString(TestRecord.OTHER_TABLE_IDENTIFIER);
return this;
}
}