Embedded per-dialect migrations applied in one transaction and recorded in schema_migrations (advisory lock on PostgreSQL, newer schemas refused). Schema for every BRIEF section 5 entity plus import history and uploads. Hand-written repositories for archives, sequences (filters, keyset paging, Archive statistics), frames (bulk insert and updates), renders, jobs (exclusive claiming, checkpoints, crash recovery), media cards, users, tokens, render nodes, settings, tags, audit and uploads. TitanoDatabase opens, migrates and exposes them, also bound to a transaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
94 lines
5.1 KiB
C#
94 lines
5.1 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization.Metadata;
|
|
|
|
namespace Titano.Data.Repositories;
|
|
|
|
/// <summary>Who a setting belongs to.</summary>
|
|
public enum SettingScope
|
|
{
|
|
/// <summary>The installation (client app or server); no scope id.</summary>
|
|
App,
|
|
|
|
/// <summary>One archive; the scope id is the archive id.</summary>
|
|
Archive,
|
|
|
|
/// <summary>One user; the scope id is the user id.</summary>
|
|
User,
|
|
}
|
|
|
|
/// <summary>Typed key/value settings stored as JSON (BRIEF §5 Setting).</summary>
|
|
public sealed class SettingsRepository
|
|
{
|
|
private static readonly string UpsertSql = SqlDialect.Upsert("settings", ["scope", "scope_id", "key", "value_json", "updated_at"], 3);
|
|
|
|
private readonly DbAccess _db;
|
|
|
|
internal SettingsRepository(DbAccess db) => _db = db;
|
|
|
|
/// <summary>The raw JSON of a setting, or null when unset.</summary>
|
|
public Task<string?> GetJsonAsync(SettingScope scope, Guid? scopeId, string key, CancellationToken cancellationToken = default) =>
|
|
_db.ScalarAsync<string>("SELECT value_json FROM settings WHERE scope = $1 AND scope_id = $2 AND key = $3",
|
|
[ScopeName(scope), ScopeId(scope, scopeId), key], cancellationToken);
|
|
|
|
/// <summary>Stores a JSON value; malformed JSON is rejected on both dialects.</summary>
|
|
public async Task SetJsonAsync(SettingScope scope, Guid? scopeId, string key, string json, CancellationToken cancellationToken = default)
|
|
{
|
|
using (JsonDocument.Parse(json))
|
|
{
|
|
}
|
|
await _db.ExecuteAsync(UpsertSql, [ScopeName(scope), ScopeId(scope, scopeId), key, DbParam.Json(json), DateTimeOffset.UtcNow], cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>Reads a setting with source-generated metadata (trim/AOT safe); <c>default</c> when unset.</summary>
|
|
public async Task<T?> GetAsync<T>(SettingScope scope, Guid? scopeId, string key, JsonTypeInfo<T> typeInfo, CancellationToken cancellationToken = default) =>
|
|
await GetJsonAsync(scope, scopeId, key, cancellationToken).ConfigureAwait(false) is { } json
|
|
? JsonSerializer.Deserialize(json, typeInfo)
|
|
: default;
|
|
|
|
public Task SetAsync<T>(SettingScope scope, Guid? scopeId, string key, T value, JsonTypeInfo<T> typeInfo, CancellationToken cancellationToken = default) =>
|
|
SetJsonAsync(scope, scopeId, key, JsonSerializer.Serialize(value, typeInfo), cancellationToken);
|
|
|
|
/// <summary>Reads a setting through reflection-based serialization; <c>default</c> when unset.</summary>
|
|
[RequiresUnreferencedCode("Reflection-based JSON; use the JsonTypeInfo overload in trimmed builds.")]
|
|
[RequiresDynamicCode("Reflection-based JSON; use the JsonTypeInfo overload in AOT builds.")]
|
|
public async Task<T?> GetAsync<T>(SettingScope scope, Guid? scopeId, string key, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) =>
|
|
await GetJsonAsync(scope, scopeId, key, cancellationToken).ConfigureAwait(false) is { } json
|
|
? JsonSerializer.Deserialize<T>(json, options)
|
|
: default;
|
|
|
|
[RequiresUnreferencedCode("Reflection-based JSON; use the JsonTypeInfo overload in trimmed builds.")]
|
|
[RequiresDynamicCode("Reflection-based JSON; use the JsonTypeInfo overload in AOT builds.")]
|
|
public Task SetAsync<T>(SettingScope scope, Guid? scopeId, string key, T value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) =>
|
|
SetJsonAsync(scope, scopeId, key, JsonSerializer.Serialize(value, options), cancellationToken);
|
|
|
|
public async Task<bool> DeleteAsync(SettingScope scope, Guid? scopeId, string key, CancellationToken cancellationToken = default) =>
|
|
await _db.ExecuteAsync("DELETE FROM settings WHERE scope = $1 AND scope_id = $2 AND key = $3",
|
|
[ScopeName(scope), ScopeId(scope, scopeId), key], cancellationToken).ConfigureAwait(false) > 0;
|
|
|
|
/// <summary>Every setting of a scope as key → JSON.</summary>
|
|
public async Task<IReadOnlyDictionary<string, string>> ListAsync(SettingScope scope, Guid? scopeId, CancellationToken cancellationToken = default)
|
|
{
|
|
var rows = await _db.ListAsync("SELECT key, value_json FROM settings WHERE scope = $1 AND scope_id = $2 ORDER BY key",
|
|
[ScopeName(scope), ScopeId(scope, scopeId)], r => KeyValuePair.Create(r.GetString(0), r.GetString(1)), cancellationToken).ConfigureAwait(false);
|
|
return new Dictionary<string, string>(rows, StringComparer.Ordinal);
|
|
}
|
|
|
|
private static string ScopeName(SettingScope scope) => scope switch
|
|
{
|
|
SettingScope.App => "app",
|
|
SettingScope.Archive => "archive",
|
|
SettingScope.User => "user",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(scope), scope, null),
|
|
};
|
|
|
|
private static string ScopeId(SettingScope scope, Guid? scopeId) => (scope, scopeId) switch
|
|
{
|
|
(SettingScope.App, null) => string.Empty,
|
|
(SettingScope.App, _) => throw new ArgumentException("App settings have no scope id.", nameof(scopeId)),
|
|
(_, { } id) => TextValues.FormatGuid(id),
|
|
_ => throw new ArgumentException($"{scope} settings need a scope id.", nameof(scopeId)),
|
|
};
|
|
}
|