├── README ├── couch_js_database_methods.markdown ├── couch_js_design_methods.markdown ├── couch_js_document_methods.markdown ├── couch_js_security_methods.markdown ├── couch_js_server_methods.markdown ├── jquery_couch_js_database_methods.markdown ├── jquery_couch_js_design_methods.markdown ├── jquery_couch_js_document_methods.markdown ├── jquery_couch_js_security_methods.markdown └── jquery_couch_js_server_methods.markdown /README: -------------------------------------------------------------------------------- 1 | Documentation for CouchDB couch.js and jquery.couch.js. -------------------------------------------------------------------------------- /couch_js_database_methods.markdown: -------------------------------------------------------------------------------- 1 | # Database 2 | 3 | ## Database Intro 4 | A new db is specified like this: 5 | db = new CouchDB("spec_db", {"X-Couch-Full-Commit":"false"}); 6 | It then has to be [created](/createDb) before you can do anything with it. 7 | 8 | 9 | ## .request() 10 | 11 | ### db.request(method, uri, requestOptions) 12 | 13 | ### Description 14 | Combines the headers of the [db](/database-intro) with the requestOptions and passes the parameters to [CouchDB.request](/request), which creates a [new XMLHTTPRequest](/newXhr). 15 | 16 | ### Results 17 | The XMLHTTPRequest has been sent with the given parameters and the headers options of the db. 18 | 19 | ### Returns 20 | A new XMLHTTPRequest with readyState 4 as made by [CouchDB.request](/request). 21 | 22 | ### Example 23 | db.request("GET", "/test_db") 24 | 25 | 26 | 27 | 28 | ## .createDb() 29 | 30 | ### db.createDb() 31 | 32 | ### Description 33 | Creates the db. 34 | 35 | ### Results 36 | The db is created, with update sequence 0. 37 | 38 | ### Returns 39 | {"ok" : true} 40 | 41 | 42 | ## .deleteDb() 43 | 44 | ### db.deleteDb() 45 | 46 | ### Description 47 | Deletes the db. 48 | 49 | ### Results 50 | The db is deleted. 51 | 52 | ### Returns 53 | {"ok" : true} 54 | 55 | 56 | ## .ensureFullCommit() 57 | 58 | ### db.ensureFullCommit() 59 | 60 | ### Description 61 | Does a POST request to the db and "_ensure_full_commit" 62 | 63 | ### Results 64 | Tells the db to do an fsync (write data to disk immediately). 65 | 66 | ### Returns 67 | {"ok" : true} 68 | and the start time of the db instance. 69 | 70 | 71 | ## .query() 72 | 73 | ### db.query(mapFun, reduceFun, options, keys, language) 74 | 75 | ### Description 76 | Applies the mandatory map function and the optional reduce function to the contents of database and returns the results. 77 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 78 | The result can be specified more by querying only for single keys. 79 | The language of the query has to be specified when another language than the default JavaScript is used. [Erlang views have to be enabled](/http://wiki.apache.org/couchdb/EnableErlangViews) beforehand. 80 | 81 | ### Returns 82 | The total number of rows and an array of rows, each row containing one result of the query. 83 | 84 | ### Example 85 | db.query("function(doc) { emit(doc._id, null); }", null, {"include_docs":"true"}, ["123", "456"]) 86 | 87 | 88 | ## .view() 89 | 90 | ### db.view(viewname, options, keys) 91 | 92 | ### Description 93 | Applies the view to the contents of database and returns the results. When there are no keys given, it does a GET request to the view path with the options as params, or a POST request with the keys as body. 94 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 95 | The result can be specified more by querying only for single keys. 96 | 97 | ### Returns 98 | The total number of rows and an array of rows, each row containing one result of the query, or null when the view doesn't exist. 99 | 100 | ### Example 101 | db.view('spec_db/viewname', {"include_docs":"true"}, ["456"]) 102 | 103 | 104 | ## .info() 105 | 106 | ### db.info() 107 | 108 | ### Description 109 | Does a GET request to the db. 110 | 111 | ### Returns 112 | db_name, doc_count, doc_del_count, update_seq, purge_seq, compact_running, disk_size, instance_start_time, disk_format_version 113 | 114 | 115 | ## .allDocs() 116 | 117 | ### db.allDocs(options, keys) 118 | 119 | ### Description 120 | When there are no keys given, it does a GET request to "_all_docs" with the options as params, or a POST request with the keys as body. 121 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 122 | The result can be specified more by querying only for single keys. 123 | 124 | ### Returns 125 | The total number of rows and an array of rows, each row containing one result of the query. 126 | 127 | ### Example 128 | db.allDocs({"include_docs":"true"}, ["456"]) 129 | 130 | 131 | ## .changes() 132 | 133 | ### db.changes(options) 134 | 135 | ### Description 136 | Does a GET request to "_changes" with the options as params. 137 | Options can include the [Changes Options of the HTTP database API](http://wiki.apache.org/couchdb/HTTP_database_API#Changes). 138 | 139 | ### Returns 140 | The last sequence and an array with results. Each result contains the ID and sequence of the changed document and an array with all the changes. For each change the revision is specified. 141 | 142 | ### Example 143 | db.changes({"since":"1"}) 144 | 145 | 146 | 147 | ## .compact() 148 | 149 | ### db.compact() 150 | 151 | ### Description 152 | Does a POST request to the db and "_compact". Compacts the db. 153 | 154 | ### Returns 155 | {"ok" : true} 156 | 157 | 158 | ## .viewCleanup() 159 | 160 | ### db.viewCleanup() 161 | 162 | ### Description 163 | Does a POST request to the db and "_view_cleanup". Cleans old view output from disk. 164 | 165 | ### Returns 166 | {"ok" : true} 167 | 168 | 169 | 170 | ## .setDbProperty() 171 | 172 | ### db.setDbProperty(propId, propValue) 173 | 174 | ### Description 175 | Does a PUT request to the db and the propId with the propValue as body. 176 | 177 | ### Results 178 | The db property is set to the specified value. 179 | 180 | ### Returns 181 | {"ok" : true} 182 | 183 | ### Example 184 | db.setDbProperty("_revs_limit", 1500) 185 | 186 | 187 | 188 | ## .getDbProperty() 189 | 190 | ### db.getDbProperty(propId) 191 | 192 | ### Description 193 | Does a GET request to the db and the propId. 194 | 195 | ### Returns 196 | The property value. 197 | 198 | ### Example 199 | db.getDbProperty("_revs_limit") 200 | 201 | 202 | 203 | ## .setSecObj() 204 | 205 | ### db.setSecObj(secObj) 206 | 207 | ### Description 208 | Does a PUT request to the db and "_security" with the security object as body. 209 | 210 | ### Results 211 | The security object is set to the specified value. 212 | 213 | ### Returns 214 | {"ok" : true} 215 | 216 | ### Example 217 | db.setSecObj({"admins" : {"names" : ["laura"], "roles" : ["boss"]}}) 218 | 219 | 220 | ## .getSecObj() 221 | 222 | ### db.getSecObj(propId) 223 | 224 | ### Description 225 | Does a GET request to the db and "_security". 226 | 227 | ### Returns 228 | The security object. 229 | 230 | 231 | 232 | -------------------------------------------------------------------------------- /couch_js_design_methods.markdown: -------------------------------------------------------------------------------- 1 | # Design Docs 2 | 3 | ## .allDesignDocs() 4 | 5 | ### CouchDB.allDesignDocs() 6 | 7 | ### Description 8 | Goes through [all dbs](/allDbs) and returns all [design docs](/designDocs) for each db. 9 | 10 | ### Returns 11 | An array with all dbs, for each db the id and revision of every design document. 12 | 13 | ### Example 14 | When there is one database "test_db" and one design document "mydesign", the result is this: 15 | {"test_db": {"total_rows": 1, "offset": 0, "rows": [{"id": "_design/spec_db", "key": "_design/mydesign", "value": {"rev": "1-3885802b8f5804e8f03cda99df8e6cc7"}}]}} 16 | 17 | 18 | 19 | ## .designInfo() 20 | 21 | ### db.designInfo(docId) 22 | 23 | ### Description 24 | Does a GET request to the db, the docId and "/_info". 25 | 26 | ### Returns 27 | The name of the design document and the view_index with the attributes: "signature", "language", "disk_size", "updater_running", "compact_running", "waiting_commit", "waiting_clients", "update_seq", "purge_seq" 28 | 29 | 30 | 31 | ## .designDocs() 32 | 33 | ### db.designDocs() 34 | 35 | ### Description 36 | Does a GET request to "_all_docs" and filters out only the design documents. 37 | 38 | ### Returns 39 | All design documents of a db. 40 | -------------------------------------------------------------------------------- /couch_js_document_methods.markdown: -------------------------------------------------------------------------------- 1 | # Documents 2 | 3 | ## .save() 4 | 5 | ### db.save(doc, options) 6 | 7 | ### Description 8 | Sets a docId when the document doesn't contain an _id attribute. Then does a PUT request to the db and the docId with the options as params and the doc as body. 9 | 10 | ### Results 11 | A saved document with an ID. 12 | 13 | ### Returns 14 | ID and revision of the saved document. 15 | 16 | ### Example 17 | db.save(doc, {"batch" : "ok"}) 18 | 19 | 20 | ## .open() 21 | 22 | ### db.open(docId, options) 23 | 24 | ### Description 25 | Does a GET request to the db and the docId with the options as params. 26 | 27 | ### Returns 28 | The document, or null when the document doesn't exist. 29 | 30 | ### Example 31 | db.open("123", {"revs" : "true"}) 32 | 33 | 34 | ## .deleteDoc() 35 | 36 | ### db.deleteDoc(doc) 37 | 38 | ### Description 39 | Does a DELETE request to the db and the docId with the revision as param. The current revision of the document has to be specified, otherwise the request results in a 409 error. 40 | 41 | ### Results 42 | The document is deleted (that means the document has the "deleted":true attribute). 43 | 44 | ### Returns 45 | ID and revision of the deleted document. 46 | 47 | ### Example 48 | db.deleteDoc({_id : "123", _rev : "1-04d86233b3254bb5a53dcf7103f97fc2"}) 49 | 50 | 51 | 52 | ## .deleteDocAttachment() 53 | 54 | ### db.deleteDocAttachment(doc, attachment_name) 55 | 56 | ### Description 57 | Does a DELETE request to the db and the docId and the attachment_name with the revision as param. The current revision of the document has to be specified, otherwise the request results in a 409 error. 58 | 59 | ### Results 60 | The attachment is deleted, the document stays intact otherwise. 61 | 62 | ### Returns 63 | ID and revision of the document whose attachment has been deleted. 64 | 65 | ### Example 66 | db.deleteDocAttachment({_id : "123", _rev : "1-04d86233b3254bb5a53dcf7103f97fc2"}, "attachment.txt") 67 | 68 | 69 | 70 | ## .bulkSave() 71 | 72 | ### db.bulkSave(docs, options) 73 | 74 | ### Description 75 | Sets a docId in each document when it doesn't contain an _id attribute. Then does a POST request to the db and _bulk_docs with the docs and the options as body. 76 | 77 | ### Results 78 | All the documents are saved with an ID. 79 | 80 | ### Returns 81 | An array with an element for each document that contains ID and revision of the document. 82 | 83 | ### Example 84 | doc1 = {"foo":"bar"} 85 | doc2 = {"foo":"baz"} 86 | db.bulkSave([doc1, doc2]) 87 | 88 | -------------------------------------------------------------------------------- /couch_js_security_methods.markdown: -------------------------------------------------------------------------------- 1 | # Security 2 | 3 | ## Security Intro 4 | 5 | The default authentication db is called "_users". In this db the information about the users is stored. You can change the authentication db like this: 6 | 7 | users_db = new CouchDB("custom_users_db"); 8 | users_db.createDb(); 9 | 10 | CouchDB.request("PUT", "/_config/couch_httpd_auth/authentication_db", { body: JSON.stringify("custom_users_db")}); 11 | 12 | You can "signup" like this: 13 | 14 | userDoc = CouchDB.prepareUserDoc({name: "username", roles: ["customrole"]}, "secretpassword"); 15 | 16 | users_db.save(userDoc); 17 | 18 | 19 | ## .login() 20 | 21 | ### CouchDB.login("username", "secretpassword") 22 | 23 | ### Description 24 | Does a POST request to "_session" with the user details. 25 | 26 | ### Results 27 | A created [session](/session) with the user name and roles. 28 | 29 | ### Returns 30 | {"ok": true, "name": "username", "roles": ["customrole"]} 31 | 32 | ### Prerequisites 33 | A user doc [prepareUserDoc](/prepareUserDoc), saved in an [authentication db](/security-intro). 34 | 35 | 36 | ## .logout() 37 | 38 | ### CouchDB.logout() 39 | 40 | ### Description 41 | Does a DELETE request to "_session". 42 | 43 | ### Results 44 | The [session's](/session) user name is set to null and the custom role is removed from the roles array. 45 | 46 | ### Returns 47 | {"ok": true} 48 | 49 | ### Prerequisites 50 | A user [session](/session), as created by [login](/login). 51 | 52 | 53 | ## .session() 54 | 55 | ### CouchDB.session(options) 56 | 57 | ### Description 58 | Does a GET request to "_session". 59 | 60 | ### Returns 61 | {"ok": true, "userCtx": {"name": "username", "roles": ["customrole"]}, "info": {"authentication_db": "_users", "authentication_handlers": ["oauth", "cookie", "default"], "authenticated": "cookie"}} 62 | 63 | ### Prerequisites 64 | A user [session](/session), as created by [login](/login). When there is no session, userCtx.name is null. 65 | 66 | 67 | ## .prepareUserDoc() 68 | 69 | ### CouchDB.prepareUserDoc(userDoc, "secretpassword") 70 | 71 | ### Description 72 | * Hashes the password 73 | * Adds an empty roles array to the userDoc when not specified 74 | * Adds an _id, composed of user_prefix and name, to the userDoc when not specified 75 | 76 | ### Returns 77 | {"name": "username", "roles": ["customrole"], "_id": "org.couchdb.user:customrole", "salt":"a salt", "password_sha": "hashed password", "type":"user"} 78 | 79 | ### Example 80 | CouchDB.prepareUserDoc({name: "username", roles: ["customrole"]}, "secretpassword") 81 | 82 | 83 | -------------------------------------------------------------------------------- /couch_js_server_methods.markdown: -------------------------------------------------------------------------------- 1 | # Server 2 | 3 | ## .allDbs() 4 | 5 | ### CouchDB.allDbs() 6 | 7 | ### Description 8 | Does a GET request to "_all_dbs". 9 | 10 | ### Returns 11 | An array with all the databases. 12 | 13 | 14 | ## .getVersion() 15 | 16 | ### CouchDB.getVersion() 17 | 18 | ### Description 19 | Does a GET request to "/". 20 | 21 | ### Returns 22 | The version of the CouchDB installation. 23 | 24 | 25 | ## .replicate() 26 | 27 | ### CouchDB.replicate(source, target, replication_options) 28 | 29 | ### Description 30 | Replicates the content of the source db to the target db. 31 | 32 | ### Results 33 | The target db contains all the documents from the source db. 34 | 35 | ### Returns 36 | {"ok": true, "session_id": "a session id", "source_last_seq": 1, "history": [{"session_id": "a session id", "start_time": "start time", "end_time": "end time", "start_last_seq": 0, "end_last_seq": 1, "recorded_seq": 1, "missing_checked": 0, "missing_found": 1, "docs_read": 1, "docs_written": 1, "doc_write_failures": 0}]} 37 | 38 | ### Example 39 | With the create_target option, the target db gets created if it doesn't already exist: 40 | CouchDB.replicate(http://localhost:5984/test_db, http://localhost:5984/test_db2, {"body" : {"create_target":true}}) 41 | 42 | 43 | ## .newXhr() 44 | 45 | ### CouchDB.newXhr() 46 | 47 | ### Description 48 | Returns an XMLHTTPRequest or an ActiveXObject, depending on the OS. If no XMLHTTPRequest support is detected, an error is thrown. 49 | 50 | ### Returns 51 | A new XMLHTTPRequest or nothing. 52 | 53 | 54 | ## .request() 55 | 56 | ### CouchDB.request(method, uri, options) 57 | 58 | ### Description 59 | Creates a [new XMLHTTPRequest](/newXhr). If the URI parameter doesn't start with "http://", the URI is prefixed with the CouchDB urlPrefix. If there are headers given in the options hash, they are set via setRequestHeader. The request is sent with the specified method. 60 | 61 | ### Results 62 | The XMLHTTPRequest has been sent with the given parameters. 63 | 64 | ### Returns 65 | A new XMLHTTPRequest with readyState 4. 66 | 67 | ### Example 68 | CouchDB.request("GET", "/", {"headers": {"X-Couch-Full-Commit":"true"}}); 69 | 70 | 71 | ## .requestStats() 72 | 73 | ### CouchDB.requestStats(module, key, test) 74 | 75 | ### Description 76 | Does a GET request to "/_stats/module/key". 77 | 78 | ### Returns 79 | Statistics about the specified module and key. 80 | 81 | ### Example 82 | This returns the number of open databases: 83 | CouchDB.requestStats('couchdb', 'open_databases', test); 84 | When the last argument is not null, "?flush=true" is appended to the request. 85 | 86 | 87 | ## .newUuids() 88 | 89 | ### CouchDB.newUuids(amount, buffer) 90 | 91 | ### Description 92 | The CouchDB.uuids_cache is filled with as many UUIDs as specified in the buffer parameter, or with 100. The next time newUuids is called, they aren't requested from CouchDB, but taken directly from this cache if the cache contains enough of them. 93 | 94 | ### Returns 95 | An array with the specified amount of UUIDs. 96 | 97 | ### Example 98 | The first request gives you 10 UUIDs, the second 125. 99 | CouchDB.newUuids(10); 100 | CouchDB.newUuids(125, 60); 101 | The uuids_cache now contains 160 UUIDs. 102 | 103 | 104 | ## .maybeThrowError() 105 | 106 | ### CouchDB.maybeThrowError(request) 107 | 108 | ### Description 109 | Throws an error when the given request has a status greater than 400. 110 | 111 | ### Example 112 | var req = CouchDB.request("GET", "/nonexisting_db") 113 | results in a 404 error, so 114 | CouchDB.maybeThrowError(req) 115 | returns 116 | {"error": "not_found", "reason": "no_db_file"} 117 | 118 | 119 | ## .params() 120 | 121 | ### CouchDB.params(options) 122 | 123 | ### Description 124 | Turns a json object into a http params string. 125 | 126 | ### Returns 127 | A string with the keys and values separated by "=" and "&" or an empty string when the options are empty. 128 | 129 | ### Example 130 | CouchDB.params({"key":"value", "key2":"value2"}) 131 | returns 132 | "key=value&key2=value2" 133 | 134 | 135 | -------------------------------------------------------------------------------- /jquery_couch_js_database_methods.markdown: -------------------------------------------------------------------------------- 1 | # Database 2 | 3 | ## Database Intro 4 | A new db is specified like this: 5 | db = $.couch.db("spec_db"); 6 | 7 | It then has to be [created](/create) before you can do anything with it. 8 | 9 | 10 | ## .compact() 11 | 12 | ### db.compact(options) 13 | 14 | ### Description 15 | Does a POST request to the db and "_compact". Compacts the db. 16 | 17 | ### Response 18 | {"ok" : true} 19 | 20 | 21 | 22 | ## .viewCleanup() 23 | 24 | ### db.viewCleanup(options) 25 | 26 | ### Description 27 | Does a POST request to the db and "_view_cleanup". Cleans old view output from disk. 28 | 29 | ### Returns 30 | {"ok" : true} 31 | 32 | 33 | 34 | ## .compactView() 35 | 36 | ### db.compactView(groupname, options) 37 | 38 | ### Description 39 | Does a POST request to the db, "_compact" and the groupname. Cleans old output from this view from disk. 40 | Raises a 404 error when the design doesn't exist. 41 | 42 | ### Returns 43 | {"ok" : true} 44 | 45 | 46 | 47 | ## .create() 48 | 49 | ### db.create(options) 50 | 51 | ### Description 52 | Creates the db. 53 | 54 | ### Results 55 | The db is created, with update sequence 0. 56 | 57 | ### Response 58 | {"ok" : true} 59 | 60 | 61 | ## .drop() 62 | 63 | ### db.drop(options) 64 | 65 | ### Description 66 | Deletes the db. 67 | 68 | ### Results 69 | The db is deleted. 70 | 71 | ### Response 72 | {"ok" : true} 73 | 74 | 75 | ## .info() 76 | 77 | ### db.info(options) 78 | 79 | ### Description 80 | Does a GET request to the db. 81 | 82 | ### Response 83 | db_name, doc_count, doc_del_count, update_seq, purge_seq, compact_running, disk_size, instance_start_time, disk_format_version 84 | 85 | 86 | 87 | ## .allDocs() 88 | 89 | ### db.allDocs(options) 90 | 91 | ### Description 92 | Does a GET request to "_all_docs" with the options as params. 93 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 94 | 95 | ### Response 96 | The total number of rows and an array of rows, each row containing one result of the query. 97 | 98 | 99 | 100 | ## .query() 101 | 102 | ### db.query(mapFun, reduceFun, language, options) 103 | 104 | ### Description 105 | Applies the mandatory map function and the optional reduce function to the contents of database and returns the results. 106 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 107 | The language of the query has to be specified when another language than the default JavaScript is used. [Erlang views have to be enabled](/http://wiki.apache.org/couchdb/EnableErlangViews) beforehand. 108 | 109 | ### Response 110 | The total number of rows and an array of rows, each row containing one result of the query. 111 | 112 | ### Example 113 | db.query("function(doc) { emit(doc._id, null); }", null, "javascript", {}) 114 | 115 | 116 | ## .view() 117 | 118 | ### db.view(viewname, options) 119 | 120 | ### Description 121 | Applies the view to the contents of database and returns the results. When there are no keys given, it does a GET request to the view path with the options as params, or a POST request with the keys as body. 122 | Options can include the [Querying Options of the HTTP view API](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). 123 | The result can be specified more by querying only for single keys. Keys are part of the options hash. 124 | 125 | ### Response 126 | The total number of rows and an array of rows, each row containing one result of the query, or null when the view doesn't exist. 127 | 128 | ### Example 129 | db.view('spec_db/viewname', {"include_docs":"true", "keys":["456"]}) 130 | 131 | 132 | 133 | 134 | ## .getDbProperty() 135 | 136 | ### db.getDbProperty(propName, options, ajaxOptions) 137 | 138 | ### Description 139 | Does a GET request to the db and the propName with the options as params. 140 | 141 | ### Response 142 | The property value. 143 | 144 | 145 | 146 | ## .setDbProperty() 147 | 148 | ### db.setDbProperty(propName, propValue, options, ajaxOptions) 149 | 150 | ### Description 151 | Does a PUT request to the db and the propName with the options as params and the propValue as body. 152 | 153 | ### Results 154 | The db property is set to the specified value. 155 | 156 | ### Response 157 | {"ok" : true} 158 | 159 | -------------------------------------------------------------------------------- /jquery_couch_js_design_methods.markdown: -------------------------------------------------------------------------------- 1 | # Design Docs 2 | 3 | ## .designDocs() 4 | 5 | ### db.designDocs(options) 6 | 7 | ### Description 8 | Does a GET request to "_all_docs" and filters out only the design documents. 9 | 10 | ### Response 11 | All design documents of a db. 12 | 13 | 14 | 15 | ## .allApps() 16 | 17 | ### db.allApps(options) 18 | 19 | ### Description 20 | I goes through [every design document](/designDocs) in the db and looks if it has an attachment with a member called "index.html" or a couchapp with a member called "index". If there is neither, it does nothing. If there is either, the "eachApp" method is called on each of those design documents. 21 | 22 | The options hash has to contain an "eachApp" attribute that contains a custom function with this signature: 23 | eachApp: function(appName, appPath, ddoc) 24 | The parameters being: 25 | 26 | * appName: the name of the design document 27 | * appPath: the full path to the index member in the design document 28 | * ddoc: the design document 29 | 30 | ### Results 31 | The eachApp function is executed on every design document that has an "index" resp. "index.html" member. 32 | 33 | ### Example 34 | var designDoc = {"_id" : "_design/with_attachments"}; 35 | 36 | designDoc._attachments = { 37 | "index.html" : { 38 | "content_type": "text\/html", 39 | // this is "
Hi, here is index!
", base64 encoded 40 | "data": "PGh0bWw+PHA+SGksIGhlcmUgaXMgaW5kZXghPC9wPjwvaHRtbD4=" 41 | } 42 | }; 43 | db.saveDoc(designDoc); 44 | 45 | db.allApps({ 46 | eachApp: function(appName, appPath, ddoc) { 47 | // appName: "with_attachments" 48 | // appPath: "/spec_db/_design/with_attachments/index.html" 49 | // ddoc._id: "_design/with_attachments" 50 | } 51 | }); -------------------------------------------------------------------------------- /jquery_couch_js_document_methods.markdown: -------------------------------------------------------------------------------- 1 | # Documents 2 | 3 | ## .openDoc() 4 | 5 | ### db.open(docId, options, ajaxOptions) 6 | 7 | ### Description 8 | Does a GET request to the db and the docId with the options as params. 9 | 10 | ### Response 11 | The document. When the document doesn't exist, a 404 error is raised. 12 | 13 | 14 | ## .saveDoc() 15 | 16 | ### db.saveDoc(doc, options) 17 | 18 | ### Description 19 | When the document doesn't contain an _id attribute it does a PUT request to the db and the docId with the options as params and the doc as body. 20 | When the document contains an _id attribute it does a POST request to the db with the options as params and the doc as body. 21 | 22 | ### Results 23 | A saved document with an ID. 24 | 25 | ### Response 26 | ID and revision of the saved document and 27 | {"ok" : true} 28 | 29 | 30 | 31 | ## .bulkSave() 32 | 33 | ### db.bulkSave(docs, options) 34 | 35 | ### Description 36 | Does a POST request to the db and _bulk_docs with the options as params and the docs as body. 37 | 38 | ### Results 39 | All the documents are saved with an ID. 40 | 41 | ### Response 42 | An array with an element for each document that contains ID and revision of the document. 43 | 44 | 45 | 46 | ## .removeDoc() 47 | 48 | ### db.removeDoc(doc) 49 | 50 | ### Description 51 | Does a DELETE request to the db and the docId with the revision and the options as params. The current revision of the document has to be specified, otherwise the request results in a 409 error. 52 | 53 | ### Results 54 | The document is deleted (that means the document has the "deleted":true attribute). 55 | 56 | ### Response 57 | ID and revision of the deleted document and 58 | {"ok" : true} 59 | 60 | 61 | 62 | ## .bulkRemove() 63 | 64 | ### db.bulkRemove(docs, options) 65 | 66 | ### Description 67 | Sets "_deleted" to true in each document. Then does a POST request to the db and _bulk_docs with the options as params and the docs as body. 68 | 69 | ### Results 70 | All the documents are deleted (that means the documents have the "deleted":true attribute).) 71 | 72 | ### Returns 73 | An array with an element for each document that contains ID and revision of the deleted document. 74 | 75 | 76 | 77 | ## .copyDoc() 78 | 79 | ### db.copyDoc(docId, options, ajaxOptions) 80 | 81 | ### Description 82 | Does a COPY request to the db and the docId. 83 | The ajaxOptions hash has to contain a "headers" attribute, which has to contain a "Destination" attribute that specifies the ID that the new document should have. 84 | 85 | ### Results 86 | Another document is saved that has the same data and the new ID. 87 | When the new document already exists, its correct revision has to be provided in order to overwrite it. When there is no correct revision, an error is thrown. 88 | 89 | ### Response 90 | The new document. 91 | 92 | ### Example 93 | Copies a document with ID "123", the new document has ID "456": 94 | db.copyDoc("123", {}, { 95 | headers: {"Destination":"456"} 96 | }); 97 | 98 | When the new document already exists, its revision has to be part of the Destination: 99 | db.copyDoc("123", {}, { 100 | headers: {"Destination":"456?rev=" + revision_of_456} 101 | }); 102 | 103 | -------------------------------------------------------------------------------- /jquery_couch_js_security_methods.markdown: -------------------------------------------------------------------------------- 1 | # Security 2 | 3 | ## Security Intro 4 | 5 | See [couch.js / Security Intro](/security-intro) 6 | 7 | 8 | ## .session() 9 | 10 | ### $.couch.session(options) 11 | 12 | ### Description 13 | Does a GET request to "_session". 14 | 15 | ### Returns 16 | {"ok": true, "userCtx": {"name": "username", "roles": ["customrole"]}, "info": {"authentication_db": "_users", "authentication_handlers": ["oauth", "cookie", "default"], "authenticated": "cookie"}} 17 | 18 | ### Prerequisites 19 | A user [session](/session), as created by [login](/login). When there is no session, userCtx.name is null. 20 | 21 | 22 | 23 | ## .userDb(callback) 24 | 25 | ### $.couch.userDb(callback) 26 | 27 | ### Description 28 | Creates [a new db](/database-intro) with the name of the [authentication db](/security-intro) and executes the callback function with the db as parameter. 29 | 30 | 31 | 32 | ## .signup() 33 | 34 | ### $.couch.signup(user_doc, password, options) 35 | 36 | ### Description 37 | * Hashes the password 38 | * Adds an empty roles array to the user_doc when not specified 39 | * Adds an _id, composed of "org.couchdb.user:" and name, to the user_doc when not specified 40 | * Saves the user_doc with options as parameters in the [userDb](/userDb) 41 | * Performs the success callback on the saved user_doc 42 | 43 | ### Response 44 | The saved user_doc. 45 | 46 | ### Results 47 | The user_doc is saved in the [userDb](/userDb). 48 | 49 | 50 | 51 | ## .login() 52 | 53 | ### $.couch.login(options) 54 | 55 | ### Description 56 | Does a POST request to "_session" with username and password, they have to be present in the options hash. 57 | Throws a 404 error when the password is wrong or there is no user with that username stored in the [userDb](/userDb). 58 | 59 | ### Results 60 | A created [session](/session) with the user name and roles. 61 | 62 | ### Returns 63 | {"ok": true, "name": "username", "roles": ["customrole"]} 64 | 65 | ### Prerequisites 66 | A [signed up user](/signup), saved in an [authentication db](/security-intro). 67 | 68 | 69 | ## .logout() 70 | 71 | ### $.couch.logout(options) 72 | 73 | ### Description 74 | Does a DELETE request to "_session". 75 | 76 | ### Results 77 | The [session's](/session) user name is set to null and the custom role is removed from the roles array. 78 | 79 | ### Returns 80 | {"ok": true} 81 | 82 | ### Prerequisites 83 | A user [session](/session), as created by [login](/login). 84 | -------------------------------------------------------------------------------- /jquery_couch_js_server_methods.markdown: -------------------------------------------------------------------------------- 1 | # Intro 2 | To get the results of all the methods, include these callbacks in the options hash, for example: 3 | $.couch.activeTasks({ 4 | success: function(resp){ 5 | console.log(resp); 6 | }, 7 | error: function(status, error, reason){ 8 | console.log(status, error, reason); 9 | } 10 | }); 11 | 12 | 13 | # Server 14 | 15 | ## .activeTasks() 16 | 17 | ### $.couch.activeTasks(options) 18 | 19 | ### Description 20 | Does a GET request to "_active_tasks". 21 | 22 | ### Response 23 | An array with all the active tasks, each entry containing type, PID, target and status of the task, or an empty array when there are no active tasks. 24 | 25 | 26 | ## .allDbs() 27 | 28 | ### $.couch.allDbs(options) 29 | 30 | ### Description 31 | Does a GET request to "_all_dbs". 32 | 33 | ### Response 34 | An array with all the databases. 35 | 36 | 37 | 38 | ## .config() 39 | 40 | ### CouchDB.config(options, section, option, value) 41 | 42 | ### Description 43 | Retreives or updates the server configuration. 44 | 45 | ### Results 46 | When called with section, option and value, the option is updated to the value. 47 | When called with section, option and null, the option is removed from the section. 48 | 49 | ### Response 50 | * Without options: All the config settings 51 | * With a section: The config section 52 | * With a section, an option and a value: an empty string 53 | * With a section, an option and null: the value the option had before 54 | 55 | ### Example 56 | Gets all the config settings: 57 | $.couch.config(); 58 | Gets a specific config setting: 59 | $.couch.config({}, "couchdb"); 60 | Updates a config setting: 61 | $.couch.config({}, "couchdb", "max_dbs_open", "120"); 62 | Deletes a config setting: 63 | $.couch.config({}, "couchdb", "max_dbs_open", null); 64 | 65 | 66 | 67 | ## .info() 68 | 69 | ### CouchDB.info(options) 70 | 71 | ### Description 72 | Does a GET request to "/". 73 | 74 | ### Response 75 | The version of the CouchDB installation. 76 | 77 | 78 | 79 | ## .replicate() 80 | 81 | ### CouchDB.replicate(source, target, ajaxOptions, replicationOptions) 82 | 83 | ### Description 84 | Replicates the content of the source db to the target db. 85 | 86 | ### Results 87 | The target db contains all the documents from the source db. 88 | 89 | ### Response 90 | {"ok": true, "session_id": "a session id", "source_last_seq": 1, "history": [{"session_id": "a session id", "start_time": "start time", "end_time": "end time", "start_last_seq": 0, "end_last_seq": 1, "recorded_seq": 1, "missing_checked": 0, "missing_found": 1, "docs_read": 1, "docs_written": 1, "doc_write_failures": 0}]} 91 | 92 | ### Example 93 | With the create_target option, the target db gets created if it doesn't already exist: 94 | CouchDB.replicate(http://localhost:5984/test_db, http://localhost:5984/test_db2, {}, {"create_target":true}) 95 | 96 | 97 | 98 | ## .newUUID() 99 | 100 | ### CouchDB.newUUID(cacheNum) 101 | 102 | ### Description 103 | fill the uuidCache with the specified number minus 1 104 | The CouchDB.uuidCache is filled with as many UUIDs as specified in the cacheNum parameter minus 1. The next time newUUID is called, the new UUID isn't requested from CouchDB, but taken directly from this cache if it's not empty. 105 | 106 | ### Response 107 | A new UUID, either from the cache or from the server. 108 | 109 | 110 | 111 | --------------------------------------------------------------------------------