SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Implement column aliases in C# and Rust modules

Open cloutiertyler opened this issue 11 months ago • 2 comments

We'd like to be able to do something like this:

// We're using this table as a singleton, so in this table
// there only be one element where the `id` is 0.
[Table(Name = "config", Public = true)]
public partial struct Config
{
    [PrimaryKey]
    [Column(Name = "id")]
    public uint Id;
    [Column(Name = "world_size")]
    public ulong WorldSize;
}
// We're using this table as a singleton, so in this table
// there only be one element where the `id` is 0.
#[spacetimedb::table(name = config, public)]
pub struct Config {
    #[primary_key]
    pub id: u32,
    #[column(name = "foobar")]
    pub foobar: u64,
}

Exactly spelling not proposed. Needs design work. This is a common feature in ORMs:

https://orm.drizzle.team/docs/sql-schema-declaration#tables-and-columns-declaration

cloutiertyler avatar Jan 24 '25 15:01 cloutiertyler

FWIW we already use the standard [DataMember(Name = "...")] attribute in autogenerated C# code to preserve column names for JSON representation (which was important for BitCraft):

https://github.com/clockworklabs/SpacetimeDB/blob/729dbb149599b4535c3b4d4e2fb4594be7ea448f/crates/cli/tests/snapshots/codegen__codegen_csharp.snap#L110-L119

In C# it might make sense to reuse the same attribute for server-side as well, instead of introducing a separate one, since DataMember can be reused by various serializers.

RReverser avatar Jan 27 '25 14:01 RReverser

The motivation for this ticket is that it's not now currently possible to follow SQL table/column name style suggestions and also not get warning in C# about not using snake_case names for variables.

(NICHE) It also makes it impossible to migrate a Rust project to C# and vice versa without changing column names which is not currently possible.

cloutiertyler avatar Apr 07 '25 19:04 cloutiertyler