Skip to content

Commit 6bdbbf9

Browse files
committed
add records + anonymous records to the sample
1 parent dd927af commit 6bdbbf9

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

FSharp/FSharpRepoDB/FSharpRepoDB/Program.fs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
// Learn more about F# at http://fsharp.org
22

3-
open FSharpRepoDB
3+
open System
44
open Microsoft.Data.SqlClient
55
open RepoDb
66
open RepoDb.Extensions
7-
open System
7+
open FSharpRepoDB.Types
8+
9+
let url (argv: string array) =
10+
let getFromEnv =
11+
System.Environment.GetEnvironmentVariable("MSSQL_DB_URL")
12+
|> Option.ofObj
13+
14+
let fromArgsOrEnv =
15+
argv
16+
|> Array.tryFind(fun arg -> arg.Contains "--dburl=")
17+
|> Option.map (fun f -> f.Split('=') |> Seq.tryLast)
18+
|> Option.flatten
19+
|> Option.orElse getFromEnv
20+
21+
fromArgsOrEnv |> Option.defaultValue "Server=127.0.0.1;Database=TestDB;User Id=sa;Password=Password1!;"
822

923
[<EntryPoint>]
1024
let main argv =
@@ -13,20 +27,21 @@ let main argv =
1327
SqlServerBootstrap.Initialize()
1428

1529
// Open the connection
16-
let connection = (new SqlConnection("Server=.;Database=TestDB;Integrated Security=SSPI;")).EnsureOpen()
30+
let connection = (new SqlConnection(url argv)).EnsureOpen()
1731

1832
// Get the fields
1933
let personType = typedefof<Person>
2034
let dbFields = DbFieldCache.Get(connection, "Person", null).AsList()
2135
let fields = FieldCache.Get(personType).AsList()
2236

23-
// Create the type
24-
let person = new Person (Convert.ToInt64(0), "John Doe", 32, "New York", true)
37+
let person = { Id = 0L; Name = "John Doe"; Address = "New York"; Age = 32; IsActive = true }
2538

2639
// Insert (Generic-Based)
2740
let id = connection.Insert<Person, int64>(person)
2841
Console.WriteLine(Convert.ToString(id))
2942

43+
// Create PersonLike Record the type
44+
let person = {| Name = "John Doe"; Age = 32; Address = "New York"; IsActive = true|}
3045
// Insert (Table-Based)
3146
let id = connection.Insert<int64>(ClassMappedNameCache.Get<Person>(), person)
3247
Console.WriteLine(Convert.ToString(id))

0 commit comments

Comments
 (0)