16 releases
0.2.5 | Jan 25, 2023 |
---|---|
0.2.4 | Jan 25, 2023 |
0.1.9 | Jan 23, 2023 |
#756 in Database interfaces
172 downloads per month
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
- In the src directory add a folder with name db. It has to be in the src directory and with the name 'db'
- 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.
- 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
- 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.
- 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
~31β43MB
~865K SLoC