#sql-database #mysql #relational #postgressql

sql_db_creator

Generate sql databases by only configure the data of the databases in json files

16 releases

0.2.5 Jan 25, 2023
0.2.4 Jan 25, 2023
0.1.9 Jan 23, 2023

#1780 in Database interfaces

MIT/Apache

30KB
576 lines

SQL DB Creator

Generate sql databases by only configure the data of the databases in json files

Check the examples after reading.

https://github.com/Sok-Bou/SQL-DB-Creator-Example

With this crate you can generate multiple mysql or postgressql databases and fill them (optional) with data.

Steps

  1. In the src directory add a folder with name db. It has to be in the src directory and with the name 'db'
  2. Add one or more folders depending on how many databases you want. Most of the time it is one database. The name of the folder would be the name of the database.
  3. Add one or more json files. The names of the json files would be the names of the database tables.

By now you folder structure should look like this

πŸ“‚ src

┣- πŸ“‚ db (1)

┃ ┣--- πŸ“‚ countries (2)

┃ ┃ ┣ --- πŸ“œ geography.json (3)

┃ ┃ β”— --- πŸ“œ government.json (4)

┃ β”—--- πŸ“‚ flowers (5)

┃ ┃ ┣ --- πŸ“œ infos.json (6)

┃ ┃ β”— --- πŸ“œ region.json (7)

┣ πŸ“œ main.rs (8)


(1) Required name 'db'

(2) First database with name 'countries'

(3) Table in database 'countries' with name 'geography'

(4) Table in database 'countries' with name 'government'

(5) Second database with name 'flowers'

(6) Table in database 'flowers' with name 'infos'

(7) Table in database 'flowers' with name 'region'

(8) The main file


  1. Now the json files should have a specific structure to generate the columns of the tables. The structure goes like this:

(mysql example. Database: 'countries', Table: 'geography')

{
    "schema": {
        "name": "VARCHAR(255)",
        "area": "INT(255)",
        "region": "VARCHAR(255)",
        "costline": "INT(255)",
        "borders": "INT(255)",
        "is_in_europe": "BIT"
    },
    "data": [
        {
            "name": "Germany",
            "area": 357021,
            "region": "Central Europe",
            "costline": 2389,
            "borders": 3714,
            "is_in_europe": true
        },
        {
            "name": "United States of America",
            "area": 9826675,
            "region": "North Americs",
            "costline": 19920,
            "borders": 12191,
            "is_in_europe": false
        }
    ]
}

(postgressql example. Database: 'countries', Table: 'geography')

{
    "schema": {
        "name": "VARCHAR(255)",
        "area": "INTEGER",
        "region": "VARCHAR(255)",
        "costline": "INTEGER",
        "borders": "INTEGER",
        "is_in_europe": "BOOLEAN"
    },
    "data": [
        {
            "name": "Germany",
            "area": 357021,
            "region": "Central Europe",
            "costline": 2389,
            "borders": 3714,
            "is_in_europe": true
        },
        {
            "name": "United States of America",
            "area": 9826675,
            "region": "North Americs",
            "costline": 19920,
            "borders": 12191,
            "is_in_europe": false
        }
    ]
}

In the schema part are the infos to create the schema of the table with column names and data types as they appear in the example above. In the data part are the infos to fill the table with our table data. The data section is optional. We can only generate the table. Of course in case we fill the table with data, we have to be careful with the values. The values should have the right type as given in the schema.


  1. To generate the database run the following code in your main

(mysql example)

use sql_db_creator::{ DBType, Config, setup };

fn main() {

    let config = Config {
        user: String::from("root"),
        password: String::from("password"),
        host: String::from("localhost")
    };

    setup(DBType::MySql, config);
}

(postgressql example)

use sql_db_creator::{ DBType, Config, setup };

fn main() {

    let config = Config {
        user: String::from("postgres"),
        password: String::from("admin"),
        host: String::from("localhost")
    };

    setup(DBType::PostgresSql, config);
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~33–49MB
~1M SLoC