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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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({
}));
},
},
{
DjangoModels: () => {
himalaya-kaushik marked this conversation as resolved.
Show resolved Hide resolved
setModal(MODAL.CODE);
const src = jsonToDjangoModels({
tables: tables,
references: relationships,
types: types,
});
setExportData((prev) => ({
...prev,
data: src,
extension: "sql",
himalaya-kaushik marked this conversation as resolved.
Show resolved Hide resolved
}));
},
},
],
function: () => {},
},
Expand Down
65 changes: 65 additions & 0 deletions src/utils/toSQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,71 @@ export function jsonToSQLite(obj) {
.join("\n");
}

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

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 )`);
}
});

// Generate Django model definition
djangoCode += `class ${type.name}(models.Model) {\n`;
type.fields.forEach(field => {
djangoCode += ` ${field.name} = models.${getTypeString(field)}(`;
himalaya-kaushik marked this conversation as resolved.
Show resolved Hide resolved
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 => {
djangoCode += ` ${field.name} = models.${getTypeString(field)}(`;
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];
djangoCode += `class ${startTable.name}(models.Model) {\n`;
djangoCode += ` ${endTable.name} = models.ForeignKey('${endTable.name}', on_delete=models.${reference.deleteConstraint}, related_name='${startTable.name.toLowerCase()}_${endTable.name.toLowerCase()}', db_column='${endTable.name.toLowerCase()}')\n`;
himalaya-kaushik marked this conversation as resolved.
Show resolved Hide resolved
djangoCode += `\n`;
});

return djangoCode;
}

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