You can do this with a source node set to dbQuery or through a mapping.
To do this with the source node and visual Channel editor:
For the source node set your format to "DB Query" and the incoming message and source will be a CSV message. Just set the destination to file and you have your CSV file
To do this in a mapping use the following code.
// run query and get results as csv message model.
var queryResults = qie.doQuery('dbConnectionName',
'select * from someTable');
//set message to csv
message = qie.createCSVMessage();
//set the query results to the message