Compare commits

..

2 Commits

11 changed files with 80 additions and 159 deletions

View File

@ -41,13 +41,15 @@ let fetchPost (postId : string) =
}
```
### A standard way to translate JSON into a strongly-typed configuration
### A standard way to translate JSON or a URI into a strongly-typed configuration
```fsharp
/// type: DataConfig
let config = DataConfig.fromJsonFile "data-config.json"
// OR
let config = DataConfig.fromConfiguration (config.GetSection "RethinkDB")
// OR
let config = DataConfig.fromUri (config.GetConnectionString "RethinkDB")
/// type: IConnection
let conn = config.Connect ()

View File

@ -1,25 +0,0 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@ -1,23 +0,0 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["RethinkDb.Driver.FSharp.Tests/RethinkDb.Driver.FSharp.Tests.fsproj", "RethinkDb.Driver.FSharp.Tests/"]
RUN dotnet restore "RethinkDb.Driver.FSharp.Tests/RethinkDb.Driver.FSharp.Tests.fsproj"
COPY . .
WORKDIR "/src/RethinkDb.Driver.FSharp.Tests"
RUN dotnet build "RethinkDb.Driver.FSharp.Tests.fsproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RethinkDb.Driver.FSharp.Tests.fsproj" -c Release -r linux-x64 --self-contained true -o /app/publish
FROM rethinkdb:latest AS test
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["RethinkDb.Driver.FSharp.Tests"]
# FROM base AS final
# WORKDIR /app
# COPY --from=publish /app/publish .
# ENTRYPOINT ["dotnet", "RethinkDb.Driver.FSharp.Tests.dll"]

View File

@ -1 +0,0 @@
module Program = let [<EntryPoint>] main _ = 0

View File

@ -1,39 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<Compile Include="Types.fs" />
<Compile Include="Tests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include=".dockerignore" />
<Content Include="Dockerfile" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RethinkDb.Driver.FSharp\RethinkDb.Driver.FSharp.fsproj" />
</ItemGroup>
</Project>

View File

@ -1,17 +0,0 @@
module Tests
open System
open RethinkDb.Driver
open RethinkDb.Driver.FSharp.Functions
open Types
open Xunit
let private r = RethinkDB.R
[<Fact>]
let ``My test`` () =
Assert.True(true)
let ``dbCreate succeeds`` () =
dbCreate "fsharp_driver_test"
|> runWrite

View File

@ -1,16 +0,0 @@
module Types
/// A simple type to use for testing
type BasicType =
{ // The ID for this record
id : string
/// A field with a boolean value
boolField : bool
/// A field with an integer value
intField : int
/// A field with a string value
stringField : string
}

View File

@ -4,8 +4,6 @@ VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 15.0.26124.0
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "RethinkDb.Driver.FSharp", "RethinkDb.Driver.FSharp\RethinkDb.Driver.FSharp.fsproj", "{9026E009-55DC-454E-87B7-39B9CBAD8BF8}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "RethinkDb.Driver.FSharp.Tests", "RethinkDb.Driver.FSharp.Tests\RethinkDb.Driver.FSharp.Tests.fsproj", "{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -28,18 +26,6 @@ Global
{9026E009-55DC-454E-87B7-39B9CBAD8BF8}.Release|x64.Build.0 = Release|Any CPU
{9026E009-55DC-454E-87B7-39B9CBAD8BF8}.Release|x86.ActiveCfg = Release|Any CPU
{9026E009-55DC-454E-87B7-39B9CBAD8BF8}.Release|x86.Build.0 = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|x64.ActiveCfg = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|x64.Build.0 = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|x86.ActiveCfg = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Debug|x86.Build.0 = Debug|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|Any CPU.Build.0 = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|x64.ActiveCfg = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|x64.Build.0 = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|x86.ActiveCfg = Release|Any CPU
{FC062B8C-81D5-4E0A-B3BA-A84541298BDB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,6 +1,8 @@
namespace RethinkDb.Driver.FSharp
open System
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Logging
open Newtonsoft.Json.Linq
open RethinkDb.Driver
open RethinkDb.Driver.Net
@ -35,18 +37,31 @@ type DataConfig =
/// An empty configuration
static member empty =
{ Parameters = [] }
/// Build the connection from the given parameters
member private this.BuildConnection () =
this.Parameters
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
/// Create a RethinkDB connection
member this.CreateConnection () : IConnection =
this.Parameters
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
|> function builder -> builder.Connect ()
(this.BuildConnection ()).Connect ()
/// Create a RethinkDB connection, logging the connection settings
member this.CreateConnection (log : ILogger) : IConnection =
let builder = this.BuildConnection ()
if not (isNull log) then log.LogInformation $"Connecting to {this.EffectiveUri}"
builder.Connect ()
/// Create a RethinkDB connection
member this.CreateConnectionAsync () : Task<Connection> =
this.Parameters
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
|> function builder -> builder.ConnectAsync ()
(this.BuildConnection ()).ConnectAsync ()
/// Create a RethinkDB connection, logging the connection settings
member this.CreateConnectionAsync (log : ILogger) : Task<Connection> =
let builder = this.BuildConnection ()
if not (isNull log) then log.LogInformation $"Connecting to {this.EffectiveUri}"
builder.ConnectAsync ()
/// The effective hostname
member this.Hostname =
@ -72,6 +87,20 @@ type DataConfig =
| Some (Database x) -> x
| _ -> RethinkDBConstants.DefaultDbName
/// The effective configuration URI (excludes password / auth key)
member this.EffectiveUri =
seq {
"rethinkdb://"
match this.Parameters |> List.tryPick (fun x -> match x with User _ -> Some x | _ -> None) with
| Some (User (username, _)) -> $"{username}:***pw***@"
| _ ->
match this.Parameters |> List.tryPick (fun x -> match x with AuthKey _ -> Some x | _ -> None) with
| Some (AuthKey _) -> "****key****@"
| _ -> ()
$"{this.Hostname}:{this.Port}/{this.Database}?timeout={this.Timeout}"
}
|> String.concat ""
/// Parse settings from JSON
///
/// A sample JSON object with all the possible properties filled in:
@ -89,14 +118,14 @@ type DataConfig =
static member FromJson json =
let parsed = JObject.Parse json
{ Parameters =
[ match parsed["hostname"] with null -> () | x -> Hostname <| x.Value<string> ()
match parsed["port"] with null -> () | x -> Port <| x.Value<int> ()
match parsed["auth-key"] with null -> () | x -> AuthKey <| x.Value<string> ()
match parsed["timeout"] with null -> () | x -> Timeout <| x.Value<int> ()
match parsed["database"] with null -> () | x -> Database <| x.Value<string> ()
match parsed["username"], parsed["password"] with
| null, _ | _, null -> ()
| userName, password -> User (userName.Value<string> (), password.Value<string> ())
[ match parsed["hostname"] with null -> () | x -> Hostname <| x.Value<string> ()
match parsed["port"] with null -> () | x -> Port <| x.Value<int> ()
match parsed["auth-key"] with null -> () | x -> AuthKey <| x.Value<string> ()
match parsed["timeout"] with null -> () | x -> Timeout <| x.Value<int> ()
match parsed["database"] with null -> () | x -> Database <| x.Value<string> ()
match parsed["username"], parsed["password"] with
| null, _ | _, null -> ()
| userName, password -> User (userName.Value<string> (), password.Value<string> ())
]
}
@ -108,11 +137,34 @@ type DataConfig =
/// Parse settings from application configuration
static member FromConfiguration (cfg : IConfigurationSection) =
{ Parameters =
[ match cfg["hostname"] with null -> () | host -> Hostname host
match cfg["port"] with null -> () | port -> Port (int port)
match cfg["auth-key"] with null -> () | key -> AuthKey key
match cfg["timeout"] with null -> () | time -> Timeout (int time)
match cfg["database"] with null -> () | db -> Database db
match cfg["username"], cfg["password"] with null, _ | _, null -> () | user -> User user
[ match cfg["hostname"] with null -> () | host -> Hostname host
match cfg["port"] with null -> () | port -> Port (int port)
match cfg["auth-key"] with null -> () | key -> AuthKey key
match cfg["timeout"] with null -> () | time -> Timeout (int time)
match cfg["database"] with null -> () | db -> Database db
match cfg["username"], cfg["password"] with null, _ | _, null -> () | user -> User user
]
}
/// Parse settings from a URI
///
/// rethinkdb://user:password@host:port/database?timeout=##
/// OR
/// rethinkdb://authkey@host:port/database?timeout=##
///
/// Scheme and host are required; all other settings optional
static member FromUri (uri : string) =
let it = Uri uri
if it.Scheme <> "rethinkdb" then invalidArg "Scheme" $"""URI scheme must be "rethinkdb" (was {it.Scheme})"""
{ Parameters =
[ Hostname it.Host
if it.Port <> -1 then Port it.Port
if it.UserInfo <> "" then
if it.UserInfo.Contains ":" then
let parts = it.UserInfo.Split ':' |> Array.truncate 2
User (parts[0], parts[1])
else AuthKey it.UserInfo
if it.Segments.Length > 1 then Database it.Segments[1]
if it.Query.Contains "?timeout=" then Timeout (int it.Query[9..])
]
}

View File

@ -10,6 +10,8 @@ open RethinkDb.Driver.FSharp
let dataCfg = DataConfig.fromJson "rethink-config.json"
// - or -
let dataCfg = DataConfig.fromConfiguration [config-section]
// - or -
let dataCfg = DataConfig.fromUri [connection-string]
let conn = dataCfg.CreateConnection () // IConnection
```

View File

@ -14,9 +14,9 @@
<Copyright>See LICENSE</Copyright>
<PackageTags>RethinkDB document F#</PackageTags>
<VersionPrefix>0.9.0</VersionPrefix>
<VersionSuffix>beta-06</VersionSuffix>
<VersionSuffix>beta-07</VersionSuffix>
<PackageReleaseNotes>
Add optional arguments for tableCreate
Add URI config option and logging CreateConnection overloads
</PackageReleaseNotes>
</PropertyGroup>