Changeset 487 for Dev/trunk/src/server/util/couch.coffee
- Timestamp:
- 03/05/14 22:44:48 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/util/couch.coffee
r479 r487 6 6 7 7 class CouchDB 8 constructor: (url) -> 9 @url = normalizeURL url 10 get: (id) -> 8 constructor: (url,db) -> 9 @serverURL = normalizeURL url 10 @db = db 11 if @db 12 @url = "#{@serverURL}/#{@db}" 13 else 14 @url = "#{@serverURL}" 15 get: (id, opts) -> 11 16 url = "#{@url}/#{id}" 12 couchRequest 'GET', url 13 post: (doc ) ->14 url = "#{@url} "15 couchRequest 'POST', url, doc 16 put: (id, doc ) ->17 couchRequest 'GET', url, null, opts 18 post: (doc, opts) -> 19 url = "#{@url}/" 20 couchRequest 'POST', url, doc, opts 21 put: (id, doc, opts) -> 17 22 url = "#{@url}/#{id}" 18 couchRequest 'PUT', url, doc 19 delete: (id, rev) -> 20 url = "#{@url}/#{id}?rev=#{rev}" 21 couchRequest 'DELETE', url 23 couchRequest 'PUT', url, doc, opts 24 delete: (id, opts) -> 25 url = "#{@url}/#{id}" 26 couchRequest 'DELETE', url, null, opts 27 uuids: (opts) -> 28 url = "#{@serverURL}/_uuids" 29 couchRequest 'GET', url, null, opts 22 30 23 31 normalizeURL = (url) -> 24 32 url.replace /\/$/, '' 25 33 26 couchRequest = (method, url, data) ->34 couchRequest = (method, url, body, opts) -> 27 35 options = 28 36 method: method, 29 37 headers: 30 'content-type': 'application/json; charset=utf-8' 31 'accept': 'application/json' 32 body: JSON.stringify (stringifyFunctions (data || {})) 38 'Content-Type': 'application/json; charset=utf-8' 39 'Accept': 'application/json' 40 body: JSON.stringify (stringifyFunctions (body || {})) 41 if opts 42 options.qs = createQueryObj opts.query if opts.query 43 _.extend options.headers, opts.headers if opts.headers 33 44 req = request(url, options) 34 req.response 35 .then (res) => 45 res = req.response.then (res) => 36 46 req.then (res) => 37 47 JSON.parse res … … 40 50 , (err) => 41 51 Q.reject err 52 res.response = req.response.then (res) => 53 statusCode: res.statusCode 54 headers: res.headers 55 body: JSON.parse res.body 56 , (err) => 57 Q.reject err 58 res 42 59 60 createQueryObj = (obj) -> 61 newObj = {} 62 _.each obj, (val,key) -> 63 newObj[key] = JSON.stringify val 64 newObj 65 43 66 stringifyFunctions = (value) -> 44 67 if value is null
Note: See TracChangeset
for help on using the changeset viewer.