Skip to content
This repository was archived by the owner on Apr 25, 2020. It is now read-only.
This repository was archived by the owner on Apr 25, 2020. It is now read-only.

mysql to ddl #3

@johnb8005

Description

@johnb8005

index.js

const Parser = require('sql-ddl-to-json-schema');
const fsp = require('fs').promises;

const Utils = require('./utils');
const Types = require('../types');


const transformColumn = (c) => {
  const {name, type, options} = c;

  const columnName = name;
  const fieldName = Utils.snakeCaseToCamel(name).replace(' ', '');

  const fieldType = Types.sqlToJvmType(type.datatype);

  const isOptional = options.nullable === true || ('default' in options);

  const columnInfo = {
    arg: fieldName,
    type: fieldType,
    optional: isOptional,
    uuid: false,
  };

  if (Utils.camelCaseToSnake(fieldName) !== columnName)
    columnInfo.column = columnName;

  return columnInfo;
};

const transformEntity = (entity) => {
  const {name, columns} = entity;

  const tableName = name;
  const entityName = Utils.capitalizeFirst(Utils.snakeCaseToCamel(name));

  const params = columns.map(transformColumn);

  if (!params.find(e => e.arg === 'id')) {
    return {
      warning: `${entityName} does not define a numeric id`
    }
  }

  const tableInfo = {
    name: entityName,
    params: params.filter(e => e.arg !== 'id')
  };

  if (Utils.camelCaseToSnake(entityName) !== tableName) {
    console.log(entityName)
    tableInfo.table = tableName;
  }

  return {
    entity: tableInfo
  }
};

const jsonFromDdlFile = async filepath => {
  const sql = await fsp.readFile(filepath, "utf8");

  const parser = new Parser('mysql').feed(sql);

  const sqlAbstractSyntaxTree = parser.results;
  const compactJsonTablesArray = parser.toCompactJson(sqlAbstractSyntaxTree);

  const transformedEntities = compactJsonTablesArray.map(transformEntity)

  return {
    entities: transformedEntities.filter(e => 'entity' in e).map(w => w.entity),
    warnings: transformedEntities.filter(e => 'warning' in e).map(w => w.warning),
    inputJson: compactJsonTablesArray,
    inputJsonAst: sqlAbstractSyntaxTree
  }
};

module.exports = {jsonFromDdlFile};

utils.js

const path = require('path');

/**
 *
 * @see https://stackoverflow.com/questions/30521224/javascript-convert-pascalcase-to-underscore-case
 */
const camelCaseToSnake = (snakeStr) => snakeStr.replace(/\.?([A-Z]+)/g, function (x, y) {
  return "_" + y.toLowerCase()
}).replace(/^_/, "");

// https://stackoverflow.com/questions/40710628/how-to-convert-snake-case-to-camelcase-in-my-app
const snakeCaseToCamel = (d) => {
  return d.replace(/(\_\w)/g, (k) => {
    return k[1].toUpperCase();
  })
};

const uncapitalizeFirst = (s) => {
  const f = s[0].toLocaleLowerCase();
  return f + s.slice(1);
};

const capitalizeFirst = (s) => {
  const f = s[0].toLocaleUpperCase();
  return f + s.slice(1);
};

const addParenthesis = (x, preOrPost) => {
  const getParenthesis = p => {
    return preOrPost ? 'Some(' : ')';
  };

  if (x.optional) {
    return getParenthesis(preOrPost);
  }

  return '';
};

function ensureDirectoryExistence(filePath) {
  const dirname = path.dirname(filePath);
  if (fs.existsSync(dirname)) {
    return true;
  }
  ensureDirectoryExistence(dirname);
  fs.mkdirSync(dirname);
}

const saveFile = (filepath, content) => {
  console.log(`Writing ${filepath}`);
  ensureDirectoryExistence(filepath);
  fs.writeFile(filepath, content, err => {
    if (err) {
      return console.log(err);
    }
  });
};

module.exports = {
  camelCaseToSnake,
  snakeCaseToCamel,
  uncapitalizeFirst,
  capitalizeFirst,
  addParenthesis,
  saveFile
};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions