adpa-enterprise-framework-automation
Version:
Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe
134 lines • 4.98 kB
JavaScript
/**
* Simple Database Adapter
*
* Provides a simple file-based database for template storage
* that can be shared between CLI and API processes.
*/
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { join } from 'path';
import { TEMPLATES_FILENAME } from '../constants.js';
export class SimpleDatabaseAdapter {
config;
constructor(config) {
this.config = {
dataDir: config?.dataDir || join(process.cwd(), 'data'),
templatesFile: config?.templatesFile || TEMPLATES_FILENAME
};
// Ensure data directory exists
if (!existsSync(this.config.dataDir)) {
mkdirSync(this.config.dataDir, { recursive: true });
}
}
getTemplatesPath() {
return join(this.config.dataDir, this.config.templatesFile);
}
loadTemplates() {
const templatesPath = this.getTemplatesPath();
if (!existsSync(templatesPath)) {
return [];
}
try {
const data = readFileSync(templatesPath, 'utf8');
return JSON.parse(data);
}
catch (error) {
console.warn('Error loading templates, starting with empty array:', error);
return [];
}
}
saveTemplates(templates) {
const templatesPath = this.getTemplatesPath();
writeFileSync(templatesPath, JSON.stringify(templates, null, 2), 'utf8');
}
async query(sql, params) {
const templates = this.loadTemplates();
// Simple SQL parsing for basic operations
const lowerSql = sql.toLowerCase().trim();
if (lowerSql.startsWith('select')) {
return this.handleSelect(sql, params, templates);
}
else if (lowerSql.startsWith('insert')) {
return this.handleInsert(sql, params, templates);
}
else if (lowerSql.startsWith('update')) {
return this.handleUpdate(sql, params, templates);
}
else if (lowerSql.startsWith('delete')) {
return this.handleDelete(sql, params, templates);
}
throw new Error(`Unsupported SQL operation: ${sql}`);
}
async transaction(callback) {
// Simple transaction simulation - for production use a real database
return callback(this);
}
handleSelect(sql, params = [], templates) {
// For now, return all active templates
// In a real implementation, parse the SQL properly
if (sql.includes('WHERE is_active = true')) {
return templates.filter(t => t.is_active === true);
}
if (sql.includes('WHERE id = $1')) {
const id = params[0];
return templates.filter(t => t.id === id);
}
if (sql.includes('WHERE generation_function = $1')) {
const func = params[0];
return templates.filter(t => t.generation_function === func);
}
// Default: return all templates
return templates;
}
handleInsert(sql, params = [], templates) {
// Create new template record
const newTemplate = {
id: this.generateId(),
name: params[0],
description: params[1],
category: params[2],
template_type: params[3],
ai_instructions: params[4],
prompt_template: params[5],
generation_function: params[6],
metadata: JSON.parse(params[7]),
version: params[8] || 1,
is_active: params[9] !== false,
is_system: params[10] || false,
created_by: params[11] || 'system',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
};
templates.push(newTemplate);
this.saveTemplates(templates);
return [newTemplate]; // Return as array to match database interface
}
handleUpdate(sql, params = [], templates) {
// Simple update implementation
const id = params[0];
const templateIndex = templates.findIndex(t => t.id === id);
if (templateIndex >= 0) {
templates[templateIndex].updated_at = new Date().toISOString();
// Update other fields based on SQL
this.saveTemplates(templates);
return [templates[templateIndex]];
}
return [];
}
handleDelete(sql, params = [], templates) {
// Soft delete - mark as inactive
const id = params[0];
const template = templates.find(t => t.id === id);
if (template) {
template.is_active = false;
template.updated_at = new Date().toISOString();
this.saveTemplates(templates);
}
return { changes: 1 };
}
generateId() {
return 'tpl_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
}
}
// Export singleton instance
export const database = new SimpleDatabaseAdapter();
//# sourceMappingURL=database.js.map