Skip to content

Commit

Permalink
Add functions for creating view and designdocconfig objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed Jan 18, 2018
1 parent 1b99250 commit a4d3073
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions Davenport.Fsharp/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type CouchProps = private {
onWarning: (IEvent<EventHandler<string>,string> -> unit) option
}

type ViewProps = private {
map: string option
reduce: string option
}

let private defaultCouchProps() = {
username = None
password = None
Expand All @@ -48,6 +53,11 @@ let private defaultCouchProps() = {
onWarning = None
}

let private defaultViewProps() = {
map = None
reduce = None
}

let private toConfig<'doctype> (props: CouchProps) =
let config = Davenport.Configuration(props.couchUrl, props.databaseName)
config.Username <- Option.defaultValue "" props.username
Expand Down Expand Up @@ -90,18 +100,42 @@ let private convertPostPutCopyResponse (r: PostPutCopyResponse) =
Rev = r.Rev
Ok = Option.ofNullable r.Ok |> Option.defaultValue false }

let private asyncMap (fn: 't -> 'u) task = async {
let asyncMap (fn: 't -> 'u) task = async {
let! result = task

return fn result
}

let private asyncMapSeq (fn: 't -> 'u) task = async {
let asyncMapSeq (fn: 't -> 'u) task = async {
let! result = task

return Seq.map fn result
}

/// Creates a sequence with a length of one out of the object given.
let toSeq x = Seq.ofList [x]

/// Used to create a DesignDoc view.
let mapFunction func = { defaultViewProps() with map = Some func }

/// Used to create a DesignDoc view.
let reduceFunction func props = { props with reduce = Some func }

/// Creates a DesignDoc view object.
let view name (props: ViewProps) =
let v = View()
v.Name <- name
v.MapFunction <- Option.toObj props.map
v.ReduceFunction <- Option.toObj props.reduce
v

/// Creates a DesignDoc object for use with the `configureDatabase` and `createDesignDocs` functions.
let designDoc name (views: View seq) =
let d = DesignDocConfig()
d.Name <- name
d.Views <- views
d

let database name couchUrl =
{ defaultCouchProps() with databaseName = name; couchUrl = couchUrl }

Expand Down Expand Up @@ -210,11 +244,11 @@ let delete id rev props =
|> Async.AwaitTask

/// Executes the view with the given designDocName and viewName.
let view<'returnType> designDocName viewName (viewOptions: ViewOptions option) props =
let executeView<'returnType> designDocName viewName (viewOptions: ViewOptions option) props =
let client = toClient props
let options = Option.toObj viewOptions

client.ViewAsync(designDocName, viewName, options)
client.ViewAsync<'returnType>(designDocName, viewName, options)
|> Async.AwaitTask

/// Gets the document with the given id. If a revision is given, that specific version will be returned.
Expand Down

0 comments on commit a4d3073

Please sign in to comment.