Skip to content

Commit 37cb9a9

Browse files
author
Lasim
committed
Refactor database schema and plugin system for improved flexibility and type safety
- Updated schema definitions in `schema.ts` to use column builder functions for better compatibility with different database dialects. - Modified `PluginManager` to utilize a unified `AnyDatabase` type, allowing for both SQLite and PostgreSQL databases. - Enhanced plugin initialization to handle cases where the database may not be set, allowing plugins to manage their own database interactions. - Introduced new database configuration management in `config.ts`, enabling dynamic setup for SQLite and PostgreSQL. - Created new routes for database setup and status, utilizing Zod for request validation and response schemas. - Added migration SQL files for example plugin tables to ensure proper database structure. - Implemented type guards and casting in example plugin to handle database-specific operations. - Updated Fastify instance to include new database and plugin manager decorations for easier access in routes.
1 parent 693df3c commit 37cb9a9

File tree

21 files changed

+1383
-305
lines changed

21 files changed

+1383
-305
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ fastly-events.log
5353
.turbo
5454
.cache
5555

56-
deploystack.db
56+
deploystack.db
57+
services/backend/persistent_data/*

package-lock.json

Lines changed: 237 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/backend/DB.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,55 @@
44

55
DeployStack uses SQLite with Drizzle ORM for database operations. This combination provides excellent performance, type safety, and a modern, developer-friendly experience without the need for external database dependencies.
66

7+
## Database Setup and Configuration
8+
9+
The backend server provides API endpoints for managing the initial database setup and checking its status.
10+
11+
### Database Status
12+
13+
You can check the current status of the database (whether it's configured and initialized) using the following endpoint:
14+
15+
- **Endpoint:** `GET /api/db/status`
16+
- **Method:** `GET`
17+
- **Response:** A JSON object indicating the database `configured` status (boolean), `initialized` status (boolean), and current `dialect` (e.g., "sqlite" or "postgres", or null if not configured).
18+
19+
### Initial Database Setup
20+
21+
To perform the initial setup of the database, use the following endpoint:
22+
23+
- **Endpoint:** `POST /api/db/setup`
24+
- **Method:** `POST`
25+
- **Request Body:** A JSON object specifying the database type and configuration.
26+
27+
**For SQLite:**
28+
The server will automatically manage the database file location. The request body should be:
29+
```json
30+
{
31+
"type": "sqlite"
32+
}
33+
```
34+
The SQLite database file will be created and stored at: `services/backend/persistent_data/database/deploystack.db`.
35+
36+
**For PostgreSQL:**
37+
The request body should be:
38+
```json
39+
{
40+
"type": "postgres",
41+
"connectionString": "postgresql://username:password@host:port/mydatabase"
42+
}
43+
```
44+
Replace the `connectionString` with your actual PostgreSQL connection URI.
45+
46+
**Important:** After the initial database setup via this API, you **must restart the backend server** for the changes to take full effect and for the application to connect to the newly configured database.
47+
48+
### Database Configuration File
49+
50+
The choice of database (SQLite or PostgreSQL) and its specific configuration (like the connection string for PostgreSQL) is stored in a JSON file located at:
51+
52+
- `services/backend/persistent_data/db.selection.json`
53+
54+
This file is automatically managed by the setup API. You typically do not need to edit it manually.
55+
756
## Key Components
857

958
- **SQLite**: Embedded SQL database engine
@@ -117,13 +166,14 @@ You can inspect the SQLite database directly using various tools:
117166
- **SQLite CLI**:
118167
119168
```bash
120-
sqlite3 ./data/deploystack.db
169+
sqlite3 services/backend/persistent_data/database/deploystack.db
121170
```
171+
(Assuming the command is run from the project root directory)
122172
123173
- **Visual Tools**: [DB Browser for SQLite](https://sqlitebrowser.org/) or VSCode extensions like SQLite Viewer
124174
125175
## Troubleshooting
126176
127177
- If you get a "table already exists" error, check if you've already applied the migration
128178
- For complex schema changes, you may need to create multiple migrations
129-
- To reset the database, delete the `./data/deploystack.db` file and restart the server
179+
- To reset the database, delete the `services/backend/persistent_data/database/deploystack.db` file and restart the server

services/backend/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ services/backend/
7979
└── tsconfig.json # TypeScript configuration
8080
```
8181

82+
## 💾 Persistent Data
83+
84+
The `services/backend/persistent_data/` directory is designated for storing all data that needs to persist across application restarts or deployments.
85+
86+
**Purpose:**
87+
- To provide a single, consistent location for all persistent backend data.
88+
- When developing backend features that require data persistence (e.g., database files, configuration files that should not be in version control but are generated/modified at runtime), use this directory exclusively.
89+
90+
**Examples of data stored here:**
91+
- SQLite database file (e.g., `persistent_data/database/deploystack.db`)
92+
- Database selection configuration (e.g., `persistent_data/db.selection.json`)
93+
94+
This ensures that persistent data is managed in a predictable way and is not scattered across the project.
95+
8296
## 🌍 Environment Variables
8397

8498
Create a `.env` file in the `services/backend` directory with the following variables:

0 commit comments

Comments
 (0)