Friday, July 01, 2005

Managing developer datasources

If you're like me, you wind up working on lots of different projects with all sorts of datasuppliers. If you work on a laptop, you may be connected to several networks at different times. This creates an irritating little problem when you go to crank up a development session. ColdFusion doesn't ignore all of those datasources you created to work on other projects that you currently don't have a connection to.

Waiting for the timeouts while RDS is looking to fetch descriptions can feel like an eternity. In the past, I've just gone into the administrator on my workstation and added and deleted them as needed. That works if you only have to deal with a few, but it quickly becomes a big waste of development time.

Well, there is a neat way to manage the datasources in a heartbeat.

Inside each application I'm working on, I create a file like this and bookmark it:

<title>MyApp Datasources</title>
<h1>Manage Datasources for MyApp</h1>
<!--- try not to destroy the production server --->
<cfif cgi.HTTP_HOST eq "" or cgi.HTTP_host eq "localhost">

<!--- Login --->
<cfinvoke component="CFIDE.adminapi.administrator" method="login">
<cfinvokeargument name="adminPassword" value="your_cfide_admin_password"/>

<!--- Get a list of existing datasources --->
<cfinvoke component="CFIDE.adminapi.datasource" method="getDatasources" returnvariable="getDatasourcesRet"> </cfinvoke>

<!--- Delete all of them --->
<cfloop index="dsn" list="#structKeyList(getDatasourcesRet)#">
<cfinvoke component="CFIDE.adminapi.datasource" method="deleteDatasource">
<cfinvokeargument name="dsnname" value="#dsn#"/>

<!--- Create your datasource(s) here --->
<cfinvoke component="CFIDE.adminapi.datasource" method="setMSSQL">
<cfinvokeargument name="name" value="your_dsn_name"/>
<cfinvokeargument name="host" value="your_server_name_or_ip"/>
<cfinvokeargument name="database" value="your_db_name"/>
<cfinvokeargument name="selectmethod" value="direct"/>
<cfinvokeargument name="username" value="your_db_username"/>
<cfinvokeargument name="password" value="your_db_password">
<p>Datasource(s) added.</p>
<p>You can only manage datasources on the localhost</p>

All of this is now possible with the (relatively) new cfide api that cf exposes. You will have to use the setDB method that is appropriate for the type of db you are using. I'm only using a minimum of the available properties here, but everything in administrator is exposed.

Take a look at the component browser for details about other functions

They are contained in the CFIDE.adminapi package.

Note: if you may not be able to put the file in a part of your application that is controlled with user authentication that uses the datasource to validate usernames and passwords. In that case, you may want to set up a little app that just has these files in it and name them for each app.

Note #2: Dreamweaver won't acutally update the datasources shown no matter how many times you hit the refresh option. Trust me, they are not there. The new list will show up the next time the program is restarted. Your new datasources will show up with a refresh though.


  1. Great Piece of Code!

    Thanks for your hard work!


  2. Thanks, Ernie!

    I'm glad you could make use of the idea. Being mobile sometimes has its drawbacks.