 db.setMaxListeners 100
 
 fs.ensureDirSync config.dir
+
+
+# TODO add tests
+getByKey = (query, key, callback) ->
+    params =
+        include_docs: true
+        key: key
+    db.query query, params, (err, docs) ->
+        if err
+            callback err
+        else if docs.rows.length is 0
+            callback()
+        else
+            callback null,  docs.rows[0].value
 
 
 module.exports = dbHelpers =

            dbHelpers.db.query 'folder/all', params, callback

        get: (key, callback) ->
            params =
                include_docs: true
                key: key
            db.query 'folder/byFullPath', params, (err, docs) ->
                if err
                    callback err
                else if docs.rows.length is 0
                    callback()
                else
                    callback null,  docs.rows[0].value

        upsert: (newDoc, callback) ->
            key = "#{newDoc.path}/#{newDoc.name}"

                params = {}
            dbHelpers.db.query 'file/all', params, callback
        get: (key, callback) ->
            getByKey 'file/byFullPath', key, callback

    folders:
        rows: []
