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

Exporting to SQLite #44

Open
grigandal625 opened this issue Jan 9, 2021 · 0 comments
Open

Exporting to SQLite #44

grigandal625 opened this issue Jan 9, 2021 · 0 comments

Comments

@grigandal625
Copy link

Would you like to add export functionality to SQLite?

Here's a sketched example of a generator, made based on your own diagram structure

function toSQLite(diagram) {
    return `${Object.keys(diagram)
        .map(
            (tableKey) => `
CREATE TABLE ${diagram[tableKey].table_name} (
    ${Object.keys(diagram[tableKey].coloumns)
        .map(
            (columnKey) =>
                `${diagram[tableKey].coloumns[columnKey].coloumn_name} ${
                    diagram[tableKey].coloumns[columnKey].dataType
                } ${diagram[tableKey].coloumns[columnKey].primaryKey ? "PRIMARY KEY" : ""}${
                    diagram[tableKey].coloumns[columnKey].primaryKey &&
                    diagram[tableKey].coloumns[columnKey].autoIncrement
                        ? " AUTOINCREMENT"
                        : ""
                }${diagram[tableKey].coloumns[columnKey].notNull ? " NOT NULL" : ""}${
                    diagram[tableKey].coloumns[columnKey].unique ? " UNIQUE" : ""
                }`
        )
        .concat(
            Object.keys(diagram[tableKey].coloumns)
                .filter((columnKey) => diagram[tableKey].coloumns[columnKey].foreignKey)
                .map((columnKey, index) => {
                    var association =
                        diagram[tableKey].association[diagram[tableKey].coloumns[columnKey].association_belong_id];
                    return `CONSTRAINT fk_${diagram[tableKey].table_name}_${diagram[association.table_id].table_name}_${
                        index + 1
                    } FOREIGN KEY (${diagram[tableKey].coloumns[columnKey].coloumn_name}) REFERENCES ${
                        diagram[association.table_id].table_name
                    } (${diagram[association.table_id].coloumns[association.targetKey].coloumn_name})`;
                })
        )
        .join(",\n    ")}
);`
        )
        .join("\n\n")}`;
}

An example of generating code from your diagram structure:

var diagram = {
    "3559740f-4fc7-4a58-8088-a6813e22696b": {
        table_name: "ies_group",
        widthTable: 213,
        heightTable: 0,
        point: { x: 356, y: 95 },
        coloumns: {
            coloumn_car_id_885ddad7_c509_4d5e_ab2e_dc5cb06d0e35: {
                coloumn_name: "id_ies_group",
                comment: "",
                dataType: "INTEGER",
                default: "",
                primaryKey: true,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: true,
                style: { shadowBlur: 0, shadowColor: "#00D2FF" },
                association_belong_id: null,
                association_has_id: ["assoc_Source_bca2a6ca-cddd-4765-b8cb-617c6a1bbb6f"],
            },
            "614a69f3-56bd-483e-bf86-bfc5da481587": {
                coloumn_name: "ies_group_name",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
        },
        association: {
            "assoc_Source_bca2a6ca-cddd-4765-b8cb-617c6a1bbb6f": {
                connector_id: "conn_car_number_driver_idb26b82c8-d673-4590-90c5-89b19fb4ab5c",
                type: "has",
                table: "c0661ebf-848c-4cb9-9631-5fd79f1ed8b1",
                table_id: "c0661ebf-848c-4cb9-9631-5fd79f1ed8b1",
                foreignKey: "0cb79ff1-382b-4fea-bb9b-c41eb3f59e34",
                foreignKey_id: "0cb79ff1-382b-4fea-bb9b-c41eb3f59e34",
                sourceKey: "id",
                sourceKey_id: "coloumn_car_id_885ddad7_c509_4d5e_ab2e_dc5cb06d0e35",
                point: { x: 0, y: 40 },
            },
        },
    },
    "c0661ebf-848c-4cb9-9631-5fd79f1ed8b1": {
        table_name: "ies_user",
        widthTable: 199,
        heightTable: 0,
        point: { x: 26, y: 50 },
        coloumns: {
            coloumn_car_id_885ddad7_c509_4d5e_ab2e_dc5cb06d0e35: {
                coloumn_name: "id_ies_user",
                comment: "",
                dataType: "INTEGER",
                default: "",
                primaryKey: true,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: true,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
            "a7d702a1-a0a7-4872-9263-264f1251bf04": {
                coloumn_name: "surname",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
            "058ca8d5-5167-4040-81af-0f5c6a1d9934": {
                coloumn_name: "name",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
            "590d2d29-4ce7-495a-beb0-3d019a473253": {
                coloumn_name: "patronymic",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: false,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
            "0cb79ff1-382b-4fea-bb9b-c41eb3f59e34": {
                coloumn_name: "id_ies_group",
                comment: "",
                dataType: "INTEGER",
                default: "",
                primaryKey: false,
                notNull: false,
                unique: false,
                foreignKey: true,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "#00D2FF" },
                association_belong_id: "assoc_Foreign_a9cc4bb3-f08c-45d7-9652-725be4f61767",
                association_has_id: [],
            },
            "cc88f304-7d7b-49b8-9f84-d32ef3141d7c": {
                coloumn_name: "user_name",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: true,
                unique: true,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
            "44fac1ae-e045-4602-9741-a5ea07e42ef9": {
                coloumn_name: "password",
                comment: "",
                dataType: "TEXT",
                default: "",
                primaryKey: false,
                notNull: true,
                unique: false,
                foreignKey: false,
                unsigned: false,
                zeroFill: false,
                autoIncrement: false,
                style: { shadowBlur: 0, shadowColor: "green" },
                association_belong_id: null,
                association_has_id: [],
            },
        },
        association: {
            "assoc_Foreign_a9cc4bb3-f08c-45d7-9652-725be4f61767": {
                connector_id: "conn_car_number_driver_idb26b82c8-d673-4590-90c5-89b19fb4ab5c",
                type: "belong",
                table: "3559740f-4fc7-4a58-8088-a6813e22696b",
                table_id: "3559740f-4fc7-4a58-8088-a6813e22696b",
                foreignKey: "0cb79ff1-382b-4fea-bb9b-c41eb3f59e34",
                foreignKey_id: "0cb79ff1-382b-4fea-bb9b-c41eb3f59e34",
                targetKey: "coloumn_car_id_885ddad7_c509_4d5e_ab2e_dc5cb06d0e35",
                targetKey_id: "coloumn_car_id_885ddad7_c509_4d5e_ab2e_dc5cb06d0e35",
                point: { x: 0, y: 120 },
            },
        },
    },
};

console.log(toSQLite(diagram));
/******************************************************************
CREATE TABLE ies_group (
    id_ies_group INTEGER PRIMARY KEY AUTOINCREMENT,
    ies_group_name TEXT 
);


CREATE TABLE ies_user (
    id_ies_user INTEGER PRIMARY KEY AUTOINCREMENT,
    surname TEXT ,
    name TEXT ,
    patronymic TEXT ,
    id_ies_group INTEGER ,
    user_name TEXT  NOT NULL UNIQUE,
    password TEXT  NOT NULL,
    CONSTRAINT fk_ies_user_ies_group_1 FOREIGN KEY (id_ies_group) REFERENCES ies_group (id_ies_group)
);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant