Compare commits
3 Commits
v0.9.0-b05
...
main
Author | SHA1 | Date | |
---|---|---|---|
a09181eee0 | |||
5d15de240d | |||
61e17038f8 |
|
@ -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
|
```fsharp
|
||||||
/// type: DataConfig
|
/// type: DataConfig
|
||||||
let config = DataConfig.fromJsonFile "data-config.json"
|
let config = DataConfig.fromJsonFile "data-config.json"
|
||||||
// OR
|
// OR
|
||||||
let config = DataConfig.fromConfiguration (config.GetSection "RethinkDB")
|
let config = DataConfig.fromConfiguration (config.GetSection "RethinkDB")
|
||||||
|
// OR
|
||||||
|
let config = DataConfig.fromUri (config.GetConnectionString "RethinkDB")
|
||||||
|
|
||||||
/// type: IConnection
|
/// type: IConnection
|
||||||
let conn = config.Connect ()
|
let conn = config.Connect ()
|
||||||
|
|
|
@ -80,6 +80,13 @@ type RethinkBuilder<'T> () =
|
||||||
| Some dbName, tblName -> this.Db (r, dbName) |> tableCreate tblName
|
| Some dbName, tblName -> this.Db (r, dbName) |> tableCreate tblName
|
||||||
| None, _ -> tableCreateInDefault tableName
|
| None, _ -> tableCreateInDefault tableName
|
||||||
|
|
||||||
|
/// Create a table, providing optional arguments (of form "dbName.tableName"; if no db name, uses default database)
|
||||||
|
[<CustomOperation "tableCreate">]
|
||||||
|
member this.TableCreate (r : RethinkDB, tableName : string, args : TableCreateOptArg list) =
|
||||||
|
match dbAndTable tableName with
|
||||||
|
| Some dbName, tblName -> this.Db (r, dbName) |> tableCreateWithOptArgs tblName args
|
||||||
|
| None, _ -> tableCreateInDefaultWithOptArgs tableName args
|
||||||
|
|
||||||
/// Drop a table (of form "dbName.tableName"; if no db name, uses default database)
|
/// Drop a table (of form "dbName.tableName"; if no db name, uses default database)
|
||||||
[<CustomOperation "tableDrop">]
|
[<CustomOperation "tableDrop">]
|
||||||
member this.TableDrop (r : RethinkDB, tableName : string) =
|
member this.TableDrop (r : RethinkDB, tableName : string) =
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
namespace RethinkDb.Driver.FSharp
|
namespace RethinkDb.Driver.FSharp
|
||||||
|
|
||||||
|
open System
|
||||||
open Microsoft.Extensions.Configuration
|
open Microsoft.Extensions.Configuration
|
||||||
|
open Microsoft.Extensions.Logging
|
||||||
open Newtonsoft.Json.Linq
|
open Newtonsoft.Json.Linq
|
||||||
open RethinkDb.Driver
|
open RethinkDb.Driver
|
||||||
open RethinkDb.Driver.Net
|
open RethinkDb.Driver.Net
|
||||||
|
@ -36,17 +38,30 @@ type DataConfig =
|
||||||
static member empty =
|
static member empty =
|
||||||
{ Parameters = [] }
|
{ Parameters = [] }
|
||||||
|
|
||||||
/// Create a RethinkDB connection
|
/// Build the connection from the given parameters
|
||||||
member this.CreateConnection () : IConnection =
|
member private this.BuildConnection () =
|
||||||
this.Parameters
|
this.Parameters
|
||||||
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
|
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
|
||||||
|> function builder -> builder.Connect ()
|
|
||||||
|
/// Create a RethinkDB connection
|
||||||
|
member this.CreateConnection () : IConnection =
|
||||||
|
(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
|
/// Create a RethinkDB connection
|
||||||
member this.CreateConnectionAsync () : Task<Connection> =
|
member this.CreateConnectionAsync () : Task<Connection> =
|
||||||
this.Parameters
|
(this.BuildConnection ()).ConnectAsync ()
|
||||||
|> Seq.fold ConnectionBuilder.build (RethinkDB.R.Connection ())
|
|
||||||
|> function builder -> builder.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
|
/// The effective hostname
|
||||||
member this.Hostname =
|
member this.Hostname =
|
||||||
|
@ -72,6 +87,20 @@ type DataConfig =
|
||||||
| Some (Database x) -> x
|
| Some (Database x) -> x
|
||||||
| _ -> RethinkDBConstants.DefaultDbName
|
| _ -> 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
|
/// Parse settings from JSON
|
||||||
///
|
///
|
||||||
/// A sample JSON object with all the possible properties filled in:
|
/// A sample JSON object with all the possible properties filled in:
|
||||||
|
@ -89,14 +118,14 @@ type DataConfig =
|
||||||
static member FromJson json =
|
static member FromJson json =
|
||||||
let parsed = JObject.Parse json
|
let parsed = JObject.Parse json
|
||||||
{ Parameters =
|
{ Parameters =
|
||||||
[ match parsed["hostname"] with null -> () | x -> Hostname <| x.Value<string> ()
|
[ match parsed["hostname"] with null -> () | x -> Hostname <| x.Value<string> ()
|
||||||
match parsed["port"] with null -> () | x -> Port <| x.Value<int> ()
|
match parsed["port"] with null -> () | x -> Port <| x.Value<int> ()
|
||||||
match parsed["auth-key"] with null -> () | x -> AuthKey <| x.Value<string> ()
|
match parsed["auth-key"] with null -> () | x -> AuthKey <| x.Value<string> ()
|
||||||
match parsed["timeout"] with null -> () | x -> Timeout <| x.Value<int> ()
|
match parsed["timeout"] with null -> () | x -> Timeout <| x.Value<int> ()
|
||||||
match parsed["database"] with null -> () | x -> Database <| x.Value<string> ()
|
match parsed["database"] with null -> () | x -> Database <| x.Value<string> ()
|
||||||
match parsed["username"], parsed["password"] with
|
match parsed["username"], parsed["password"] with
|
||||||
| null, _ | _, null -> ()
|
| null, _ | _, null -> ()
|
||||||
| userName, password -> User (userName.Value<string> (), password.Value<string> ())
|
| userName, password -> User (userName.Value<string> (), password.Value<string> ())
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,11 +137,34 @@ type DataConfig =
|
||||||
/// Parse settings from application configuration
|
/// Parse settings from application configuration
|
||||||
static member FromConfiguration (cfg : IConfigurationSection) =
|
static member FromConfiguration (cfg : IConfigurationSection) =
|
||||||
{ Parameters =
|
{ Parameters =
|
||||||
[ match cfg["hostname"] with null -> () | host -> Hostname host
|
[ match cfg["hostname"] with null -> () | host -> Hostname host
|
||||||
match cfg["port"] with null -> () | port -> Port (int port)
|
match cfg["port"] with null -> () | port -> Port (int port)
|
||||||
match cfg["auth-key"] with null -> () | key -> AuthKey key
|
match cfg["auth-key"] with null -> () | key -> AuthKey key
|
||||||
match cfg["timeout"] with null -> () | time -> Timeout (int time)
|
match cfg["timeout"] with null -> () | time -> Timeout (int time)
|
||||||
match cfg["database"] with null -> () | db -> Database db
|
match cfg["database"] with null -> () | db -> Database db
|
||||||
match cfg["username"], cfg["password"] with null, _ | _, null -> () | user -> User user
|
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..])
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -576,6 +576,14 @@ let tableCreate (tableName : string) (db : Db) =
|
||||||
let tableCreateInDefault (tableName : string) =
|
let tableCreateInDefault (tableName : string) =
|
||||||
r.TableCreate tableName
|
r.TableCreate tableName
|
||||||
|
|
||||||
|
/// Create a table in the connection-default database, providing optional arguments
|
||||||
|
let tableCreateInDefaultWithOptArgs (tableName : string) args =
|
||||||
|
r.TableCreate tableName |> TableCreateOptArg.apply args
|
||||||
|
|
||||||
|
/// Create a table in the given database, providing optional arguments
|
||||||
|
let tableCreateWithOptArgs (tableName : string) args (db : Db) =
|
||||||
|
db.TableCreate tableName |> TableCreateOptArg.apply args
|
||||||
|
|
||||||
/// Drop a table in the given database
|
/// Drop a table in the given database
|
||||||
let tableDrop (tableName : string) (db : Db) =
|
let tableDrop (tableName : string) (db : Db) =
|
||||||
db.TableDrop tableName
|
db.TableDrop tableName
|
||||||
|
|
|
@ -418,6 +418,12 @@ val tableCreate : string -> Db -> TableCreate
|
||||||
/// Create a table in the connection-default database
|
/// Create a table in the connection-default database
|
||||||
val tableCreateInDefault : string -> TableCreate
|
val tableCreateInDefault : string -> TableCreate
|
||||||
|
|
||||||
|
/// Create a table in the connection-default database, providing optional arguments
|
||||||
|
val tableCreateInDefaultWithOptArgs : string -> TableCreateOptArg list -> TableCreate
|
||||||
|
|
||||||
|
/// Create a table in the given database, providing optional arguments
|
||||||
|
val tableCreateWithOptArgs : string -> TableCreateOptArg list -> Db -> TableCreate
|
||||||
|
|
||||||
/// Drop a table in the given database
|
/// Drop a table in the given database
|
||||||
val tableDrop : string -> Db -> TableDrop
|
val tableDrop : string -> Db -> TableDrop
|
||||||
|
|
||||||
|
|
|
@ -299,6 +299,57 @@ module RunOptArg =
|
||||||
args
|
args
|
||||||
|
|
||||||
|
|
||||||
|
/// Definition of server tag/replica count
|
||||||
|
type ReplicaTag =
|
||||||
|
/// A tagged server replica, along with the number of replicas per shard for that server
|
||||||
|
| ReplicaTag of string * int
|
||||||
|
|
||||||
|
/// Definition of replicas per shard when creating a table
|
||||||
|
type ReplicaSpec =
|
||||||
|
/// Create this number of replicas per shard
|
||||||
|
| Number of int
|
||||||
|
/// Create the replicas across tagged servers, using the specified tag as the primary server
|
||||||
|
| WithTags of string * ReplicaTag list
|
||||||
|
|
||||||
|
/// Optional arguments for creating tables
|
||||||
|
type TableCreateOptArg =
|
||||||
|
/// The name of the primary key field (default is "id")
|
||||||
|
| PrimaryKey of string
|
||||||
|
/// The durability of the command
|
||||||
|
| Durability of Durability
|
||||||
|
/// The number of shards to create (1 to 64)
|
||||||
|
| Shards of int
|
||||||
|
/// The replicas per shard for this table
|
||||||
|
| Replicas of ReplicaSpec
|
||||||
|
|
||||||
|
/// Functions to support `tableCreate` optional arguments
|
||||||
|
module TableCreateOptArg =
|
||||||
|
|
||||||
|
/// Apply a list of optional arguments to a tableCreate statement
|
||||||
|
let apply opts (tc : TableCreate) =
|
||||||
|
opts
|
||||||
|
|> List.fold (fun (tc : TableCreate) arg ->
|
||||||
|
match arg with
|
||||||
|
| PrimaryKey pk -> tc.OptArg ("primary_key", pk)
|
||||||
|
| Durability dur -> tc.OptArg dur.reql
|
||||||
|
| Shards sh -> tc.OptArg ("shards", sh)
|
||||||
|
| Replicas rep ->
|
||||||
|
match rep with
|
||||||
|
| Number count -> tc.OptArg ("replicas", count)
|
||||||
|
| WithTags (primary, all) ->
|
||||||
|
let (ReplicaTag (firstTag, firstCount)) = List.head all
|
||||||
|
let replica =
|
||||||
|
all
|
||||||
|
|> List.skip 1
|
||||||
|
|> List.fold (fun (h : Model.MapObject) arg ->
|
||||||
|
let (ReplicaTag (tag, count)) = arg
|
||||||
|
h.With (tag, count))
|
||||||
|
(RethinkDB.R.HashMap (firstTag, firstCount))
|
||||||
|
tc.OptArg("replicas", replica).OptArg ("primary_replica_tag", primary)
|
||||||
|
)
|
||||||
|
tc
|
||||||
|
|
||||||
|
|
||||||
/// Optional arguments for the `update` statement
|
/// Optional arguments for the `update` statement
|
||||||
type UpdateOptArg =
|
type UpdateOptArg =
|
||||||
/// The durability of the command
|
/// The durability of the command
|
||||||
|
|
|
@ -10,6 +10,8 @@ open RethinkDb.Driver.FSharp
|
||||||
let dataCfg = DataConfig.fromJson "rethink-config.json"
|
let dataCfg = DataConfig.fromJson "rethink-config.json"
|
||||||
// - or -
|
// - or -
|
||||||
let dataCfg = DataConfig.fromConfiguration [config-section]
|
let dataCfg = DataConfig.fromConfiguration [config-section]
|
||||||
|
// - or -
|
||||||
|
let dataCfg = DataConfig.fromUri [connection-string]
|
||||||
|
|
||||||
let conn = dataCfg.CreateConnection () // IConnection
|
let conn = dataCfg.CreateConnection () // IConnection
|
||||||
```
|
```
|
||||||
|
|
|
@ -14,9 +14,9 @@
|
||||||
<Copyright>See LICENSE</Copyright>
|
<Copyright>See LICENSE</Copyright>
|
||||||
<PackageTags>RethinkDB document F#</PackageTags>
|
<PackageTags>RethinkDB document F#</PackageTags>
|
||||||
<VersionPrefix>0.9.0</VersionPrefix>
|
<VersionPrefix>0.9.0</VersionPrefix>
|
||||||
<VersionSuffix>beta-05</VersionSuffix>
|
<VersionSuffix>beta-07</VersionSuffix>
|
||||||
<PackageReleaseNotes>
|
<PackageReleaseNotes>
|
||||||
Add retry for cursor DSL operations
|
Add URI config option and logging CreateConnection overloads
|
||||||
</PackageReleaseNotes>
|
</PackageReleaseNotes>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user