Skip to content

Executing queries

Jacek edited this page Jun 29, 2017 · 8 revisions

The preferrable way of executing queries is to pass a query function to one of these methods:

let blog = getBlog id |> run

for synchronous execution, or

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

for asynchronous one.

When mor 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 some 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
Clone this wiki locally