From 3e681bcc77dfe553b74bbceb961be483e0b32b81 Mon Sep 17 00:00:00 2001 From: Anton Avramov Date: Sat, 20 Jul 2019 11:17:12 -0400 Subject: [PATCH 1/2] Add option for overwriting guesses with known string columns --- bin/tables.js | 10 +++++++++- lib/guess-model.js | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/tables.js b/bin/tables.js index 44377e5..22cb2ac 100755 --- a/bin/tables.js +++ b/bin/tables.js @@ -69,6 +69,11 @@ command.option( expression that starts with "/", for example "/^(key|column)$" or can be a comma-delimited list of columns such as "key|column_1" or ".*key|.*id".` ); +command.option( + '-s, --string-columns [name]', + `A comma-delimited list of columns that would be treated as string instead of guessed. + Only the length of the string would be guessed.` +); command.option( '-t, --transformer [file]', `Reference to JS file that exports a function to transform data @@ -150,6 +155,9 @@ async function cli() { key: command.key ? _.map(command.key.split(','), d => _.snakeCase(d.trim())) : undefined, + stringColumns: command.stringColumns + ? _.map(command.stringColumns.split(','), d => _.snakeCase(d.trim())) + : [], id: command.id ? command.id : undefined, fieldsToIndex: command.indexFields && command.indexFields.match(/^\//) @@ -170,7 +178,7 @@ async function cli() { transformer: command.transformer ? command.transformer : undefined, models: command.models ? command.models : undefined }; - + // Input type specific options if (command.jsonPath) { options.format = 'json'; diff --git a/lib/guess-model.js b/lib/guess-model.js index 923e0af..950a48a 100644 --- a/lib/guess-model.js +++ b/lib/guess-model.js @@ -129,9 +129,12 @@ function dataToType(data, name, tablesOptions = {}) { let maxLength = _.maxBy(data, d => (d && d.length ? d.length : 0)); maxLength = maxLength ? maxLength.length : maxLength; let kind; - + // If string is passed as option we just use that + if (tablesOptions.stringColumns.indexOf(_.snakeCase(name)) != -1) { + kind = 'STRING'; + } // If none, then just assume string - if (_.size(data) === 0) { + else if (_.size(data) === 0) { return Sequelize.STRING; } // If there is only one kind, stick with that From 0378f2abf01e70f9748f580f6badd250ad8488e2 Mon Sep 17 00:00:00 2001 From: Anton Avramov Date: Sat, 20 Jul 2019 16:00:12 -0400 Subject: [PATCH 2/2] Fix compatability issue --- bin/tables.js | 2 +- lib/guess-model.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/tables.js b/bin/tables.js index 22cb2ac..2f74886 100755 --- a/bin/tables.js +++ b/bin/tables.js @@ -157,7 +157,7 @@ async function cli() { : undefined, stringColumns: command.stringColumns ? _.map(command.stringColumns.split(','), d => _.snakeCase(d.trim())) - : [], + : undefined, id: command.id ? command.id : undefined, fieldsToIndex: command.indexFields && command.indexFields.match(/^\//) diff --git a/lib/guess-model.js b/lib/guess-model.js index 950a48a..421d38e 100644 --- a/lib/guess-model.js +++ b/lib/guess-model.js @@ -130,7 +130,7 @@ function dataToType(data, name, tablesOptions = {}) { maxLength = maxLength ? maxLength.length : maxLength; let kind; // If string is passed as option we just use that - if (tablesOptions.stringColumns.indexOf(_.snakeCase(name)) != -1) { + if (tablesOptions.stringColumns && tablesOptions.stringColumns.indexOf(_.snakeCase(name)) != -1) { kind = 'STRING'; } // If none, then just assume string