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
function CouchDB(database)
{
	this.query = function (type, command, data, successCallback, errorCallback)
	{
		$.ajax(
		{
			contentType: typeof data !== "undefined" && (type === "POST" || type === "PUT") ? "application/json" : "application/x-www-form-urlencoded",
			data: typeof data !== "undefined" && (type === "POST" || type === "PUT") ? JSON.stringify(data) : {},
			dataType: "json",
			error: function () { typeof errorCallback !== "undefined" ? errorCallback(arguments) : function () {} },
			global: false,
			success: function () { successCallback(arguments) },
			timeout: 15000,
			type: type,
			url: database + (typeof command !== "undefined" ? command : "")
		});
	};
	
	
	return {
		crud: function (type, command, data, successCallback, errorCallback)
		{
			query(type, command, data, successCallback, errorCallback);
		},
			
		create: function (data, successCallback, errorCallback)
		{
			query("POST", undefined, data, successCallback, errorCallback);
		},
			
		read: function ()
		{
			if (typeof arguments[0] !== "undefined")
			{
				if (typeof arguments[0] === "function")
				{
					query("GET", "_all_docs", undefined, arguments[0], arguments[1]);
				}
				else
				{
					query("GET", arguments[0], undefined, arguments[1], arguments[2]);
				}
			}
		},
		
		update: function (id, data, successCallback, errorCallback)
		{
			query
			(
				"GET",
				id,
				undefined,
				function (_data)
				{
					data._rev = _data[0]._rev;
					
					query("PUT", id, data, successCallback, errorCallback);
				},
				errorCallback
			);
		},
		
		"delete": function (id, successCallback, errorCallback)
		{
			query("DELETE", id, undefined, successCallback, errorCallback);
		}
	};
}