@@ -24,6 +24,7 @@ var supportedDialects = {
24
24
* @param {String } [options.append] String to append to the end of the generated output
25
25
* @param {String } [options.omitComments] Omit autogenerated comments
26
26
* @param {String } [options.includeSchema] Include schema in definition
27
+ * @param {String } [options.modularize] Omit require('sql') and wrap generated code in module.exports = function() {...}
27
28
* @param {function } [options.log] Logging function
28
29
* @param {Function } [callback]
29
30
*/
@@ -285,9 +286,15 @@ module.exports = function(options, callback) {
285
286
} ) ;
286
287
}
287
288
288
- functions . push ( function ( next ) {
289
- write ( 'var sql = require(\'sql\');' , options . eol , next ) ;
290
- } ) ;
289
+ if ( options . modularize ) {
290
+ functions . push ( function ( next ) {
291
+ write ( 'module.exports = function(sql) {' , options . eol , next ) ;
292
+ } ) ;
293
+ } else {
294
+ functions . push ( function ( next ) {
295
+ write ( 'var sql = require(\'sql\');' , options . eol , next ) ;
296
+ } ) ;
297
+ }
291
298
292
299
async . series ( functions , next ) ;
293
300
}
@@ -301,7 +308,9 @@ module.exports = function(options, callback) {
301
308
302
309
function processTables ( next ) {
303
310
function writeTable ( tableName , next ) {
304
- var start = Date . now ( ) ;
311
+ var start = Date . now ( ) ,
312
+ //indent everything by one if modularize is set
313
+ indent = options . modularize ? options . indent : '' ;
305
314
log ( 'info' , 'Starting ' + options . schema + '.' + tableName + '...' ) ;
306
315
getListOfColumns ( tableName , function ( err , columnData ) {
307
316
if ( err ) {
@@ -315,20 +324,20 @@ module.exports = function(options, callback) {
315
324
fullName = ( options . schema || options . database ) + '.' + tableName ;
316
325
317
326
if ( ! options . omitComments ) {
318
- args . push ( '/**' ) ;
319
- args . push ( ' * SQL definition for ' + fullName ) ;
320
- args . push ( ' */' ) ;
327
+ args . push ( indent + '/**' ) ;
328
+ args . push ( indent + ' * SQL definition for ' + fullName ) ;
329
+ args . push ( indent + ' */' ) ;
321
330
}
322
331
323
- args . push ( 'exports.' + camelize ( tableName ) + ' = sql.define({' ) ;
324
- args . push ( options . indent + 'name: \'' + tableName + '\',' ) ;
332
+ args . push ( indent + 'exports.' + camelize ( tableName ) + ' = sql.define({' ) ;
333
+ args . push ( indent + options . indent + 'name: \'' + tableName + '\',' ) ;
325
334
if ( options . includeSchema ) {
326
- args . push ( options . indent + 'schema: \'' + ( options . schema || options . database ) + '\',' ) ;
335
+ args . push ( indent + options . indent + 'schema: \'' + ( options . schema || options . database ) + '\',' ) ;
327
336
}
328
- args . push ( options . indent + 'columns: [' ) ;
337
+ args . push ( indent + options . indent + 'columns: [' ) ;
329
338
330
339
args . push ( columnData . map ( function ( column ) {
331
- var columnString = options . indent + options . indent + '{ ' +
340
+ var columnString = indent + options . indent + options . indent + '{ ' +
332
341
'name: \'' + column . name + '\'' ;
333
342
334
343
if ( options . camelize ) {
@@ -339,8 +348,8 @@ module.exports = function(options, callback) {
339
348
return columnString ;
340
349
} ) . join ( ',' + options . eol ) ) ;
341
350
342
- args . push ( options . indent + ']' ) ;
343
- args . push ( '});' ) ;
351
+ args . push ( indent + options . indent + ']' ) ;
352
+ args . push ( indent + '});' ) ;
344
353
args . push ( options . eol ) ;
345
354
346
355
args . push ( function ( err ) {
@@ -359,10 +368,23 @@ module.exports = function(options, callback) {
359
368
}
360
369
361
370
function writeTail ( next ) {
362
- if ( options . append ) {
363
- write ( options . append , next ) ;
371
+ if ( options . modularize ) {
372
+ write ( '};' , tail ) ;
364
373
} else {
365
- process . nextTick ( next ) ;
374
+ tail ( ) ;
375
+ }
376
+
377
+ function tail ( err ) {
378
+ if ( err ) {
379
+ next ( err ) ;
380
+ return ;
381
+ }
382
+
383
+ if ( options . append ) {
384
+ write ( options . append , next ) ;
385
+ } else {
386
+ process . nextTick ( next ) ;
387
+ }
366
388
}
367
389
}
368
390
0 commit comments