Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality for including related documents #34

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/Marten.FSharp/Marten.FSharp.fs
Expand Up @@ -1904,3 +1904,73 @@ module Queryable =
/// **Exceptions**
///
let paging (skipped: int) (takeAmount: int) (q: IQueryable<'a>) = q |> skip skipped |> take takeAmount

/// **Description**
///
/// Fetches a single related document.else
///
/// **Parameters**
/// * `selector` - foreign key selector
/// * `action` - related document callback
/// * `q` - your `IQueryable` parameter
///
/// **Output Type**
/// * `IQueryable<'a>`
///
/// **Reference**
/// https://martendb.io/documents/querying/linq/include.html#join-a-single-document
///
/// **Exceptions**
///
let includeSingle<'a, 'b> (selector: Quotations.Expr<'a -> obj>) (action: 'b -> unit) (q: IQueryable<'a>) =
q.Include(selector |> Lambda.ofArity1, action)

/// **Description**
///
/// Fetches a collection of related documents.
/// Appends the documents to your specified `IList<'b>` parameter
///
/// **Parameters**
/// * `selector` - foreign key selector
/// * `list` - the list the related documents will be appended to.
/// * `q` - your `IQueryable` parameter
///
/// **Output Type**
/// * `IQueryable<'a>`
///
/// **Reference**
/// https://martendb.io/documents/querying/linq/include.html#join-many-documents
///
/// **Exceptions**
///
let includeList<'a, 'b>
(selector: Quotations.Expr<'a -> obj>)
(list: Collections.Generic.IList<'b>)
(q: IQueryable<'a>)
=
q.Include(selector |> Lambda.ofArity1, list)

/// **Description**
///
/// Fetches a collection of related documents.
/// Appends the document's primary key and data to the dictionary.
///
/// **Parameters**
/// * `selector` - foreign key selector
/// * `dict` - the dictionary the related documents will be appended to.
/// * `q` - your `IQueryable` parameter
///
/// **Output Type**
/// * `IQueryable<'a>`
///
/// **Reference**
/// https://martendb.io/documents/querying/linq/include.html#join-many-documents
///
/// **Exceptions**
///
let includeDict<'a, 'b, 'c>
(selector: Quotations.Expr<'a -> obj>)
(dict: Collections.Generic.Dictionary<'b, 'c>)
(q: IQueryable<'a>)
=
q.Include(selector |> Lambda.ofArity1, dict)
111 changes: 109 additions & 2 deletions tests/Marten.FSharp.Tests/Tests.fs
Expand Up @@ -40,6 +40,13 @@ open Marten.PLv8
open Marten.Linq
open Npgsql

type Author = { Id: int; Name: string }

type Book =
{ Id: int
AuthorId: int
Title: string }

module DatabaseTestHelpers =
open Weasel.Postgresql

Expand Down Expand Up @@ -119,7 +126,11 @@ module DatabaseTestHelpers =
inner ()

let getStore (database: DisposableDatabase) =
database.Conn |> string |> DocumentStore.For
DocumentStore.For(fun options ->
options.Connection(database.Conn |> string)

options.Schema.For<Book>().ForeignKey<Author>(fun book -> book.AuthorId)
|> ignore)

[<CLIMutable>]
type Dog =
Expand Down Expand Up @@ -765,6 +776,101 @@ let sqlTests =
Expect.equal person marcoPolo "Not marco"
} ]

let includeTests =
[ testCase' "include single related document."
<| fun (db, store: IDocumentStore) ->
let author = { Id = 1; Name = "John Doe" }

let book =
{ Id = 1
AuthorId = 1
Title = "The adventures of a mysterious man." }

use session = store.OpenSession()

session |> Session.storeSingle author
session |> Session.storeSingle book
Session.saveChanges session

use session2 = store.OpenSession()

let mutable bookAuthor: Author option = None

let bookFromDb =
session2
|> Session.query<Book>
|> Queryable.includeSingle <@ fun book -> book.AuthorId @> (fun value -> bookAuthor <- Some value)
|> Queryable.tryHead

Expect.equal bookFromDb (Some book) "Not the same book"
Expect.equal bookAuthor (Some author) "Not the same author"

testCase' "include list of related documents."
<| fun (db, store: IDocumentStore) ->
let authors = [ { Id = 1; Name = "John Doe" }; { Id = 2; Name = "Jane Doe" } ]

let books =
[ { Id = 1
AuthorId = 1
Title = "The adventures of a mysterious man." }

{ Id = 2
AuthorId = 2
Title = "The adventures of a mysterious woman." } ]

use session = store.OpenSession()

session |> Session.storeMany authors
session |> Session.storeMany books
Session.saveChanges session

use session2 = store.OpenSession()

let bookAuthors = Collections.Generic.List<Author>()

let booksFromDb =
session2
|> Session.query<Book>
|> Queryable.includeList <@ fun book -> book.AuthorId @> bookAuthors
|> Queryable.toList

Expect.equal (booksFromDb |> Seq.toList) books "Not the same books"
Expect.equal (bookAuthors |> Seq.toList) authors "Not the same authors"

testCase' "include dictionary of related documents."
<| fun (db, store: IDocumentStore) ->
let authors = [ { Id = 1; Name = "John Doe" }; { Id = 2; Name = "Jane Doe" } ]

let books =
[ { Id = 1
AuthorId = 1
Title = "The adventures of a mysterious man." }

{ Id = 2
AuthorId = 2
Title = "The adventures of a mysterious woman." } ]

use session = store.OpenSession()

session |> Session.storeMany authors
session |> Session.storeMany books
Session.saveChanges session

use session2 = store.OpenSession()

let bookAuthors = Collections.Generic.Dictionary<int, Author>()

let booksFromDb =
session2
|> Session.query<Book>
|> Queryable.includeDict <@ fun book -> book.AuthorId @> bookAuthors
|> Queryable.toList

Expect.equal (booksFromDb |> Seq.toList) books "Not the same books"
Expect.equal bookAuthors.Count 2 "Count is greater than or less than 2"
Expect.equal bookAuthors[1] authors[0] "Authors do not match"
Expect.equal bookAuthors[2] authors[1] "Authors do not match" ]

[<Tests>]
let ``API Tests`` =
testList
Expand All @@ -780,4 +886,5 @@ let ``API Tests`` =
yield! CRUDTests
// yield! PatchTests
yield! LinQQueryTests
yield! sqlTests ] ]
yield! sqlTests
yield! includeTests ] ]