欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

couchbeam应用 博客分类: couchdb CouchDBErlangjsonSecurity 

程序员文章站 2024-03-23 17:12:28
...

1、couchbeam是基于erlang的couchdb客户端.couchbeam是一个otp应用程序,所以在使用前必需先启动couchbeam

couchbeam:start().


2、创建数据库的连接

%% 如需要权限,請看下方Option的basic_auth
Host = "localhost",
Port = 5984,
Prefix = "",
Options = [],
S = couchbeam:server_connection(Host, Port, Prefix, Options).
%% @spec server_connection(Host::string(), Port::integer(),
%%                        Prefix::string(), Options::optionList()) 
%%                        -> Server::server()
%% optionList() = [option()]
%% option() =
%%          {is_ssl, boolean()}                |
%%          {ssl_options, [SSLOpt]}            |
%%          {pool_name, atom()}                |
%%          {proxy_host, string()}             |
%%          {proxy_port, integer()}            |
%%          {proxy_user, string()}             |
%%          {proxy_password, string()}         |
%%          {basic_auth, {username(), password()}} |
%%          {cookie, string()}                 |
%%          {oauth, oauthOptions()}
%%
%% username() = string()
%% password() = string()
%% SSLOpt = term()
%% oauthOptions() = [oauth()]
%% oauth() = 
%%          {consumer_key, string()} |
%%          {token, string()} |
%%          {token_secret, string()} |
%%          {consumer_secret, string()} |
%%          {signature_method, string()}
%%


3、打开或创建一个数据库

ptions = [],
{ok, Db} = couchbeam:open_db(Server, "testdb", Options).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Options = [],
{ok, Db} = couchbeam:create_db(Server, "testdb", Options).


4. 创建一个文档

Doc = {[
{<<"_id">>, <<"test">>},
{<<"content">>, <<"some text">>}
]}.
{ok, Doc1} = couchbeam:save_doc(Db, Doc).


5. 检索文件

{ok, Doc2} = couchbeam:open_doc(Db, "test").
%%If you want a specific revision:
Rev = couchbeam_doc:get_rev(Doc1),
Options = [{rev, Rev}],
{ok, Doc3} = couchbeam:open_doc(Db, "test", Options).


6. 取得所有文档

Options = [{include_docs, "true"}],
{ok, AllDocs} = couchbeam:all_docs(Db, Options).


或者:

{ok, Results} = couchbeam_view:fetch(AllDocs).


7. Couchdb views - Map/Reduce

Options = [],
DesignNam = "designname",
ViewName = "viewname",
{ok, View} = couchbeam:view(Db, {DesignNam, ViewName}, Options).


8. Put,Fetch and Delete documents attachments

%% 上传一个附件
DocID = "test",
AttName = "test.txt",
Att = "some content I want to attach",
Options = []
{ok, _Result} = couchbeam:put_attachment(Db, DocId, AttName, Att, Options).
%% 取出一个附件
{ok Att1} = couchbeam:fetch_attachment(Db, DocId, AttName).
%% 删除一个附件
{ok, Doc4} = couchbeam:open_doc(Db, DocID),
ok = couchbeam:delete_attachment(Db, Doc4, AttName).


9. Changes

Options = [],
{ok, Changes} = couchbeam:changes_wait_once(Db, Options).


10、如想修改数据库的admins/readers,在couchbeam没找到现成方法,望知道的留言相告.简单实现了下

%%  add auth to database	
	_json = "{\"admins\":{\"names\":[\"user1\"],\"roles\":[]},\"readers\":{\"names\":[\"user2\"],\"roles\":[]}}",
	Result = couchbeam:db_request(put, "http://127.0.0.1:5984/d2/_security",[], Options,[],erlang:list_to_binary(_json)),


couchdb权限介紹