From 835501d7e5ecb558d66b6d37736794c2c068556f Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 19 Apr 2022 12:49:31 -0400 Subject: [PATCH] Fix table addressing --- README.md | 4 ++-- src/RethinkDb.Driver.FSharp/Builder.fs | 32 +++++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5d11d41..8a481f7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RethinkDb.Driver.FSharp Idiomatic F# extensions for the C# RethinkDB driver -![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/RethinkDb.Driver.FSharp) +[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/RethinkDb.Driver.FSharp)](https://www.nuget.org/packages/RethinkDb.Driver.FSharp/) ## Using @@ -28,7 +28,7 @@ let fetchPost (postId : string) = /// string -> (IConnection -> Task) let fetchPost (postId : string) = rethink { - withTableInDb "Post" "Blog" + withTable "Blog.Post" get postId result withRetryDefault diff --git a/src/RethinkDb.Driver.FSharp/Builder.fs b/src/RethinkDb.Driver.FSharp/Builder.fs index 5541bd8..fa5f287 100644 --- a/src/RethinkDb.Driver.FSharp/Builder.fs +++ b/src/RethinkDb.Driver.FSharp/Builder.fs @@ -18,6 +18,14 @@ type RethinkBuilder<'T> () = fields |> List.fold (fun (m : Model.MapObject) item -> m.With (fst item, snd item)) (RethinkDB.R.HashMap ()) + /// Split a table name with a "." separator into database and table parts + let dbAndTable (table : string) = + match table.Contains "." with + | true -> + let parts = table.Split '.' + Some parts[0], parts[1] + | false -> None, table + member _.Bind (expr : ReqlExpr, f : ReqlExpr -> ReqlExpr) = f expr member this.For (expr, f) = this.Bind (expr, f) @@ -28,15 +36,14 @@ type RethinkBuilder<'T> () = /// Specify a database for further commands [] - member _.Db (expr : RethinkDB, db : string) = match db with "" -> expr.Db () | _ -> expr.Db db + member _.Db (r : RethinkDB, db : string) = match db with "" -> r.Db () | _ -> r.Db db - /// Specify a table in the default database + /// Identify a table (of form "dbName.tableName"; if no db name, uses default database) [] - member _.TableInDefaultDb (expr : RethinkDB, table : string) = expr.Table table - - /// Specify a table in a specific database - [] - member _.Table (db : Db, table : string) = db.Table table + member this.Table (r : RethinkDB, table : string) = + match dbAndTable table with + | Some db, tbl -> this.Db(r, db).Table tbl + | None, _ -> r.Table table /// Create an equality join with another table [] @@ -61,13 +68,12 @@ type RethinkBuilder<'T> () = [] member this.TableList (r : RethinkDB, db : string) = this.Db(r, db).TableList () - /// Create a table in the default database + /// Create a table (of form "dbName.tableName"; if no db name, uses default database) [] - member _.TableCreate (r : RethinkDB, table : string) = r.TableCreate table - - /// Create a table in the default database - [] - member _.TableCreate (db : Db, table : string) = db.TableCreate table + member this.TableCreate (r : RethinkDB, table : string) = + match dbAndTable table with + | Some db, tbl -> this.Db(r, db).TableCreate tbl + | None, _ -> r.TableCreate table /// List all indexes for a table []