|
| 1 | +# How Do I Use LiteDB? |
| 2 | +The default template uses in-memory storage. This recipe will show you how to replace the in-memory storage with [LiteDB](https://github.com/mbdavid/LiteDB) in the form of [LiteDB.FSharp](https://github.com/Zaid-Ajaj/LiteDB.FSharp). |
| 3 | + |
| 4 | +#### 1. Add LiteDB.FSharp |
| 5 | +Add the [LiteDB.FSharp](https://www.nuget.org/packages/LiteDB.FSharp/) NuGet package to the [server project](./../package-management/add-nuget-package-to-server.md). |
| 6 | + |
| 7 | +#### 2. Create the database |
| 8 | +Replace the use of the `ResizeArray` in the `Storage` type with a database and collection: |
| 9 | + |
| 10 | +```fsharp |
| 11 | +open LiteDB.FSharp |
| 12 | +open LiteDB |
| 13 | +
|
| 14 | +type Storage () = |
| 15 | + let database = |
| 16 | + let mapper = FSharpBsonMapper() |
| 17 | + let connStr = "Filename=Todo.db;mode=Exclusive" |
| 18 | + new LiteDatabase (connStr, mapper) |
| 19 | + let todos = database.GetCollection<Todo> "todos" |
| 20 | +``` |
| 21 | + |
| 22 | +> LiteDb is a file-based database, and will create the file if it does not exist automatically. |
| 23 | +
|
| 24 | +This will create a database file `Todo.db` in the `Server` folder. The option `mode=Exclusive` is added for MacOS support (see this [issue](https://github.com/mbdavid/LiteDB/issues/787)). |
| 25 | + |
| 26 | +> See [here](https://www.litedb.org/docs/connection-string/) for more information on connection string arguments. |
| 27 | +
|
| 28 | +> See the [official docs](https://www.litedb.org/docs) for details on constructor arguments. |
| 29 | +
|
| 30 | +#### 3. Implement the rest of the repository |
| 31 | +Replace the implementations of `GetTodos` and `AddTodo` as follows: |
| 32 | + |
| 33 | +```fsharp |
| 34 | + /// Retrieves all todo items. |
| 35 | + member _.GetTodos () = |
| 36 | + todos.FindAll () |> List.ofSeq |
| 37 | +
|
| 38 | + /// Tries to add a todo item to the collection. |
| 39 | + member _.AddTodo (todo:Todo) = |
| 40 | + if Todo.isValid todo.Description then |
| 41 | + todos.Insert todo |> ignore |
| 42 | + Ok () |
| 43 | + else |
| 44 | + Error "Invalid todo" |
| 45 | +``` |
| 46 | + |
| 47 | +#### 4. Initialise the database |
| 48 | +Modify the existing "priming" so that it first checks if there are any records in the database before inserting data: |
| 49 | + |
| 50 | +```fsharp |
| 51 | +if storage.GetTodos() |> Seq.isEmpty then |
| 52 | + storage.AddTodo(Todo.create "Create new SAFE project") |> ignore |
| 53 | + storage.AddTodo(Todo.create "Write your app") |> ignore |
| 54 | + storage.AddTodo(Todo.create "Ship it !!!") |> ignore |
| 55 | +``` |
| 56 | + |
| 57 | +#### 5. Make Todo compatible with LiteDb |
| 58 | +Add the [CLIMutable](https://github.com/MicrosoftDocs/visualfsharpdocs/blob/master/docs/conceptual/core.climutableattribute-class-%5Bfsharp%5D.md) attribute to the `Todo` record in `Shared.fs` |
| 59 | + |
| 60 | +```fsharp |
| 61 | +[<CLIMutable>] |
| 62 | +type Todo = |
| 63 | + { Id : Guid |
| 64 | + Description : string } |
| 65 | +``` |
| 66 | + |
| 67 | +> This is required to allow LiteDB to hydrate (read) data into F# records. |
| 68 | +
|
| 69 | +#### All Done! |
| 70 | +* Run the application. |
| 71 | +* You will see that a database has been created in the Server folder and that you are presented with the standard TODO list. |
| 72 | +* Add an item and restart the application; observe that your data is still there. |
0 commit comments