Skip to content

Commit b127f01

Browse files
committed
added --modularize option
1 parent 23a4ebe commit b127f01

File tree

5 files changed

+87
-23
lines changed

5 files changed

+87
-23
lines changed

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,32 @@ Omitting it will assume a schema of "public".
5959

6060
#### `--help`
6161
```
62-
Usage: node-sql-generate [options]
62+
Usage: node-sql-generate.js [options]
6363
6464
Options:
6565
66-
-h, --help utput usage information
66+
-h, --help output usage information
6767
-V, --version output the version number
68+
--dsn <dsn> Connection string
6869
-d, --dialect <dialect> Specify the SQL dialect: "mysql" or "pg"
6970
-o, --output-file <file> Output to this file; defaults to stdout
7071
-i, --indent <token> Indentation token; defaults to a TAB character
7172
-D, --database <name> Name of database to extract from
7273
-s, --schema <name> Name of schema to extract from (Postgres only)
73-
--camelize Convert underscored names to camel case ("foo_bar" -> "fooBar")
74+
--camelize Convert underscored names to camel case, requires sql >= 0.18.0"
7475
--eol <token> Line terminator token; defaults to "\n"
7576
--mode <mode> The permission mode of the generated file; defaults to 0644
7677
--encoding <encoding> The encoding to use for writing; defaults to "utf8"
7778
--prepend <text> Prepend text to the beginning of the file
7879
--append <text> Append text to the end of the file
79-
--dsn <dsn> Connection string
80+
--modularize Omit "require('sql')" and wrap generated code in "module.exports = function(sql) {...}"
8081
--omit-comments Omit autogenerated comments
82+
--include-schema Include schema in definition
8183
-v, --verbose Print debugging information
8284
8385
Example DSN:
84-
PostgreSQL: "postgres://user:password@host/database"
85-
MySQL: "mysql://user:password@host/database"
86+
PostgreSQL: "postgres://user:password@host:5432/database"
87+
MySQL: "mysql://user:password@host:3306/database"
8688
```
8789

8890
### API

bin/node-sql-generate.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ program
1717
.option('--encoding <encoding>', 'The encoding to use for writing; defaults to "utf8"', 'utf8')
1818
.option('--prepend <text>', 'Prepend text to the beginning of the file')
1919
.option('--append <text>', 'Append text to the end of the file')
20+
.option('--modularize', 'Omit "require(\'sql\')" and wrap generated code in "module.exports = function(sql) {...}"')
2021
.option('--omit-comments', 'Omit autogenerated comments', false)
2122
.option('--include-schema', 'Include schema in definition', false)
2223
.option('-v, --verbose', 'Print debugging information', false)

index.js

+39-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var supportedDialects = {
2424
* @param {String} [options.append] String to append to the end of the generated output
2525
* @param {String} [options.omitComments] Omit autogenerated comments
2626
* @param {String} [options.includeSchema] Include schema in definition
27+
* @param {String} [options.modularize] Omit require('sql') and wrap generated code in module.exports = function() {...}
2728
* @param {function} [options.log] Logging function
2829
* @param {Function} [callback]
2930
*/
@@ -285,9 +286,15 @@ module.exports = function(options, callback) {
285286
});
286287
}
287288

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+
}
291298

292299
async.series(functions, next);
293300
}
@@ -301,7 +308,9 @@ module.exports = function(options, callback) {
301308

302309
function processTables(next) {
303310
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 : '';
305314
log('info', 'Starting ' + options.schema + '.' + tableName + '...');
306315
getListOfColumns(tableName, function(err, columnData) {
307316
if (err) {
@@ -315,20 +324,20 @@ module.exports = function(options, callback) {
315324
fullName = (options.schema || options.database) + '.' + tableName;
316325

317326
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 + ' */');
321330
}
322331

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 + '\',');
325334
if (options.includeSchema) {
326-
args.push(options.indent + 'schema: \'' + (options.schema || options.database) + '\',');
335+
args.push(indent + options.indent + 'schema: \'' + (options.schema || options.database) + '\',');
327336
}
328-
args.push(options.indent + 'columns: [');
337+
args.push(indent + options.indent + 'columns: [');
329338

330339
args.push(columnData.map(function(column) {
331-
var columnString = options.indent + options.indent + '{ ' +
340+
var columnString = indent + options.indent + options.indent + '{ ' +
332341
'name: \'' + column.name + '\'';
333342

334343
if (options.camelize) {
@@ -339,8 +348,8 @@ module.exports = function(options, callback) {
339348
return columnString;
340349
}).join(',' + options.eol));
341350

342-
args.push(options.indent + ']');
343-
args.push('});');
351+
args.push(indent + options.indent + ']');
352+
args.push(indent + '});');
344353
args.push(options.eol);
345354

346355
args.push(function(err) {
@@ -359,10 +368,23 @@ module.exports = function(options, callback) {
359368
}
360369

361370
function writeTail(next) {
362-
if (options.append) {
363-
write(options.append, next);
371+
if (options.modularize) {
372+
write('};', tail);
364373
} 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+
}
366388
}
367389
}
368390

tests/expected/modularize.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
module.exports = function(sql) {
3+
4+
5+
/**
6+
* SQL definition for node_sql_generate.bar
7+
*/
8+
exports.bar = sql.define({
9+
name: 'bar',
10+
columns: [
11+
{ name: 'id' },
12+
{ name: 'foo_id' }
13+
]
14+
});
15+
16+
17+
/**
18+
* SQL definition for node_sql_generate.foo
19+
*/
20+
exports.foo = sql.define({
21+
name: 'foo',
22+
columns: [
23+
{ name: 'id' },
24+
{ name: 'field_1' },
25+
{ name: 'foo_bar_baz' }
26+
]
27+
});
28+
29+
30+
};

tests/generator-tests.js

+9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ describe('generator', function() {
223223
done();
224224
});
225225
});
226+
227+
it('with modularization', function(done) {
228+
generate(options({ modularize: true }, defaults), function(err, stats) {
229+
should.not.exist(err);
230+
var expected = getExpected('modularize');
231+
removeAutogeneratedComment(stats.buffer).should.equal(expected);
232+
done();
233+
});
234+
});
226235
});
227236
}(dsn, db, dialect));
228237
}

0 commit comments

Comments
 (0)