Dancer::Plugin::Database 0.01 released

Last night I finished and released the first version of Dancer::Plugin::Database, a plugin for the Dancer web framework to provide easy database connections using the venerable DBI.

It takes the database connection details from the app config file, and provides a database keyword which will return you a connected database handle (taking care of ensuring that the DB connection is still alive, etc).

So, usage can be as simple as:

my $books = database->selectall_arrayref(
    'select * from books where author = ?', 
    { Slice => {}}, $author
);

(Calling database() simply returns a DBI database handle, so you can obviously do anything you can do with DBI).

Also, at the moment, if a database connection could not be established for any reason, database() will just return undef, so you will need to handle errors appropriately. After considering whether it's wise, I may tweak it to die, so that Dancer will handle the failure with a pretty 500 error for you (in which case, I'll make it possible to disable that via the config).

2 Comments

Pretty cool ... I was starting to think about how to do DBI sessions in Dancer and you've started me off. Just one question though, why not use connect_cached from DBI instead of rolling your own?

Pretty cool ... I was starting to think about how to do DBI sessions in Dancer and you've started me off. Just one question though, why not use connect_cached from DBI instead of rolling your own?

Leave a comment

About David Precious

user-pic Professional Perl developer, Dancer core team member, beer drinker. See http://www.preshweb.co.uk/about for more :)