forked from cybersonic/couchdbcfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchDB.cfc
128 lines (108 loc) · 3.44 KB
/
CouchDB.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
component{
function init(host="localhost", port="5984", db){
variables.host = host;
variables.port = port;
variables.db = db;
variables.mainurl = "http://#host#:#port#/";
variables.dbserver = "http://#host#:#port#/#db#";
return this;
}
/* Main http call for all functions */
function relax(stArgs, aParams=[]){
var relax = new HTTP(argumentCollection=stArgs);
for(p in aParams){
relax.addParam(p)
}
var result = relax.send().getPrefix().filecontent;
return DeSerializeJSON(result);
}
function getDocument(id){
return relax({url=variables.dbserver & "/" & id});
}
function getView(viewname, key="", designDocument="", group="false"){
if(!Len(designDocument)){
designDocument = viewname;
}
var dburl = variables.dbserver & "/_design/#designDocument#/_view/#viewname#";
// if group is true, then we don't bother with key
if(group){
dburl = dburl & '?group=true';
}else{
if(Len(key)){
dburl = dburl & '?key="#key#"';
}
}
return relax({url=dburl});
}
function getViewDesign(viewname){
return relax({url=variables.dbserver & "/_design/" & viewname});
}
function bulkDocs(Struct documents=[]){
var dburl = variables.dbserver & "/_bulk_docs";
var relax = new HTTP(url=dburl, method="POST");
relax.addParam(type="header", name="Content-Type", value="application/json");
relax.addParam(type="BODY", value=SerializeJSON(documents), label="text/json");
var res1 = relax.send().getPrefix();
return DeSerializeJSON(result);
}
function update(id, document){
var update = new HTTP(url="#variables.dbserver#/#arguments.id#", method="PUT");
update.addParam(type="BODY", value=SerializeJSON(document));
var insres = update.send().getPrefix();
return insres;
}
function insert(id, document){
//Get it first...
var check = new HTTP(url="#variables.dbserver#/#arguments.id#", method="GET").send().getPrefix();
//if it doesn't exist, safe to insert
if(check.status_code EQ 404){
var insert = new HTTP(url="#variables.dbserver#/#arguments.id#", method="PUT");
insert.addParam(type="BODY", value=SerializeJSON(document));
var res = insert.send().getPrefix();
}
//It must exist! Let's get the revision
else {
document["_rev"] = DeSerializeJSON(check.filecontent)._rev; // _rev is case-sensitive
//
var update = new HTTP(url="#variables.dbserver#/#arguments.id#", method="PUT");
update.addParam(type="BODY", value=SerializeJSON(document));
var insres = update.send().getPrefix();
}
}
function unsafe_insert(id, document){
var update = new HTTP(url="#variables.dbserver#/#arguments.id#", method="PUT");
update.addParam(type="BODY", value=SerializeJSON(document));
var insres = update.send().getPrefix();
return insres;
}
function deleteDatabase(){
var command = {
url=variables.dbserver,
method="DELETE"
};
return relax(command);
}
function createDatabase(){
var create = new HTTP(url="#variables.mainurl#/#variables.db#", method="PUT");
create.addParam(type="BODY", value=variables.db);
var rCreate = create.send().getPrefix();
return rCreate;
}
function delete(id, rev){
var command = {
url = variables.dbserver & "/" & arguments.id & "?rev=" & arguments.rev,
method = "delete"
};
return relax(command);
}
function deleteAll(){
var command = {
url=variables.dbserver & "/" & "_all_docs",
method="get"
}
var allDocs = relax(command);
for(var d in allDocs.rows){
delete(d.id, d.value.rev);
}
}
}