Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to convert JSON schema to Django models #19 #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
jsonToSQLite,
jsonToMariaDB,
jsonToSQLServer,
jsonToDjangoModels
} from "../../utils/toSQL";
import {
ObjectType,
Expand Down Expand Up @@ -933,6 +934,21 @@ export default function ControlPanel({
}));
},
},
{
"Django Models": () => {
setModal(MODAL.CODE);
const src = jsonToDjangoModels({
tables: tables,
references: relationships,
types: types,
});
setExportData((prev) => ({
...prev,
data: src,
extension: "py",
}));
},
}
],
function: () => {},
},
Expand Down
41 changes: 40 additions & 1 deletion src/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,43 @@ export const SIDESHEET = {
NONE: 0,
TODO: 1,
TIMELINE: 2,
};
};

export const SQL_TO_DJANGO_TYPE_MAPPING = {
'INT': 'IntegerField',
'TINYINT': 'SmallIntegerField',
'SMALLINT': 'SmallIntegerField',
'MEDIUMINT': 'IntegerField',
'BIGINT': 'BigIntegerField',
'FLOAT': 'FloatField',
'DOUBLE': 'FloatField',
'DECIMAL': 'DecimalField',
'DATE': 'DateField',
'TIME': 'TimeField',
'DATETIME': 'DateTimeField',
'TIMESTAMP': 'DateTimeField',
'YEAR': 'IntegerField',
'CHAR': 'CharField',
'VARCHAR': 'CharField',
'TEXT': 'TextField',
'BLOB': 'BinaryField',
'MEDIUMBLOB': 'BinaryField',
'LONGBLOB': 'BinaryField',
'TINYBLOB': 'BinaryField',
};

export const SQL_TO_DJANGO_DELETE_CONSTRAINT_MAPPING = {
'NO ACTION': 'DO_NOTHING',
'RESTRICT': 'PROTECT',
'CASCADE': 'CASCADE',
'SET NULL': 'SET_NULL',
'SET DEFAULT': 'SET_DEFAULT',
};

export const SQL_TO_DJANGO_UPDATE_CONSTRAINT_MAPPING = {
'NO ACTION': 'DO_NOTHING',
'RESTRICT': 'PROTECT',
'CASCADE': 'CASCADE',
'SET NULL': 'SET_NULL',
'SET DEFAULT': 'SET_DEFAULT',
};
77 changes: 75 additions & 2 deletions src/utils/toSQL.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { sqlDataTypes } from "../data/constants";
import { sqlDataTypes, SQL_TO_DJANGO_TYPE_MAPPING,
SQL_TO_DJANGO_DELETE_CONSTRAINT_MAPPING,
SQL_TO_DJANGO_UPDATE_CONSTRAINT_MAPPING }
from "../data/constants";
import { strHasQuotes } from "./utils";

export function getJsonType(f) {
if (!sqlDataTypes.includes(f.type)) {
return '{ "type" : "object", additionalProperties : true }';
Expand Down Expand Up @@ -397,6 +399,77 @@ export function jsonToSQLite(obj) {
.join("\n");
}

export function jsonToDjangoModels(obj) {
let djangoCode = "";
himalaya-kaushik marked this conversation as resolved.
Show resolved Hide resolved

djangoCode += "from django.db import models\n";
djangoCode += "from django.db.models import DO_NOTHING, CASCADE, PROTECT, SET_NULL, SET_DEFAULT\n\n";

obj.types.forEach(type => {
let typeStatements = [];

type.fields.forEach(field => {
if (field.type === "ENUM" || field.type === "SET") {
typeStatements.push(` ${field.name}_t = models.TextChoices(\n${field.values.map(value => ` ('${value}', '${value}')`).join(',\n')}\n )`);
}
});

djangoCode += `class ${type.name}(models.Model) {\n`;
type.fields.forEach(field => {
const djangoType = SQL_TO_DJANGO_TYPE_MAPPING[field.type] || 'CharField';
djangoCode += ` ${field.name} = models.${djangoType}(`;
if (field.primary) {
djangoCode += "primary_key=True, ";
}
if (field.unique) {
djangoCode += "unique=True, ";
}
if (!field.notNull) {
djangoCode += "null=True, ";
}
if (field.default !== "") {
djangoCode += `default=${parseDefault(field)}, `;
}
djangoCode += ")\n";
});
djangoCode += `}\n\n`;
});

obj.tables.forEach(table => {
djangoCode += `class ${table.name}(models.Model) {\n`;
table.fields.forEach(field => {
const djangoType = SQL_TO_DJANGO_TYPE_MAPPING[field.type] || 'CharField'; // Convert SQL type to Django type
djangoCode += ` ${field.name} = models.${djangoType}(`;
if (field.primary) {
djangoCode += "primary_key=True, ";
}
if (field.unique) {
djangoCode += "unique=True, ";
}
if (!field.notNull) {
djangoCode += "null=True, ";
}
if (field.default !== "") {
djangoCode += `default=${parseDefault(field)}, `;
}
djangoCode += ")\n";
});
djangoCode += `}\n\n`;
});

obj.references.forEach(reference => {
const startTable = obj.tables[reference.startTableId];
const endTable = obj.tables[reference.endTableId];
const deleteConstraint = SQL_TO_DJANGO_DELETE_CONSTRAINT_MAPPING[reference.deleteConstraint.toUpperCase()] || 'CASCADE';
const updateConstraint = SQL_TO_DJANGO_UPDATE_CONSTRAINT_MAPPING[reference.updateConstraint.toUpperCase()] || 'CASCADE';
djangoCode += `class ${startTable.name}(models.Model) {\n`;
djangoCode += ` ${endTable.name} = models.ForeignKey('${endTable.name}', on_delete=models.${deleteConstraint}, related_name='${startTable.name.toLowerCase()}_${endTable.name.toLowerCase()}', db_column='${endTable.name.toLowerCase()}', on_update=models.${updateConstraint})\n`;
djangoCode += `\n`;
});

return djangoCode;
}

export function jsonToMariaDB(obj) {
return `${obj.tables
.map(
Expand Down