Last active
February 3, 2020 16:53
-
-
Save pizycki/ace6569f0e189b6bc34462d6fb039056 to your computer and use it in GitHub Desktop.
Open conn, store and simple query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://martendb.io | |
| open Marten | |
| [<CLIMutable>] | |
| type User = | |
| { Id : Guid | |
| Name : string } | |
| let store = | |
| let dbPass = "secret" | |
| let dbUser = "secret" | |
| let dbHost = "secret" | |
| let dbName = "secret" | |
| let dbPort = 5432 | |
| let connStr = sprintf "host=%s;database=%s;password=%s;username=%s;port=%d;sslmode=Require;Trust Server Certificate=true;" dbHost dbName dbPass dbUser dbPort | |
| //printfn "%s" connStr | |
| DocumentStore.For(fun x -> | |
| x.Storage.MappingFor(typeof<User>).Alias <- "users" // When scripting in LinqPad, every time you run your query, a new namespace is being generated. To be able to query stored documents we have to explicitly set their collection names. | |
| x.Connection(connStr) | |
| ) | |
| let session = store.OpenSession() | |
| // First, query for all existing documents | |
| let users = session.Query<User>().ToArray() | |
| printfn "%A" users | |
| // Secondly, put a new document to store | |
| let usr = | |
| { Id = Guid.Empty // Marten will replace ID with new one once "Store" method is called | |
| Name = "Issac" } | |
| session.Store(usr) | |
| session.SaveChanges() | |
| // When scripting, don't forget to dispose/close your connection as it'll stay open and Heroku only allows for 20 of these! | |
| session.Dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment