|
1 | | -# Connecting to a Database |
| 1 | +# Database Integration |
| 2 | + |
| 3 | +The Database Integration allows you to connect your AI-generated applications to real databases, automatically generating schemas and enabling data-driven functionality. |
2 | 4 |
|
3 | 5 | ```python exec |
4 | 6 | import reflex as rx |
5 | | -``` |
6 | | - |
7 | | -Connecting to a database is critical to give your app access to real data. This section will cover how to connect to a database using the AI Builder. |
8 | | - |
9 | | -To connect to a database you will need a `DB_URI`. Reflex.build currently supports `postgresql` and `mysql` databases. |
10 | | - |
11 | | -This is what it looks like for a Postgres database: |
12 | | - |
13 | | -```bash |
14 | | -postgresql://username:password@hostname:port/database_name |
15 | | -``` |
16 | | - |
17 | | -```bash |
18 | | -postgresql://admin:secret123@db.mycompany.com:5432/mydatabase |
19 | | -``` |
20 | | - |
21 | | -```python eval |
22 | | -rx.box(height="1rem") |
| 7 | +from reflex_image_zoom import image_zoom |
23 | 8 | ``` |
24 | 9 |
|
25 | 10 | ```python eval |
26 | | -rx.accordion.root( |
27 | | - rx.accordion.item( |
28 | | - header="DB URI (More Details)", |
29 | | - content=rx.table.root( |
30 | | - rx.table.header( |
31 | | - rx.table.row( |
32 | | - rx.table.column_header_cell("Component"), |
33 | | - rx.table.column_header_cell("Example"), |
34 | | - rx.table.column_header_cell("Description"), |
35 | | - ), |
| 11 | +rx.el.div( |
| 12 | + image_zoom( |
| 13 | + rx.image( |
| 14 | + src=rx.color_mode_cond( |
| 15 | + "/ai_builder/integrations/database_light.png", |
| 16 | + "/ai_builder/integrations/database_dark.png", |
36 | 17 | ), |
37 | | - rx.table.body( |
38 | | - rx.table.row( |
39 | | - rx.table.row_header_cell(rx.code("postgresql://")), |
40 | | - rx.table.cell(rx.code("postgresql://")), |
41 | | - rx.table.cell("The database ", rx.el.span("dialect", font_weight="bold"), " or driver (can also be ", |
42 | | - rx.code("mysql://"), ", ", rx.code("sqlite:///"), ", etc.)"), |
43 | | - ), |
44 | | - rx.table.row( |
45 | | - rx.table.row_header_cell(rx.code("username")), |
46 | | - rx.table.cell(rx.code("admin")), |
47 | | - rx.table.cell("The ", rx.el.span("username", font_weight="bold"), " used to authenticate with the database"), |
48 | | - ), |
49 | | - rx.table.row( |
50 | | - rx.table.row_header_cell(rx.code("password")), |
51 | | - rx.table.cell(rx.code("secret123")), |
52 | | - rx.table.cell("The ", rx.el.span("password", font_weight="bold"), " for the database user (insecure to expose this in plain text)"), |
53 | | - ), |
54 | | - rx.table.row( |
55 | | - rx.table.row_header_cell(rx.code("hostname")), |
56 | | - rx.table.cell(rx.code("db.mycompany.com")), |
57 | | - rx.table.cell("The ", rx.el.span("host", font_weight="bold"), " where the database server is running"), |
58 | | - ), |
59 | | - rx.table.row( |
60 | | - rx.table.row_header_cell(rx.code("port")), |
61 | | - rx.table.cell(rx.code("5432")), |
62 | | - rx.table.cell("The ", rx.el.span("port", font_weight="bold"), " the database server is listening on (default for PostgreSQL)"), |
63 | | - ), |
64 | | - rx.table.row( |
65 | | - rx.table.row_header_cell(rx.code("database_name")), |
66 | | - rx.table.cell(rx.code("mydatabase")), |
67 | | - rx.table.cell("The ", rx.el.span("name", font_weight="bold"), " of the database to connect to"), |
68 | | - ), |
69 | | - ), |
70 | | - width="100%", |
| 18 | + class_name="p-2 rounded-md h-auto", |
| 19 | + border=f"0.81px solid {rx.color('slate', 5)}", |
71 | 20 | ), |
| 21 | + class_name="rounded-md overflow-hidden", |
72 | 22 | ), |
73 | | - variant="surface", |
| 23 | + class_name="w-full flex flex-col rounded-md cursor-pointer", |
74 | 24 | ) |
75 | 25 | ``` |
76 | 26 |
|
77 | | -```python eval |
78 | | -rx.box(height="1rem") |
79 | | -``` |
| 27 | +## Supported Databases |
80 | 28 |
|
81 | | -You can also use a MySQL database. The connection string looks like this: |
| 29 | +- **PostgreSQL** - Recommended for production applications |
| 30 | +- **MySQL** - Popular open-source database |
| 31 | +- **SQLite** - Lightweight database, perfect for development and small applications |
82 | 32 |
|
83 | | -```bash |
84 | | -mysql://username:password@hostname:port/database_name |
85 | | -``` |
| 33 | +## Getting Started |
86 | 34 |
|
| 35 | +### Opening the Database Integration |
87 | 36 |
|
| 37 | +1. Navigate to your app in the AI Builder |
| 38 | +2. Open the **Settings drawer** (gear icon) |
| 39 | +3. Click on the **Integrations** tab |
| 40 | +4. Find and enable the **Database** integration |
88 | 41 |
|
89 | | -## Connecting your Database before the app is generated |
| 42 | +### Connection Methods |
90 | 43 |
|
91 | | -You can add your `Database URI` at the start of your generation as shown below. |
| 44 | +The Database Integration offers two convenient ways to connect: |
92 | 45 |
|
93 | | -```python eval |
94 | | -rx.image( |
95 | | - src="/ai_builder/add_db_before_app.gif", |
96 | | - height="auto", |
97 | | - padding_bottom="2rem", |
98 | | -) |
99 | | -``` |
| 46 | +#### 1. Connection Details (Recommended) |
100 | 47 |
|
101 | | -Here if you wanted to build a dashboard for example we recommend a prompt as follows: |
| 48 | +This user-friendly form breaks down your database connection into individual fields: |
102 | 49 |
|
103 | | -`Build a dashboard around my database data` |
| 50 | +**For PostgreSQL and MySQL:** |
| 51 | +- **Database Type**: Select from dropdown (PostgreSQL/MySQL) |
| 52 | +- **Hostname**: Your database server address (e.g., `localhost`, `db.company.com`) |
| 53 | +- **Port**: Automatically filled (PostgreSQL: 5432, MySQL: 3306) or specify custom port |
| 54 | +- **Username**: Your database username |
| 55 | +- **Password**: Your database password (securely handled) |
| 56 | +- **Database Name**: The specific database to connect to |
104 | 57 |
|
| 58 | +**For SQLite:** |
| 59 | +- **Database Type**: Select "SQLite" from dropdown |
| 60 | +- **SQLite Download URL**: Either a local file path or HTTP URL to download the database file |
105 | 61 |
|
| 62 | +#### 2. Database URI |
106 | 63 |
|
107 | | -## Connecting your Database after the app is generated |
| 64 | +For advanced users who prefer the traditional connection string format: |
108 | 65 |
|
109 | | -You can add your `Database URI` after you've already generated an app or directly from a template that you have forked as shown below. |
| 66 | +**PostgreSQL:** |
| 67 | +``` |
| 68 | +postgresql://username:password@hostname:port/database_name |
| 69 | +``` |
110 | 70 |
|
111 | | -```python eval |
112 | | -rx.image( |
113 | | - src="/ai_builder/add_db_in_app.gif", |
114 | | - height="auto", |
115 | | - padding_bottom="2rem", |
116 | | -) |
| 71 | +**MySQL:** |
| 72 | +``` |
| 73 | +mysql://username:password@hostname:port/database_name |
117 | 74 | ``` |
118 | 75 |
|
119 | | -Here if you wanted to hook up your data correctly we recommend a prompt as follows: |
| 76 | +**SQLite:** |
| 77 | +``` |
| 78 | +sqlite:///path/to/database.sqlite |
| 79 | +sqlite+https://example.com/database.sqlite |
| 80 | +``` |
120 | 81 |
|
121 | | -`Use the database I added to rewrite the dashboard to display my expense reporting data, keep the existing layout charts and structure the same` |
| 82 | +## Database URI Components |
| 83 | + |
| 84 | +Protocol (postgresql://) - Database type identifier |
| 85 | +Username (admin) - Database user credentials |
| 86 | +Password (secret123) - User password (kept secure) |
| 87 | +Hostname (db.company.com) - Server address |
| 88 | +Port (5432) - Connection port |
| 89 | +Database (mydatabase) - Target database name |
| 90 | + |
| 91 | +## Connection Process |
| 92 | + |
| 93 | +1. **Choose your method**: Use either Connection Details form or Database URI |
| 94 | +2. **Fill in credentials**: Provide your database connection information |
| 95 | +3. **Click Connect**: The system will validate and test your connection |
| 96 | +4. **Schema Generation**: Upon successful connection, the system automatically: |
| 97 | + - Connects to your database |
| 98 | + - Analyzes the database structure |
| 99 | + - Generates SQLAlchemy models |
| 100 | + - Makes schema available to the AI for queries |
0 commit comments