Skip to content

Executing queries

Jacek edited this page Jun 29, 2017 · 8 revisions

To execute query, open connection to database is needed. To provide it, we can use one of query execution methods:

let blog = getBlog id |> run

for synchronous execution, or

async {
    let! blog = getBlog id |> runAsync
    ...
}

for asynchronous one.

When more than one query should be executed in context of one open connection, the function with DataContext parameter can be defined:

let insertPostWithTags (p: Post) (ctx: DataContext) = 
    let postId = insertPost p ctx
    insertTags postId p.tags ctx

insertPostWithTags p |> run

But there is even better approach. Functions of type DataContext -> 't are examples of Reader monad and it's possible to create co,mputation expression for them:

dbaction {
    let! postId = insertPost p
    do! insertTags postId p.tags 
} |> run

There is also asynchronous version:

asyncdb {
    let! postId = insertPost p
    do! insertTags postId p.tags
} |> runAsync |> Async.Start

Of course, nothing prevents you from creating connection manually, and execute query within use block, but I strongly believe, that it's not a proper way of doing things in functional language.

Clone this wiki locally