Skip to content

Commit

Permalink
Merge pull request #18 from wymsee/andrew/sqlite
Browse files Browse the repository at this point in the history
added support for sqlite3 and fixed bugs when no parent class is provided
  • Loading branch information
allie-wake-up authored Apr 9, 2019
2 parents 70115e4 + 793dbfa commit 9a60c46
Show file tree
Hide file tree
Showing 6 changed files with 774 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ typings/
# dotenv environment variables file
.env

Session.vim
79 changes: 57 additions & 22 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ nconf.argv(yargs.options({
alias: 'b',
describe: 'Path to a file containing a class for the model to exend. The class should be the default export'
},
client: {
alias: 'c',
describe: 'The database client: mysql or sqlite3'
},
filename: {
alias: 'f',
describe: 'Path to a sqlite database file'
},
host: {
alias: 'h',
describe: 'Database host url'
Expand Down Expand Up @@ -105,7 +113,7 @@ nconf.argv(yargs.options({
if (nconf.get('envFile')) {
dotenv.config({ path: nconf.get('envFile') });
} else {
dotenv.config({ path: path.resolve(__dirname, '..', '..', '.env') });
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
}

nconf.env({
Expand Down Expand Up @@ -137,12 +145,13 @@ if (configFile) {
} else {
nconf.file({
file: 'typeormgen.json',
dir: __dirname,
dir: process.cwd(),
search: true
});
}

nconf.defaults({
client: 'mysql',
tabSize: 4
});

Expand All @@ -157,9 +166,11 @@ if (!nconf.get('model')) {
nconf.set('model', model);
}

const client = nconf.get('client');
const knex = Knex({
client: 'mysql',
client,
connection: {
filename: nconf.get('filename'),
host: nconf.get('host'),
port: nconf.get('port'),
user: nconf.get('user'),
Expand All @@ -168,24 +179,48 @@ const knex = Knex({
}
});

knex.raw(`DESCRIBE ${nconf.get('table')}`).then(([rows]) => {
const info = {};
rows.forEach(row => {
const openIndex = row.Type.indexOf('(');
info[row.Field] = {
type: openIndex > -1 ? row.Type.slice(0, openIndex - row.Type.length): row.Type,
length: parseInt(row.Type.slice(openIndex + 1, -1), 10),
nullable: row.Null === 'YES'
};
if (info[row.Field].type === 'decimal') {
info[row.Field].length += 1;
if (client === 'mysql') {
knex.raw(`DESCRIBE ${nconf.get('table')}`).then(([rows]) => {
const info = {};
rows.forEach(row => {
const openIndex = row.Type.indexOf('(');
info[row.Field] = {
type: openIndex > -1 ? row.Type.slice(0, openIndex - row.Type.length): row.Type,
length: parseInt(row.Type.slice(openIndex + 1, -1), 10),
nullable: row.Null === 'YES'
};
if (info[row.Field].type === 'decimal') {
info[row.Field].length += 1;
}
});
try {
writeModel(info, nconf);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit();
});
try {
writeModel(info, nconf);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit();
});
} else {
knex(nconf.get('table')).columnInfo().then(columns => {
const info = {};
Object.keys(columns).forEach(name => {
const column = columns[name];
info[name] = {
type: column.type,
length: parseInt(column.maxLength, 10),
nullable: column.nullable
};
if (info[name].type === 'decimal') {
info[name].length += 1;
}
});
try {
writeModel(info, nconf);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit();
});
}
Loading

0 comments on commit 9a60c46

Please sign in to comment.