[erlang-questions] I need some advices for a framework I'm doing

Ivan Carmenates Garcia <>
Tue Aug 25 20:11:50 CEST 2015


I think the idea of an alias is not so bad, that's what I did.

 

Before I had

{database_manager, [
    {postgres_backend, [
        {server, "localhost"},
        {username, "postgres"},
        {password, "server"},
        {database, "eoc_db"},
        %% max amount of database connections in the connection pool.
        {max_reusable_connections, 10}, % 10 connections.
        %% max time it will wait for an available connection.
        {wait_for_reusable_connection_timeout, 5000} % 5 seconds.
    ]}
]},

 

Now I have:

{database_manager, [
    {main_backend, [
        {backend, postgres_backend},
        {server, "localhost"},
        {username, "postgres"},
        {password, "server"},
        {database, "eoc_db"},
        %% max amount of database connections in the connection pool.
        {max_reusable_connections, 10}, % 10 connections.
        %% max time it will wait for an available connection.
        {wait_for_reusable_connection_timeout, 5000} % 5 seconds.
    ]}
]},

 

Then I can safely use 

database:connection_session(fun(DBSession)-> . end, main_backend).

 

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.

 

 

Cheers,

Ivan.

 

 

From: 
[mailto:] On Behalf Of Ivan Carmenates
Garcia
Sent: Monday, August 24, 2015 4:21 PM
To: 
Subject: [erlang-questions] I need some advices for a framework I'm doing

 

Hi everyone, I am making a little framework for cowboy, but in general what
I need is some advices regards final looking, i.e.:

I have implemented a multi database backend connection pool that manages
available connections for me. Here is an example:

 

database_manager:connection_session(fun(DBSession) ->
        {ok, 1, #{id : UserID}} =
            database_manager:insert_and_return(DBSession,
                {users, #{
                    username => security_lib:generate_unique_id(),
                    password =>
security_lib:hash_password(security_lib:generate_unique_id()),
                    password_old =>
security_lib:hash_password(security_lib:generate_unique_id()),
                    online => true,
                    last_online_date => datetime_lib:universal_datetime(),
                    created_date => datetime_lib:universal_datetime(),
                    activation_email => security_lib:generate_unique_id() ++
"@eoc.com"}},
                %% returning id!!!
                [id], [{result_format, map}]),


        {ok, 1} = database_manager:insert(DBSession,
            {user_activation_tokens, #{
                user_id => UserID,
                activation_token =>
security_lib:hash_random(integer_to_list(UserID))
            }})
end, postgres_backend),

 

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.

 

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.

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.

 

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 ;). 

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.

 

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.

 

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.

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150825/bbd5effd/attachment.htm>


More information about the erlang-questions mailing list