Database - SynapseSL/Synapse GitHub Wiki
Synapse embeds a small document relational single-file database called LiteDB for you as a developer to access and use in your plugins. You can open and view your database using LiteDB.Studio. It doesn't require any setup up to since the framework uses this database for it's internal PlayerRepository, although you can disable the database in the Synapse Configuration
LiteDB uses POCOs as Entities. You have to implement the IDatabaseEntity Interface in order for your entity to be compatible with our API. An example entity could look like this:
public class PlayerDbo : IDatabaseEntity
{
//Required
public int Id { get; set; }
//Optional
public string Name { get; set; }
//Optional
public Dictionary<string,string> Data { get; set; }
//Makes the codestyle more readable for other devs
public int GetId() => Id;
}
As you see, you just have to include an Id-Int-Field and every other field will be managed by your plugin.
After you have created you POCO you'd have to create a Repository, which is just a class extending the parameterized class Repository, for your entity like that:
public class PlayerRepository : Repository<PlayerDbo>
{
//Example additional query method
public PlayerDbo FindByName(string name)
{
return Get(LiteDB.Query.EQ("Name", name));
}
}
As you see, you make custom query methods and not only use the ones already provided by the repository class. Here is a quick example showing how to use already existing methods:
void example()
{
//Use of the Query() method
var List = Repository.Query(queryable => queryable
.Where("Name", "Test")
.ToList()
);
//Insert a new entity
var player = Repository.Insert(new PlayerDbo()
{
Name = "Hanz",
Data = {{"Key1", "Value1"},{"OtherKey","OtherValue"}}
}
);
//Updates a entity
player.Name = "Gunter";
Repository.save(player);
//ID will be injected after calling Insert()
SynapseController.Server.Logger.Info(player.GetId());
//Deletes the entity
Repository.Delete(Player);
}
LiteDB will map all common C#-DataTypes to the corresponding type in BSON. It comes with a few standard converters, where objects will be further serialized and deserialized:
-
Int16, UInt16, Byte, SByte
: Int32 -
UInt32, UInt64
: Int64 -
Single
: Double -
Char, Enum
: String -
IList<T>
: Array -
T[]
: Array -
IDictionary<K,T>
: Document -
Any other .NET type
: Document