Closed srlopez closed 5 years ago
@humhei How can I fix it? It's a problem of my code or it's an authentic issue?
In fsharp world mutable fields in record type is not recommanded(Not thread safe; I didn't see others to use it) What's your use case of it?
Create a new record instead of setting property mutable
open System
open LiteDB
open LiteDB.FSharp
[<StructuredFormatDisplay("a{SizeGb}GB")>]
[<CLIMutable>]
type Disk =
{ SizeGb : int }
[<StructuredFormatDisplay("Computer #{Id}: {Manufacturer}/{DiskCount}/{Disks}")>]
[<CLIMutable>]
type Computer =
{ Id: int
Manufacturer: string
Disks: Disk list }
[<BsonIgnore>]
member __.DiskCount = __.Disks.Length // property calculated
[<EntryPoint>]
let main argv =
let myPc =
{ Id = 0
Manufacturer = "Computers Inc."
Disks =
[ { SizeGb = 100 }
{ SizeGb = 250 }
{ SizeGb = 500 } ] }
printfn "%A" myPc
let mapper = FSharpBsonMapper()
use db = new LiteDatabase("data.db", mapper)
let computers = db.GetCollection<Computer>("computers")
let myDellPc =
{myPc with Manufacturer = "Dell" }
computers.Insert(myDellPc) |> ignore
...
Hmm even though it is not recommended, I don't see an issue in supporting mutable
constructs in the library, I will work on it...
Hmm even though it is not recommended, I don't see an issue in supporting
mutable
constructs in the library, I will work on it...
And [<BsonIgnore>]
and [<BsonField>]
too, please
I am afraid that [<BsonIgnore>]
doesn't really make sense within F# records. Using a member within the record is the way to go as it will be used to compute properties derived from the record fields and doesn't get persisted (= ignored) as follows:
[<CLIMutable>]
type Computer =
{ Id: int
Manufacturer: string
Disks: Disk list }
// this is not persisted
with member this.Ignored = sprintf "%d => %s" this.Id this.Manufacturer
Aren't these members good enough for your use-case?
we can continue discussion at #26
As for [<BsonField>]
I think it is possible but will require more work, I will open another issue for it.
Fixed and released in version 2.8.0
to nuget, should be available really soon when nuget does it's magic. Now I decided to take control of how the fields are marshalled, so now these mutable records will work just fine :smile:
The
mutable
attribute avoids insert in LiteDB, or passing through Json's encoder, something happens that the name of the field is modified.The field you are trying to modify
Manufacturer
seems to change its nameManufacturer@'
The full error is:Thanks