Created
March 26, 2016 03:56
-
-
Save esad/7fb9f04da5f06f6f6dc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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