Skip to content

Instantly share code, notes, and snippets.

@esad
Created March 26, 2016 03:56
Show Gist options
  • Save esad/7fb9f04da5f06f6f6dc4 to your computer and use it in GitHub Desktop.
Save esad/7fb9f04da5f06f6f6dc4 to your computer and use it in GitHub Desktop.
module PouchDB where
import Task
import Json.Decode exposing (..)
import Native.PouchDB
type PouchDB = PouchDB
type alias Document a =
{ a | pouchId : Maybe String, pouchRev : Maybe String }
create : String -> Task.Task String PouchDB
create name =
Native.PouchDB.create name
documentDecoder : Json.Decode.Decoder a -> Json.Decode.Decoder (Document a)
documentDecoder decoder =
object3
(\id rev model -> { model | pouchId = Just id, pouchRev = Just rev })
("id" := string)
("_rev" := string)
decoder
all : PouchDB -> Json.Decode.Decoder a -> Task.Task String (List (Document a))
all db decoder =
Native.PouchDB.allDocs
|> Task.map (List.filterMap (Json.Decode.decodeValue (documentDecoder decoder) >> Result.toMaybe))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment