source: Dev/trunk/src/server/util/couch.coffee @ 475

Last change on this file since 475 was 475, checked in by hendrikvanantwerpen, 12 years ago
  • Added CouchDB script to make interacting with couch easier.
  • Use this script in the database configuration.
  • Get CSV export to work.
File size: 1.1 KB
RevLine 
[475]1request = require './q-request'
2_ = require 'underscore'
3Q = require 'q'
4
5class CouchDB
6    constructor: (url) ->
7        @url = normalizeURL url
8    get: (id) ->
9        url = "#{@url}/#{id}"
10        couchRequest 'GET', url
11    put: (id, doc) ->
12        url = "#{@url}/#{id}"
13        couchRequest 'PUT', url, doc
14
15normalizeURL = (url) ->
16    url.replace /\/$/, ''
17
18couchRequest = (method, url, data) ->
19    options =
20        method: method,
21        headers:
22            'content-type': 'application/json; charset=utf-8'
23            'accept': 'application/json'
24        body: JSON.stringify (stringifyFunctions (data || {}))
25    request(url, options)
26    .then (res) =>
27        JSON.parse res
28    , (err) =>
29        Q.reject (JSON.parse err)
30
31stringifyFunctions = (value) ->
32    if value is null
33        null
34    else if _.isArray value
35        value
36    else if _.isFunction value
37        value.toString()
38    else if _.isObject value
39        value = _.clone value
40        _.each value, (propValue, propName) =>
41            value[propName] = stringifyFunctions propValue
42        value
43    else
44        value
45
46exports.CouchDB = CouchDB
Note: See TracBrowser for help on using the repository browser.