realm / realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
https://realm.io
Apache License 2.0
1.24k stars 162 forks source link

Support for Guid, Decimal and Enums #740

Closed sunniejai closed 3 years ago

sunniejai commented 8 years ago

Are there any plans to support Guid, Decimal, and Enums? Currently we're getting around Guid and Decimal by storing them as strings. and Enums by storing them as int, but having them supported would make the class more well defined.

AndyDentFree commented 8 years ago

What kind of queries would you want to run on these types?

We have ongoing discussions across the different teams of additional data types to support. As Realm is a multi-platform engine with support in many languages, we aim to provide uniform support of types.

Storing as a byte array would be more efficient for GUID and Decimal, if you only want to do equality searches. You can see an example of binary searches in SearchComparingByteArrays in SimpleLINQtests.cs. If you need sorting or comparative searches, storing as formatted strings is unfortunately your only option for now.

sunniejai commented 8 years ago

for Guid, i think just equality checking is ok. For decimal it'd be nice to be able to do comparative searches.

I think Guid is important if you use UUID's as the "ObjectId or primary key", however you want to call it. And decimal is important if you're working with currency.

It doesn't really matter so much how Realm stores it internally if you can define the classes like so. It adds alot more clarity to what the type is supposed to be:

public class Item : RealmObject
{
    public Guid ItemId { get; set; }
    public decimal Price { get; set; }
}

currently you'd have to hack it like so:

public class Item : RealmObject
{
    public string Itemid { get; set; }
    public string Price { get; set; }
}

or wrap it in another property:

public class Item : RealmObject
{
    //make sure to query using stored item id instead
    public string StoredItemId { get; set; }
    public Guid ItemId get { return new Guid(StoredItemid); } set { StoredItemId = value.ToString() }

    //make sure to query using stored price instead
   public string Price { get; set; }
   public decimal Price { get { return Decimal.Parse(StoredPrice); } set { StoredPrice = value.ToString(CurrentCulture.InvariantCulture); }
}
listepo commented 6 years ago

Hi, any news?

arminrasoulian commented 6 years ago

Are there any plans to support these?

nirinchev commented 6 years ago

We don't have immediate plans to support those. There is an easy workaround for guids and enums, and while we would like to get native decimal support, we have quite a few financial institutions as customers and they have not requested it, so barring a paid contract, it's unlikely it'll land before 2019.

karlingen commented 6 years ago

@nirinchev what’s the workaround for enums?

fealebenpae commented 6 years ago

@karlingen off the top of my head it should be something like this:

enum Color : int
{
    Red,
    Green,
    Blue
}

class Item : RealmObject
{
    private int Color_raw { get; set; }

    public Color Color
    {
        get { return (Color)Color_raw; }
        set { Color_raw = (int)value; }
    }
}
nirinchev commented 6 years ago

Yes - alternatively, you can store the raw value as a string if you prefer the readability (e.g. if you open the Realm in studio) or if you are worried about someone reordering the values accidentally.

karlingen commented 5 years ago

@fealebenpae @nirinchev Thanks to both of you!

I did this:

public enum Color
{
    Undefined,
    Red,
    Blue
}

class Item : RealmObject
{
    private string ColorRaw { get; set; }

    public Color Color
    {
        get { return Enum.TryParse(ColorRaw, out Color result) ? result : Color.Undefined; }
        set { ColorRaw = value.ToString(); }
    }
}
charlesroddie commented 4 years ago

Guid doesn't have a good workaround at the moment, since Realm doesn't allow byte[] as a PrimaryKey or Index property. So you have to store as a string, i.e. using even more storage than necessary for a datatype that is already very large.

Having good support for byte[] would fix this: there is no reason that byte[] shouldn't support all the functionality that string does.

nirinchev commented 3 years ago

Decimals are already supported and support for Guids was implemented recently, so the only item on this list left is supporting Enums. This is covered by https://github.com/realm/realm-dotnet/issues/549, so I'll close this issue.

peppy commented 3 years ago

Very happy to have Guid support in! Weirdly, I'm getting this fody error (Realm 10.0.0-beta3):

image

Is this an oversight or am I doing something incorrectly?

nirinchev commented 3 years ago

Support for Guids was merged but hasn't been released yet.

peppy commented 3 years ago

Right, thanks (thought it may have been due to the PR being merged before the recent beta release).

Will this be in the next beta release per chance?

nirinchev commented 3 years ago

It will happen to work, but not be officially supported. We expect to announce official support for it later this quarter.

softlion commented 2 years ago

It will happen to work, but not be officially supported. We expect to announce official support for it later this quarter.

Could you link the Project's task id so we have a status ?

nirinchev commented 2 years ago

Guids are already supported.