Skip to content

Commit 69e228f

Browse files
committed
feat: Add local database support for all backend frameworks
- Add SQLite database support to Flask backend creation - Add database models, CRUD operations, and SQLAlchemy for Flask - Add SQLite database support to Django with Item model - Add SQLAlchemy, Pydantic models, and database layer to FastAPI - Add better-sqlite3 database support to Node.js with Item model - Update backend creation to include database initialization - Run Django migrations automatically during setup - All frameworks now create local SQLite databases by default
1 parent 1f61a0c commit 69e228f

File tree

2 files changed

+684
-32
lines changed

2 files changed

+684
-32
lines changed

src/ipc/handlers/app_handlers.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,67 @@ async function ensureBackendDirectory(backendPath: string): Promise<void> {
235235
// Check if backend directory is empty or missing key files
236236
const backendFiles = fs.readdirSync(backendPath);
237237
if (backendFiles.length === 0) {
238-
// Create a basic Python Flask backend structure
238+
// Create a basic Python Flask backend structure with database support
239239
const requirementsTxt = `flask==2.3.3
240240
flask-cors==4.0.0
241+
flask-sqlalchemy==3.0.5
241242
python-dotenv==1.0.0
242243
`;
243244

244-
const appPy = `from flask import Flask, jsonify
245+
const appPy = `from flask import Flask, jsonify, request
245246
from flask_cors import CORS
247+
from flask_sqlalchemy import SQLAlchemy
246248
import os
247249
248250
app = Flask(__name__)
249251
CORS(app)
250252
253+
# Database configuration
254+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
255+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
256+
db = SQLAlchemy(app)
257+
258+
# Example model
259+
class Item(db.Model):
260+
id = db.Column(db.Integer, primary_key=True)
261+
name = db.Column(db.String(100), nullable=False)
262+
created_at = db.Column(db.DateTime, default=db.func.now())
263+
264+
def to_dict(self):
265+
return {
266+
'id': self.id,
267+
'name': self.name,
268+
'created_at': self.created_at.isoformat() if self.created_at else None
269+
}
270+
271+
# Create database tables
272+
with app.app_context():
273+
db.create_all()
274+
251275
@app.route('/')
252276
def hello():
253-
return jsonify({"message": "Backend API is running!"})
277+
return jsonify({"message": "Backend API with Database is running!"})
254278
255279
@app.route('/api/health')
256280
def health():
257281
return jsonify({"status": "healthy"})
258282
283+
@app.route('/api/items', methods=['GET'])
284+
def get_items():
285+
items = Item.query.all()
286+
return jsonify([item.to_dict() for item in items])
287+
288+
@app.route('/api/items', methods=['POST'])
289+
def create_item():
290+
data = request.get_json()
291+
if not data or 'name' not in data:
292+
return jsonify({"error": "Name is required"}), 400
293+
294+
new_item = Item(name=data['name'])
295+
db.session.add(new_item)
296+
db.session.commit()
297+
return jsonify(new_item.to_dict()), 201
298+
259299
if __name__ == '__main__':
260300
port = int(os.environ.get('PORT', 5000))
261301
app.run(debug=True, host='0.0.0.0', port=port)
@@ -275,7 +315,7 @@ python app.py
275315
// Make start.sh executable
276316
await fsPromises.chmod(path.join(backendPath, 'start.sh'), 0o755);
277317

278-
logger.info(`Created basic Flask backend structure in ${backendPath}`);
318+
logger.info(`Created basic Flask backend structure with database in ${backendPath}`);
279319
} catch (error) {
280320
logger.error(`Failed to create backend structure in ${backendPath}:`, error);
281321
throw error;

0 commit comments

Comments
 (0)