diff --git a/src/RethinkDb.Driver.FSharp/Builder.fs b/src/RethinkDb.Driver.FSharp/Builder.fs index 23ed414..07f4527 100644 --- a/src/RethinkDb.Driver.FSharp/Builder.fs +++ b/src/RethinkDb.Driver.FSharp/Builder.fs @@ -80,6 +80,13 @@ type RethinkBuilder<'T> () = | Some dbName, tblName -> this.Db (r, dbName) |> tableCreate tblName | None, _ -> tableCreateInDefault tableName + /// Create a table, providing optional arguments (of form "dbName.tableName"; if no db name, uses default database) + [] + 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) [] member this.TableDrop (r : RethinkDB, tableName : string) = diff --git a/src/RethinkDb.Driver.FSharp/Functions.fs b/src/RethinkDb.Driver.FSharp/Functions.fs index 3f2e1c6..bae1daa 100644 --- a/src/RethinkDb.Driver.FSharp/Functions.fs +++ b/src/RethinkDb.Driver.FSharp/Functions.fs @@ -576,6 +576,14 @@ let tableCreate (tableName : string) (db : Db) = let tableCreateInDefault (tableName : string) = 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 let tableDrop (tableName : string) (db : Db) = db.TableDrop tableName diff --git a/src/RethinkDb.Driver.FSharp/Functions.fsi b/src/RethinkDb.Driver.FSharp/Functions.fsi index f61dd17..ebc2c0f 100644 --- a/src/RethinkDb.Driver.FSharp/Functions.fsi +++ b/src/RethinkDb.Driver.FSharp/Functions.fsi @@ -418,6 +418,12 @@ val tableCreate : string -> Db -> TableCreate /// Create a table in the connection-default database 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 val tableDrop : string -> Db -> TableDrop diff --git a/src/RethinkDb.Driver.FSharp/OptArgs.fs b/src/RethinkDb.Driver.FSharp/OptArgs.fs index 7e12af6..258f38d 100644 --- a/src/RethinkDb.Driver.FSharp/OptArgs.fs +++ b/src/RethinkDb.Driver.FSharp/OptArgs.fs @@ -299,6 +299,57 @@ module RunOptArg = 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 type UpdateOptArg = /// The durability of the command diff --git a/src/RethinkDb.Driver.FSharp/RethinkDb.Driver.FSharp.fsproj b/src/RethinkDb.Driver.FSharp/RethinkDb.Driver.FSharp.fsproj index 0693a10..cbd930d 100644 --- a/src/RethinkDb.Driver.FSharp/RethinkDb.Driver.FSharp.fsproj +++ b/src/RethinkDb.Driver.FSharp/RethinkDb.Driver.FSharp.fsproj @@ -14,9 +14,9 @@ See LICENSE RethinkDB document F# 0.9.0 - beta-05 + beta-06 - Add retry for cursor DSL operations + Add optional arguments for tableCreate