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.
- CRUD Operations: Create, Read, Update, and Delete data with ease.
- Dynamic Table Management: Create and delete tables programmatically.
- Parameterized Queries: Secure query execution with parameterized inputs to prevent SQL injection.
- Admin Routes: Special routes for database schema management.
- Endpoint:
/db/insert-data
- Method:
POST
- Description: Inserts data into the specified table.
- Request Body:
{ "tableName": "<table-name>", "data": { "column1": "value1", "column2": "value2", "...": "..." } }
- Response:
{ "isSuccess": true, "message": "Data inserted into table '<table-name>' successfully." }
- Endpoint:
/db/fetch-data
- Method:
GET
- Description: Fetches data from the specified table with optional conditions.
- Request Body:
{ "tableName": "<table-name>", "conditions": { "column1": "value1", "column2": "value2" } }
- Response:
{ "isSuccess": true, "data": [ { "column1": "value1", "column2": "value2", "...": "..." } ] }
- Endpoint:
/db/admin/create-table
- Method:
POST
- Description: Creates a new table with specified columns and constraints.
- Request Body:
{ "tableName": "<table-name>", "tableColumns": [ { "name": "<column-name>", "type": "<column-type>", "constraints": "<optional-constraints>" } ] }
- Response:
{ "isSuccess": true, "message": "Table '<table-name>' created successfully." }
- Endpoint:
/db/admin/delete-table
- Method:
DELETE
- Description: Deletes a table from the database.
- Request Body:
{ "tableName": "<table-name>" }
- Response:
{ "isSuccess": true, "message": "Table '<table-name>' deleted successfully." }
- Ensures only authorized admins can access certain routes.
├── routes
│ ├── crud.routes.ts # Public database routes
│ ├── admin.routes.ts # Admin-specific routes for schema management
├── controllers
│ ├── createTableController.ts
│ ├── deleteTableController.ts
│ ├── fetchDataController.ts
│ ├── insertDataController.ts
├── config
│ ├── db.ts # Database connection configuration
express
: For route handling.mysql2
or compatible database client: For database connection and query execution.dotenv
: For environment variable management.
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"tableName": "users",
"data": {
"id": "1",
"name": "John Doe",
"email": "[email protected]"
}
}' \
http://localhost:3000/db/insert-data
curl -X GET \
-H "Content-Type: application/json" \
-d '{
"tableName": "users",
"conditions": {
"email": "[email protected]"
}
}' \
http://localhost:3000/db/fetch-data
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"tableName": "users",
"tableColumns": [
{ "name": "id", "type": "VARCHAR(36)", "constraints": "PRIMARY KEY" },
{ "name": "name", "type": "VARCHAR(255)" },
{ "name": "email", "type": "VARCHAR(255)", "constraints": "UNIQUE" }
]
}' \
http://localhost:3000/db/admin/create-table
Ensure the following environment variables are set:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=yourpassword
DB_NAME=flexibase
This module is part of the Flexibase project and is licensed under MIT.