DataSmith 0.1.2
Loading...
Searching...
No Matches
Database Support

DataSmith supports external databases through a pluggable provider system.

Instead of hardcoding a specific database engine, DataSmith communicates with databases through the IDatabase interface. This allows you to switch between storage backends without changing your model code.


๐Ÿง  Overview

Database-backed models use ModelValueType.DB.

These models:

  • Persist data outside of memory
  • Can execute queries through a database provider
  • Support large datasets
  • Enable server-backed or persistent storage
  • Are independent of a specific SQL implementation

๐Ÿ—๏ธ Enabling Database Models

Mark your model with ModelValueType.DB.

[GameModel(ModelValueType.DB)]
public class AccountData
{
[PrimaryKey]
public int AccountId;
public string Username;
public int Coins;
}
Definition GameModel.cs:8
Definition DataSmithGenerator.cs:13

Generated models will use the active database provider for storage and queries.


๐Ÿ”Œ Database Provider System

DataSmith communicates with databases using the IDatabase interface.

A provider implements this interface and executes commands against a specific backend.

Example Interface

public interface IDatabase
{
IEnumerable<T> Query<T>(string commandText);
int Execute(string commandText);
}

The exact interface may vary depending on the provider implementation.

โš™๏ธ Configuring a Provider

The active database provider is configured through the DataContext.

DataContext.Database = new SqliteDatabase(connectionString);

Once configured, all DB models will use this provider automatically.


๐Ÿงช Built-In SQLite Support

DataSmith can work with Mono.Data.Sqlite when available.

SQLite is ideal for:

  • Local save data
  • Offline applications
  • Prototyping
  • Small to medium datasets

Requirements

  • Mono.Data.Sqlite.dll present in the project
  • Compatible Unity scripting backend

Custom Database Providers

You can implement support for any database system by creating your own provider.

Example: Custom Provider

public class MyDatabase : IDatabase
{
public IEnumerable<T> Query<T>(string commandText)
{
// Execute query and map results
}
public int Execute(string commandText)
{
// Execute non-query command
}
}

Then register it:

DataContext.Database = new MyDatabase();

DataContext.Database = new MyDatabase();

๐Ÿ”Ž Database Queries

For DB models, generated queries may be translated into database commands instead of in-memory filtering.

Example usage:

var richPlayers =
DataContext.Get<AccountDataModel>()
.Query()
.CoinsGreaterThan(1000)
.Execute();

The provider determines how this query is executed.


๐Ÿ’พ Persistence Behavior

Unlike in-memory models:

  • Data is stored externally
  • Changes may be committed immediately or batched
  • Behavior depends on the provider implementation