Skip to content

Commit b1d075f

Browse files
committedJan 1, 2025·
added docs for flexibase module
Signed-off-by: Tanmay Vaij <tanmayvaij22@gmail.com>
1 parent 73aff21 commit b1d075f

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
 

‎readme.md

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Database Module Documentation
2+
3+
The Database module in **Flexibase** provides a seamless interface to perform database operations such as creating and deleting tables, inserting and fetching data, and administrative tasks. This module supports SQL-based databases and is designed for flexibility and efficiency.
4+
5+
## Features
6+
7+
- **CRUD Operations**: Create, Read, Update, and Delete data with ease.
8+
- **Dynamic Table Management**: Create and delete tables programmatically.
9+
- **Parameterized Queries**: Secure query execution with parameterized inputs to prevent SQL injection.
10+
- **Admin Routes**: Special routes for database schema management.
11+
12+
---
13+
14+
## API Endpoints
15+
16+
### Public Routes
17+
18+
#### Insert Data
19+
20+
- **Endpoint**: `/db/insert-data`
21+
- **Method**: `POST`
22+
- **Description**: Inserts data into the specified table.
23+
- **Request Body**:
24+
```json
25+
{
26+
"tableName": "<table-name>",
27+
"data": {
28+
"column1": "value1",
29+
"column2": "value2",
30+
"...": "..."
31+
}
32+
}
33+
```
34+
- **Response**:
35+
```json
36+
{
37+
"isSuccess": true,
38+
"message": "Data inserted into table '<table-name>' successfully."
39+
}
40+
```
41+
42+
#### Fetch Data
43+
44+
- **Endpoint**: `/db/fetch-data`
45+
- **Method**: `GET`
46+
- **Description**: Fetches data from the specified table with optional conditions.
47+
- **Request Body**:
48+
```json
49+
{
50+
"tableName": "<table-name>",
51+
"conditions": {
52+
"column1": "value1",
53+
"column2": "value2"
54+
}
55+
}
56+
```
57+
- **Response**:
58+
```json
59+
{
60+
"isSuccess": true,
61+
"data": [
62+
{
63+
"column1": "value1",
64+
"column2": "value2",
65+
"...": "..."
66+
}
67+
]
68+
}
69+
```
70+
71+
### Admin Routes
72+
73+
#### Create Table
74+
75+
- **Endpoint**: `/db/admin/create-table`
76+
- **Method**: `POST`
77+
- **Description**: Creates a new table with specified columns and constraints.
78+
- **Request Body**:
79+
```json
80+
{
81+
"tableName": "<table-name>",
82+
"tableColumns": [
83+
{ "name": "<column-name>", "type": "<column-type>", "constraints": "<optional-constraints>" }
84+
]
85+
}
86+
```
87+
- **Response**:
88+
```json
89+
{
90+
"isSuccess": true,
91+
"message": "Table '<table-name>' created successfully."
92+
}
93+
```
94+
95+
#### Delete Table
96+
97+
- **Endpoint**: `/db/admin/delete-table`
98+
- **Method**: `DELETE`
99+
- **Description**: Deletes a table from the database.
100+
- **Request Body**:
101+
```json
102+
{
103+
"tableName": "<table-name>"
104+
}
105+
```
106+
- **Response**:
107+
```json
108+
{
109+
"isSuccess": true,
110+
"message": "Table '<table-name>' deleted successfully."
111+
}
112+
```
113+
114+
---
115+
116+
## Middleware
117+
118+
### `adminAuthenticator`
119+
- Ensures only authorized admins can access certain routes.
120+
121+
---
122+
123+
## Implementation Details
124+
125+
### Folder Structure
126+
```
127+
├── routes
128+
│ ├── crud.routes.ts # Public database routes
129+
│ ├── admin.routes.ts # Admin-specific routes for schema management
130+
├── controllers
131+
│ ├── createTableController.ts
132+
│ ├── deleteTableController.ts
133+
│ ├── fetchDataController.ts
134+
│ ├── insertDataController.ts
135+
├── config
136+
│ ├── db.ts # Database connection configuration
137+
```
138+
139+
### Dependencies
140+
141+
- `express`: For route handling.
142+
- `mysql2` or compatible database client: For database connection and query execution.
143+
- `dotenv`: For environment variable management.
144+
145+
---
146+
147+
## Example Usage
148+
149+
### Insert Data
150+
```bash
151+
curl -X POST \
152+
-H "Content-Type: application/json" \
153+
-d '{
154+
"tableName": "users",
155+
"data": {
156+
"id": "1",
157+
"name": "John Doe",
158+
"email": "john.doe@example.com"
159+
}
160+
}' \
161+
http://localhost:3000/db/insert-data
162+
```
163+
164+
### Fetch Data
165+
```bash
166+
curl -X GET \
167+
-H "Content-Type: application/json" \
168+
-d '{
169+
"tableName": "users",
170+
"conditions": {
171+
"email": "john.doe@example.com"
172+
}
173+
}' \
174+
http://localhost:3000/db/fetch-data
175+
```
176+
177+
### Create Table
178+
```bash
179+
curl -X POST \
180+
-H "Content-Type: application/json" \
181+
-d '{
182+
"tableName": "users",
183+
"tableColumns": [
184+
{ "name": "id", "type": "VARCHAR(36)", "constraints": "PRIMARY KEY" },
185+
{ "name": "name", "type": "VARCHAR(255)" },
186+
{ "name": "email", "type": "VARCHAR(255)", "constraints": "UNIQUE" }
187+
]
188+
}' \
189+
http://localhost:3000/db/admin/create-table
190+
```
191+
192+
---
193+
194+
## Environment Variables
195+
196+
Ensure the following environment variables are set:
197+
198+
```env
199+
DB_HOST=localhost
200+
DB_USER=root
201+
DB_PASSWORD=yourpassword
202+
DB_NAME=flexibase
203+
```
204+
205+
---
206+
207+
## License
208+
209+
This module is part of the **Flexibase** project and is licensed under [MIT](LICENSE).

0 commit comments

Comments
 (0)
Please sign in to comment.