UNPKG

express-hale

Version:

🚀 Interactive Express.js scaffold CLI with comprehensive error handling, TypeScript/JavaScript, database integrations, Git Flow, and development tools

178 lines (145 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseTemplates = void 0; const base_renderer_1 = require("./base-renderer"); class DatabaseTemplates extends base_renderer_1.BaseRenderer { renderDatabaseConfig(database, language) { if (database === 'mysql') { return language === 'typescript' ? `import mysql from 'mysql2/promise'; import dotenv from 'dotenv'; dotenv.config(); const dbConfig = { host: process.env.MYSQL_HOST || 'localhost', port: Number(process.env.MYSQL_PORT) || 3306, user: process.env.MYSQL_USER || 'root', password: process.env.MYSQL_PASSWORD || '', database: process.env.MYSQL_DATABASE || 'test' }; export const pool = mysql.createPool(dbConfig); export const connectToMySQL = async (): Promise<void> => { try { const connection = await pool.getConnection(); console.log('✅ Connected to MySQL database'); connection.release(); } catch (error) { console.error('❌ MySQL connection failed:', error); console.warn('⚠️ Application will continue without MySQL connection'); } }; // Initialize connection connectToMySQL();` : `const mysql = require('mysql2/promise'); const dotenv = require('dotenv'); dotenv.config(); const dbConfig = { host: process.env.MYSQL_HOST || 'localhost', port: Number(process.env.MYSQL_PORT) || 3306, user: process.env.MYSQL_USER || 'root', password: process.env.MYSQL_PASSWORD || '', database: process.env.MYSQL_DATABASE || 'test' }; const pool = mysql.createPool(dbConfig); const connectToMySQL = async () => { try { const connection = await pool.getConnection(); console.log('✅ Connected to MySQL database'); connection.release(); } catch (error) { console.error('❌ MySQL connection failed:', error); console.warn('⚠️ Application will continue without MySQL connection'); } }; // Initialize connection connectToMySQL(); module.exports = { pool, connectToMySQL };`; } if (database === 'mongodb') { return language === 'typescript' ? `import mongoose from 'mongoose'; import dotenv from 'dotenv'; dotenv.config(); const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/test'; export const connectToMongoDB = async (): Promise<void> => { try { await mongoose.connect(MONGODB_URI); console.log('✅ Connected to MongoDB database'); } catch (error) { console.error('❌ MongoDB connection failed:', error); process.exit(1); } }; // Initialize connection connectToMongoDB(); export { mongoose };` : `const mongoose = require('mongoose'); const dotenv = require('dotenv'); dotenv.config(); const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/test'; const connectToMongoDB = async () => { try { await mongoose.connect(MONGODB_URI); console.log('✅ Connected to MongoDB database'); } catch (error) { console.error('❌ MongoDB connection failed:', error); process.exit(1); } }; // Initialize connection connectToMongoDB(); module.exports = { mongoose, connectToMongoDB };`; } if (database === 'redis') { return language === 'typescript' ? `import { createClient, RedisClientType } from 'redis'; import dotenv from 'dotenv'; dotenv.config(); const client: RedisClientType = createClient({ socket: { host: process.env.REDIS_HOST || 'localhost', port: Number(process.env.REDIS_PORT) || 6379, }, password: process.env.REDIS_PASSWORD || undefined }); export const connectToRedis = async (): Promise<void> => { try { await client.connect(); console.log('✅ Connected to Redis database'); } catch (error) { console.error('❌ Redis connection failed:', error); console.warn('⚠️ Application will continue without Redis connection'); } }; client.on('error', (err) => console.error('Redis Client Error:', err)); // Initialize connection connectToRedis(); export { client };` : `const { createClient } = require('redis'); const dotenv = require('dotenv'); dotenv.config(); const client = createClient({ socket: { host: process.env.REDIS_HOST || 'localhost', port: Number(process.env.REDIS_PORT) || 6379, }, password: process.env.REDIS_PASSWORD || undefined }); const connectToRedis = async () => { try { await client.connect(); console.log('✅ Connected to Redis database'); } catch (error) { console.error('❌ Redis connection failed:', error); console.warn('⚠️ Application will continue without Redis connection'); } }; client.on('error', (err) => console.error('Redis Client Error:', err)); // Initialize connection connectToRedis(); module.exports = { client, connectToRedis };`; } return ''; } } exports.DatabaseTemplates = DatabaseTemplates; //# sourceMappingURL=database-templates.js.map