Open johnb8005 opened 4 years ago
// list of JVM types that are supported
const jvmTypes = ['Int', 'Long', 'Double', 'LocalDateTime', 'LocalDate', 'Boolean', 'BigDecimal', 'String'];
const jvmToSqlType = i => {
switch (i) {
case 'Int':
return 'INT';
case 'Long':
return 'BIGINT';
case 'Double':
return 'FLOAT';
//case 'DateTime': // datetime is not accepted
case 'LocalDateTime':
//return 'TIMESTAMP'; //TODO: Fix ambiguity between timestamp & datetime
return 'DATETIME';
case 'LocalDate':
return 'DATE';
case 'Boolean':
return 'BIT';
case 'BigDecimal':
return 'DECIMAL(12,4)';
case 'String':
return 'VARCHAR(512)'; //TODO: Fix hardcoded length
default:
console.log(`Couldn't translate "${i}" to SQL type, fallback to \`BIGINT\`.`);
return 'BIGINT';
}
};
const sqlToJvmType = i => {
switch (i.toLowerCase()) {
case 'int':
return 'Int';
case 'bigint':
return 'Long';
case 'timestamp':
return 'LocalDateTime';
case 'date':
return 'LocalDate';
case 'datetime':
return 'LocalDateTime';
case 'bit':
return 'Boolean';
case 'decimal':
return 'BigDecimal';
case 'float':
return 'Double';
case 'char':
case 'text':
case 'varchar':
return 'String';
default:
throw `Couldn't translate "${i}" to JVM type.`
}
};
const modelToSqlType = jvmToSqlType;
const modelToJvmType = (x) => x; // Currently equivalent
module.exports = {
jvmTypes,
sqlToJvmType,
jvmToSqlType,
modelToSqlType,
modelToJvmType
};
const Types = require('./types.js');
test('jvm To Sql Type', () => {
expect(Types.jvmToSqlType('String')).toBe('VARCHAR(512)')
});
test('Sql to jvm Type', () => {
expect(Types.sqlToJvmType('VARCHAR')).toBe('String')
});
const Utils = require('./utils.js');
test('camel case to snake case', () => {
expect(Utils.camelCaseToSnake('tableSource',)).toBe('table_source')
});
test('snake to camel', () => {
const a = Utils.camelCaseToSnake('motorRPM');
expect(a).toBe('motor_rpm');
const b = Utils.camelCaseToSnake('aLongVariable');
expect(b).toBe('a_long_variable');
});
test('add parenthesis', () => {
const x = {optional: true};
const p = Utils.addParenthesis(x, true);
const q = Utils.addParenthesis(x, false);
expect(p).toBe('Some(');
expect(q).toBe(')');
});
// test('random int', () => {
// const r = Utils.randomInt(1, 10);
//
// expect(typeof r).toBe('number');
// expect(r).toBeGreaterThan(0);
// expect(r).toBeLessThan(11);
// });
/lib/sql
=> PRmysqldump -u root -p --no-data dbname > schema.sql
https://stackoverflow.com/questions/6175473/mysql-export-schema-without-data
the library sql-ddl-to-json-schema does fails while parsing foreign keys
index.js
utils.js