Module

Database.PouchDB

Package
purescript-pouchdb
Repository
fehrenbach/purescript-pouchdb

#Id Source

newtype Id (d :: Type)

Constructors

Instances

#Rev Source

newtype Rev (d :: Type)

Constructors

Instances

#Subrow Source

class Subrow (r :: Row Type) (s :: Row Type) 

Instances

#pouchDBLocal Source

pouchDBLocal :: forall options. WriteForeign { name :: String | options } => Subrow options (adapter :: String, auto_compaction :: Boolean, revs_limit :: Int) => { name :: String | options } -> Aff PouchDB

Create or open a local database

For example, the follwing opens (and creates if it does not exist) the local database "movies" with automatic compaction enabled:

do moviedb <- pouchDBLocal { name: "movies", auto_compaction: true }
   -- do something with your database

name is required, for the other options, see https://pouchdb.com/api.html#create_database

#pouchDBRemote Source

pouchDBRemote :: forall options. WriteForeign { name :: String | options } => Subrow options (ajax :: { cache :: Boolean, timeout :: Int, withCredentials :: Boolean }, auth :: { password :: String, username :: String }, skip_setup :: Boolean) => { name :: String | options } -> Aff PouchDB

Create or open a remote database

For example, the follwing opens a connection to the "query-movies" database on cloudant.com without trying to create it should it not exist already (skip_setup):

do moviedb <- pouchDBRemote { name: "https://examples.cloudant.com/query-movies", skip_setup: true }
   -- do something with your database

name is required, for the other options, see https://pouchdb.com/api.html#create_database

#getDoc Source

getDoc :: forall dat doc. ReadForeign doc => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> Id doc -> Aff doc

Fetch a document by _id.

#info Source

info :: PouchDB -> Aff { db_name :: String, doc_count :: Int, update_seq :: Int }

Get information about a database

#destroy Source

destroy :: PouchDB -> Aff Unit

Deletes the database.

DO NOT try to use the handle again. You can create a new PouchDB object using the same database name.

#createDoc Source

createDoc :: forall dat doc. WriteForeign { _id :: Id doc | dat } => Lacks "_id" dat => Lacks "_rev" dat => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> Id doc -> Record dat -> Aff doc

Save the given proto-document as a new document with the given id.

The document doc needs to be a newtype over a with _id, _rev and some data. The proto-document is just the data record, without _id and _rev fields.

This is the recommended way to generate documents, rather than relying on PouchDB to make up a random document ID for you.

#saveDoc Source

saveDoc :: forall dat doc. WriteForeign doc => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> doc -> Aff doc

Write a (modified) document back to the database.

On success, returns the document itself with updated _rev.

#deleteDoc Source

deleteDoc :: forall dat doc. WriteForeign { _deleted :: Boolean, _id :: Id doc, _rev :: Rev doc | dat } => Lacks "_deleted" dat => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> doc -> Aff doc

Delete a document from the database

Unlike PouchDB's remove, this does not clear all fields, but merely sets _deleted: true.

#bulkGet Source

bulkGet :: forall dat doc. ReadForeign doc => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> Array (Id doc) -> Aff (Array doc)

Fetch multiple documents at once.

This will throw the first parse error only.

This uses PouchDB's allDocs under the hood. It should not be confused with PouchDB's actual bulkGet which is used mainly internally for replication.

#bulkSave Source

bulkSave :: forall doc dat. ReadForeign doc => WriteForeign doc => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> Array doc -> Aff (Array doc)

Write multiple (modified) documents back to the database.

Note that this is not a transaction. Individual writes might fail but other changes might already have been made. This function will throw (only) the first error, if any.

As far as I can tell, PouchDB will increase the revision number even if you write the same exact document that's already there. You might want to avoid that.

#ReplicationInfo Source

type ReplicationInfo ext = { doc_write_failures :: Int, docs_read :: Int, docs_written :: Int, errors :: Array Foreign, last_seq :: Int, ok :: Boolean, start_time :: Foreign | ext }

#startReplication Source

startReplication :: forall options. WriteForeign { live :: Boolean | options } => Subrow options (batch_size :: Int, batches_limit :: Int, checkpoint :: Checkpoint, retry :: Boolean) => Lacks "live" options => PouchDB -> PouchDB -> Record options -> (ReplicationEvent -> Effect Unit) -> Effect (Effect Unit)

Start a continous (live) replication from source to target.

This sets the live option to true. For an explanation of the other options see https://pouchdb.com/api.html#replication

The event handler will be called with replication events as they happen.

Returns an effect with which you can cancel the replication.

#singleShotReplication Source

singleShotReplication :: forall options. WriteForeign (Record options) => Subrow options (batch_size :: Int, batches_limit :: Int, checkpoint :: Checkpoint) => PouchDB -> PouchDB -> Record options -> Aff (ReplicationInfo (end_time :: Foreign, status :: String))

Single-shot replication from source to target.

This is not live replication, and thus does not accept the retry option. For other options, see https://pouchdb.com/api.html#replication

This "blocks execution" and only returns when replication is complete or encounters an error.

#viewKeys Source

viewKeys :: forall v k l. ReadForeign { id :: Id l, key :: k, value :: v } => WriteForeign k => PouchDB -> String -> Array k -> Aff (Array { id :: Id l, key :: k, value :: v })

Simple keys query, no reduce, no docs included.

#viewKeysInclude Source

viewKeysInclude :: forall v k dat doc l. ReadForeign { doc :: doc, id :: Id l, key :: k, value :: v } => WriteForeign k => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> String -> Array k -> Aff (Array { doc :: doc, id :: Id l, key :: k, value :: v })

Simple keys query, no reduce, include docs.

Throws the first parse error only (if any).

#viewKeysDoc Source

viewKeysDoc :: forall k dat doc. ReadForeign doc => WriteForeign k => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> String -> Array k -> Aff (Array doc)

Simple keys query, no reduce, only return docs.

This is like viewKeysInclude, except that you only get the docs.

This is useful to look up a bunch of documents by a secondary key. Something like bulkGet, except for a different key than _id.

#viewRangeLimit Source

viewRangeLimit :: forall v l k. ReadForeign k => WriteForeign k => ReadForeign v => PouchDB -> String -> { endkey :: k, startkey :: k } -> Int -> Aff (Array { id :: Id l, key :: k, value :: v })

Range query with limit, no reduce, no docs.

#allDocsRange Source

allDocsRange :: forall doc dat. ReadForeign doc => Newtype doc { _id :: Id doc, _rev :: Rev doc | dat } => PouchDB -> { endkey :: String, startkey :: String } -> Aff (Array doc)

Fetch all docs between startkey and endkey (inclusive)

Consider using rangeFromPrefix when looking for doc ids starting with some string.

#rangeFromPrefix Source

rangeFromPrefix :: String -> { endkey :: String, startkey :: String }

Construct a {startkey, endkey} record for range queries that should match everything that starts with a given string. As recommended in in the CouchDB docs, we just append the special character \uFFF0 to the string to obtain the endkey. (This means it doesn't work if you use that in your doc ids/keys!) https://wiki.apache.org/couchdb/View_collation#String_Ranges

#viewRangeGroupLevel Source

viewRangeGroupLevel :: forall v k. WriteForeign k => ReadForeign k => ReadForeign v => PouchDB -> String -> { endkey :: Array k, startkey :: Array k } -> Int -> Aff (Array { key :: Array k, value :: v })

Reduce query over a range of keys, with group_level.

The types here are less than ideal, because we can have compound keys with different types, e.g. movie Id (as string) and year (as Int).

#changesLiveSinceNow Source

changesLiveSinceNow :: PouchDB -> ({ deleted :: Boolean, id :: String, rev :: String } -> Effect Unit) -> Aff Unit

Subscribe to future changes