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
|
-module(fabric).
-export([all_databases/1, create_db/2, delete_db/2, get_db_info/2,
db_path/2]).
-export([open_doc/3, open_revs/4, update_doc/3, update_docs/3]).
-include("../../couch/src/couch_db.hrl").
% db operations
-spec db_path(bstring(), bstring()) -> bstring().
db_path(RawUri, Customer) ->
CustomerUri = generate_customer_path(RawUri, Customer),
{Path, _, _} = mochiweb_util:urlsplit_path(CustomerUri),
Path.
all_databases(Customer) ->
fabric_db:all_databases(Customer).
get_db_info(DbName, Customer) ->
fabric_db:get_db_info(dbname(DbName), Customer).
create_db(DbName, Options) ->
fabric_db:create_db(dbname(DbName), Options).
delete_db(DbName, Options) ->
fabric_db:delete_db(dbname(DbName), Options).
open_doc(DbName, Id, Options) ->
fabric_doc:open_doc(dbname(DbName), docid(Id), Options).
open_revs(DbName, Id, Revs, Options) ->
fabric_doc:open_revs(dbname(DbName), docid(Id), Revs, Options).
update_doc(DbName, Doc, Options) ->
{ok, [Result]} = update_docs(DbName, [Doc], Options),
Result.
update_docs(DbName, Docs, Options) ->
fabric_doc:update_docs(dbname(DbName), docs(Docs), Options).
%% some simple type validation and transcoding
dbname(DbName) when is_list(DbName) ->
list_to_binary(DbName);
dbname(DbName) when is_binary(DbName) ->
DbName;
dbname(DbName) ->
erlang:error({illegal_database_name, DbName}).
docid(DocId) when is_list(DocId) ->
list_to_binary(DocId);
docid(DocId) when is_binary(DocId) ->
DocId;
docid(DocId) ->
erlang:error({illegal_docid, DocId}).
docs(Docs) when is_list(Docs) ->
[doc(D) || D <- Docs];
docs(Docs) ->
erlang:error({illegal_docs_list, Docs}).
doc(#doc{} = Doc) ->
Doc;
doc({_} = Doc) ->
couch_doc:from_json_obj(Doc);
doc(Doc) ->
erlang:error({illegal_doc_format, Doc}).
generate_customer_path("/", _Customer) ->
"";
generate_customer_path("/favicon.ico", _Customer) ->
"favicon.ico";
generate_customer_path([$/,$_|Rest], _Customer) ->
lists:flatten([$_|Rest]);
generate_customer_path([$/|RawPath], Customer) ->
case Customer of
"" ->
RawPath;
Else ->
lists:flatten([Else, "%2F", RawPath])
end.
|