Initial extraction from other project

This commit is contained in:
Daniel J. Summers
2017-07-16 20:18:47 -05:00
parent 81c4890897
commit a54aa3ba0c
6 changed files with 320 additions and 16 deletions

View File

@@ -0,0 +1,95 @@
namespace RethinkDb.Driver.FSharp
open Newtonsoft.Json.Linq
open RethinkDb.Driver
open RethinkDb.Driver.Net
/// Parameters for the RethinkDB configuration
type DataConfigParameter =
| Hostname of string
| Port of int
| User of string * string
| AuthKey of string
| Timeout of int
| Database of string
/// RethinDB configuration
type DataConfig =
{ Parameters : DataConfigParameter list }
with
static member empty =
{ Parameters = [] }
/// Create a RethinkDB connection
member this.CreateConnection () : IConnection =
let folder (builder : Connection.Builder) block =
match block with
| Hostname x -> builder.Hostname x
| Port x -> builder.Port x
| User (x, y) -> builder.User (x, y)
| AuthKey x -> builder.AuthKey x
| Timeout x -> builder.Timeout x
| Database x -> builder.Db x
let bldr =
this.Parameters
|> Seq.fold folder (RethinkDB.R.Connection ())
upcast bldr.Connect ()
/// The effective hostname
member this.Hostname =
match this.Parameters
|> List.tryPick (fun x -> match x with Hostname _ -> Some x | _ -> None) with
| Some (Hostname x) -> x
| _ -> RethinkDBConstants.DefaultHostname
/// The effective port
member this.Port =
match this.Parameters
|> List.tryPick (fun x -> match x with Port _ -> Some x | _ -> None) with
| Some (Port x) -> x
| _ -> RethinkDBConstants.DefaultPort
/// The effective connection timeout
member this.Timeout =
match this.Parameters
|> List.tryPick (fun x -> match x with Timeout _ -> Some x | _ -> None) with
| Some (Timeout x) -> x
| _ -> RethinkDBConstants.DefaultTimeout
/// The effective database
member this.Database =
match this.Parameters
|> List.tryPick (fun x -> match x with Database _ -> Some x | _ -> None) with
| Some (Database x) -> x
| _ -> RethinkDBConstants.DefaultDbName
/// Parse settings from JSON
///
/// A sample JSON object with all the possible properties filled in:
/// {
/// "hostname" : "my-host",
/// "port" : 12345,
/// "username" : "my-user-name",
/// "password" : "my-password",
/// "auth-key" : "my-auth-key",
/// "timeout" : 77,
/// "database" : "default-db"
/// }
///
/// None of these properties are required, and properties not matching any of the above listed ones will be ignored.
static member FromJson json =
let isNotNull = not << isNull
let parsed = JObject.Parse json
let config =
seq {
match parsed.["hostname"] with x when isNotNull x -> yield Hostname <| x.Value<string> () | _ -> ()
match parsed.["port"] with x when isNotNull x -> yield Port <| x.Value<int> () | _ -> ()
match parsed.["auth-key"] with x when isNotNull x -> yield AuthKey <| x.Value<string> () | _ -> ()
match parsed.["timeout"] with x when isNotNull x -> yield Timeout <| x.Value<int> () | _ -> ()
match parsed.["database"] with x when isNotNull x -> yield Database <| x.Value<string> () | _ -> ()
let userName = parsed.["username"]
let password = parsed.["password"]
match isNotNull userName && isNotNull password with
| true -> yield User (userName.Value<string> (), password.Value<string> ())
| _ -> ()
}
|> List.ofSeq
{ Parameters = config }
/// Parse settings from a JSON text file
///
/// See doc for FromJson for the expected JSON format.
static member FromJsonFile = System.IO.File.ReadAllText >> DataConfig.FromJson

View File

@@ -0,0 +1,80 @@
[<AutoOpen>]
module RethinkDb.Driver.FSharp.Functions
open RethinkDb.Driver
let private r = RethinkDB.R
/// Get a connection builder that can be used to create one RethinkDB connection
let connection () =
r.Connection ()
/// Get the results of an expression
let asyncResult<'T> conn (expr : Ast.ReqlExpr) =
expr.RunResultAsync<'T> conn
|> Async.AwaitTask
/// Get the result of a non-select ReQL expression
let asyncReqlResult conn (expr : Ast.ReqlExpr) =
expr.RunResultAsync conn
|> Async.AwaitTask
/// Get a list of databases
let dbList conn =
r.DbList ()
|> asyncResult<string list> conn
/// Create a database
let dbCreate dbName conn =
r.DbCreate dbName
|> asyncReqlResult conn
/// Reference a database
let db dbName =
r.Db dbName
/// Reference the default database
let defaultDb =
(fun () -> r.Db ()) ()
/// Get a list of tables for the given database
let tableList conn (db : Ast.Db) =
db.TableList ()
|> asyncResult<string list> conn
/// Create a table in the given database
let tableCreate tableName conn (db : Ast.Db) =
db.TableCreate tableName
|> asyncReqlResult conn
/// Return all documents in a table (may be further refined)
let table tableName (db : Ast.Db) =
db.Table tableName
/// Return all documents in a table from the default database (may be further refined)
let fromTable tableName =
table tableName defaultDb
/// Get a list of indexes for the given table
let indexList conn (table : Ast.Table) =
table.IndexList ()
|> asyncResult<string list> conn
/// Create an index on the given table
let indexCreate indexName conn (table : Ast.Table) =
table.IndexCreate indexName
|> asyncReqlResult conn
/// Get a document by its primary key
let get documentId (table : Ast.Table) =
table.Get documentId
/// Get all documents matching keys in the given index
let getAll (ids : 'T seq) indexName (table : Ast.Table) =
table.GetAll(ids |> Array.ofSeq).OptArg("index", indexName)
/// Get a cursor with the results of an expression
let asyncCursor<'T> conn (expr : Ast.ReqlExpr) =
expr.RunCursorAsync<'T> conn
|> Async.AwaitTask

View File

@@ -0,0 +1,29 @@
<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard1.6</TargetFrameworks>
<Description>Idiomatic F# extentions to the official RethinkDB C# driver</Description>
<Authors>Daniel J. Summers</Authors>
<PackageLicenseUrl>https://github.com/danieljsummers/RethinkDb.Driver.FSharp/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/danieljsummers/RethinkDb.Driver.FSharp</PackageProjectUrl>
<!-- PackageIconUrl>https://github.com/danieljsummers/RethinkDb.Driver.FSharp/raw/master/icon/icon.png</PackageIconUrl -->
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>Alpha; use at your own risk</PackageReleaseNotes>
<Copyright>See LICENSE</Copyright>
<PackageTags>RethinkDB document F#</PackageTags>
<VersionPrefix>0.7.0</VersionPrefix>
<VersionSuffix>alpha-0001</VersionSuffix>
</PropertyGroup>
<ItemGroup>
<Compile Include="Functions.fs" />
<Compile Include="Config.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.2.*" />
<PackageReference Include="FSharp.NET.Sdk" Version="1.0.*" PrivateAssets="All" />
<PackageReference Include="RethinkDb.Driver" Version="2.*" />
</ItemGroup>
</Project>