<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:"Courier New";}
span.EmailStyle19
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.EmailStyle21
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='font-size:12.0pt'>I think the idea of an alias is not so bad, that’s what I did.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'>Before I had<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'>{database_manager, [<br> {postgres_backend, [<br> {server, "localhost"},<br> {username, "postgres"},<br> {password, "server"},<br> {database, "eoc_db"},<br> %% max amount of database connections in the connection pool.<br> {max_reusable_connections, 10}, % 10 connections.<br> %% max time it will wait for an available connection.<br> {wait_for_reusable_connection_timeout, 5000} % 5 seconds.<br> ]}<br>]},<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'>Now I have:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'> <o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'>{database_manager, [<br> {main_backend, [<br> {backend, postgres_backend},<br> {server, "localhost"},<br> {username, "postgres"},<br> {password, "server"},<br> {database, "eoc_db"},<br> %% max amount of database connections in the connection pool.<br> {max_reusable_connections, 10}, % 10 connections.<br> %% max time it will wait for an available connection.<br> {wait_for_reusable_connection_timeout, 5000} % 5 seconds.<br> ]}<br>]},<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Then I can safely use <o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'>database:connection_session(fun(DBSession)-> … end, main_backend).<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>So if I have to change the database I only need to change the configuration for main_backend to use anther backend module with other config. That enables me to have multiple postgres backend for example under different alias. And I could name backends with names I like, ie, main_backend, backup_backend, other_stuffs.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Cheers,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Ivan.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b>From:</b> erlang-questions-bounces@erlang.org [mailto:erlang-questions-bounces@erlang.org] <b>On Behalf Of </b>Ivan Carmenates Garcia<br><b>Sent:</b> Monday, August 24, 2015 4:21 PM<br><b>To:</b> erlang-questions@erlang.org<br><b>Subject:</b> [erlang-questions] I need some advices for a framework I'm doing<o:p></o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='font-size:12.0pt'>Hi everyone, I am making a little framework for cowboy, but in general what I need is some advices regards final looking, i.e.:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>I have implemented a multi database backend connection pool that manages available connections for me. Here is an example:<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='font-family:Consolas'>database_manager:connection_session(fun(DBSession) -><br> {ok, 1, #{id : UserID}} =<br> database_manager:insert_and_return(DBSession,<br> {users, #{<br> username => security_lib:generate_unique_id(),<br> password => security_lib:hash_password(security_lib:generate_unique_id()),<br> password_old => security_lib:hash_password(security_lib:generate_unique_id()),<br> online => true,<br> last_online_date => datetime_lib:universal_datetime(),<br> created_date => datetime_lib:universal_datetime(),<br> activation_email => security_lib:generate_unique_id() ++ "@eoc.com"}},<br> %% returning id!!!<br> [id], [{result_format, map}]),<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:Consolas'><br> {ok, 1} = database_manager:insert(DBSession,<br> {user_activation_tokens, #{<br> user_id => UserID,<br> activation_token => security_lib:hash_random(integer_to_list(UserID))<br> }})<br>end, postgres_backend),<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>database_manager:connection_session/2 gives me an available connection for a postgres backend in this case, and inside of It I can make any query I want, using DBSession information to redirect the query to the appropriate backend. So anywhere I want I can call database_manager:connection_session/2 to obtain a database connection from any backend, I have to specify the backend I will use at the end, because I could simultaneously use the backend I want to access many different database systems at the same time. But later I think that if writing all over the code the backend I will use, then if I want to change the database I have to check all the source code and changes the backend, later I came up with an idea of setting an alias and use the alias instead so in the app.config file I just need to change the database configuration and backend for an specific alias, but I don’t know, seems too much work to do to implement it.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>What I want to know is that if as it is seems look nice and is a good idea to have a connection_session available all over the place, because other framework what they do is simply call an insert or a find function and the connection is managed under the hood, that seems fine, but I don’t know why I like my implementation, seems to me like I have the control over the connection I want and use it until I want.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>You can also choose in each function insert or find how you want the result data to be, if in map format or proplist or simple raw as the database driver says.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>I have another interesting things like handler/action routes that matches a handler module and a function as an action, pretty similar to ChicagoBoss, but just I like it more ;). <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>And the last thing I do, is that you can change any of your code in the fly, just by refreshing the page and just the recently changed code got compiled and reload. Pretty fast for development mode proposes, for production I just need to call ec_control:recompile_all() and has the same effect than refreshing the page but only that it is when I wanted. Work pretty well to me.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>I also have sessions mechanism, url parameters like /param-value1-value2/param2-value3… etc and many other interesting things. Today I even don’t sleep programing, lol, I like when things get interesting.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Well anybody could give some advices to improve it, I will put it on github as soon as it is ready at least to test it. I also need to implement other backends, it is pretty easy because it is a template module free of dirty code, simple as putting a name you want and implement a certain interface, and later just use the same name in connection_session/2 calls, and database manager manages to redirect and handles the calls and connection pool, you need also to install it, calling database_manager:install_backend/2 to create its connection pool and all its stuffs, you can remove it the same.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>