From 1ae333312db4e3bbe081f4be3ab5473816651bdb Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Thu, 25 Dec 2025 18:40:54 +0000 Subject: [PATCH] feat: add initializeDBAL function for database client setup --- .../lib/dbal/database-dbal/get-dbal.server.ts | 15 ++ .../database-dbal/initialize-dbal.server.ts | 36 ++++ .../declarative-component-renderer/index.ts | 159 ++---------------- 3 files changed, 63 insertions(+), 147 deletions(-) create mode 100644 frontends/nextjs/src/lib/dbal/database-dbal/get-dbal.server.ts create mode 100644 frontends/nextjs/src/lib/dbal/database-dbal/initialize-dbal.server.ts diff --git a/frontends/nextjs/src/lib/dbal/database-dbal/get-dbal.server.ts b/frontends/nextjs/src/lib/dbal/database-dbal/get-dbal.server.ts new file mode 100644 index 000000000..f68af0c82 --- /dev/null +++ b/frontends/nextjs/src/lib/dbal/database-dbal/get-dbal.server.ts @@ -0,0 +1,15 @@ +import 'server-only' + +import type { DBALClient } from '@/lib/dbal-stub' +import { dbalState } from './dbal-state.server' +import { initializeDBAL } from './initialize-dbal.server' + +/** + * Get DBAL client instance (lazy initialization) + */ +export async function getDBAL(): Promise { + if (!dbalState.initialized) { + await initializeDBAL() + } + return dbalState.client +} diff --git a/frontends/nextjs/src/lib/dbal/database-dbal/initialize-dbal.server.ts b/frontends/nextjs/src/lib/dbal/database-dbal/initialize-dbal.server.ts new file mode 100644 index 000000000..f46034106 --- /dev/null +++ b/frontends/nextjs/src/lib/dbal/database-dbal/initialize-dbal.server.ts @@ -0,0 +1,36 @@ +import 'server-only' + +import { DBALClient } from '@/lib/dbal-stub' +import type { DBALConfig } from '@/lib/dbal-stub' +import { dbalState } from './dbal-state.server' + +/** + * Initialize DBAL client for database operations + */ +export async function initializeDBAL(): Promise { + if (dbalState.initialized) { + return + } + + try { + const config: DBALConfig = { + mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', + adapter: 'prisma', + database: { + url: process.env.DATABASE_URL || 'file:./prisma/dev.db', + }, + security: { + sandbox: 'permissive', + enableAuditLog: false, + }, + } + + dbalState.client = new DBALClient(config) + dbalState.initialized = true + console.log('DBAL client initialized successfully') + } catch (error) { + console.error('Failed to initialize DBAL client:', error) + dbalState.client = null + dbalState.initialized = true + } +} diff --git a/frontends/nextjs/src/lib/rendering/declarative-component-renderer/index.ts b/frontends/nextjs/src/lib/rendering/declarative-component-renderer/index.ts index abaa49508..5b73c89ca 100644 --- a/frontends/nextjs/src/lib/rendering/declarative-component-renderer/index.ts +++ b/frontends/nextjs/src/lib/rendering/declarative-component-renderer/index.ts @@ -1,149 +1,14 @@ -import type { ComponentInstance } from './builder-types' -import { LuaEngine } from './lua-engine' +export type { DeclarativeComponentConfig, MessageFormat, LuaScriptDefinition } from './types' -export interface DeclarativeComponentConfig { - type: string - category: string - label: string - description: string - icon: string - props: Array<{ - name: string - type: string - label: string - defaultValue?: any - required: boolean - }> - config: { - layout: string - styling: { - className: string - } - children: any[] - } -} +export { DeclarativeComponentRenderer } from './renderer' +export { getDeclarativeRenderer } from './get-declarative-renderer' +export { loadPackageComponents } from './load-package-components' -export interface MessageFormat { - id: string - username: string - userId: string - message: string - timestamp: number - type: 'message' | 'system' | 'join' | 'leave' -} - -export class DeclarativeComponentRenderer { - private luaEngine: LuaEngine - private componentConfigs: Record = {} - private luaScripts: Record = {} - - constructor() { - this.luaEngine = new LuaEngine() - } - - registerComponentConfig(componentType: string, config: DeclarativeComponentConfig) { - this.componentConfigs[componentType] = config - } - - registerLuaScript(scriptId: string, script: { code: string; parameters: any[]; returnType: string }) { - this.luaScripts[scriptId] = script - } - - async executeLuaScript(scriptId: string, params: any[]): Promise { - const script = this.luaScripts[scriptId] - if (!script) { - throw new Error(`Lua script not found: ${scriptId}`) - } - - const paramContext: Record = {} - script.parameters.forEach((param, index) => { - if (params[index] !== undefined) { - paramContext[param.name] = params[index] - } - }) - - const paramAssignments = script.parameters - .map(p => `local ${p.name} = context.data.params["${p.name}"]`) - .join('\n') - - const paramList = script.parameters.map(p => p.name).join(', ') - - const wrappedCode = ` -${paramAssignments} - -${script.code} - -local result_fn = sendMessage or handleCommand or formatTime or userJoin or userLeave or countThreads -if result_fn and type(result_fn) == "function" then - return result_fn(${paramList}) -end -` - - const result = await this.luaEngine.execute(wrappedCode, { - data: { params: paramContext } - }) - - if (!result.success) { - console.error(`Lua script error (${scriptId}):`, result.error, result.logs) - throw new Error(result.error || 'Lua script execution failed') - } - - return result.result - } - - getComponentConfig(componentType: string): DeclarativeComponentConfig | undefined { - return this.componentConfigs[componentType] - } - - hasComponentConfig(componentType: string): boolean { - return componentType in this.componentConfigs - } - - interpolateValue(template: string, context: Record): string { - if (!template || typeof template !== 'string') return template - - return template.replace(/\{([^}]+)\}/g, (match, key) => { - const value = context[key] - return value !== undefined ? String(value) : match - }) - } - - evaluateConditional(condition: string | boolean, context: Record): boolean { - if (typeof condition === 'boolean') return condition - if (!condition) return true - - const value = context[condition] - return Boolean(value) - } - - resolveDataSource(dataSource: string, context: Record): any[] { - if (!dataSource) return [] - return context[dataSource] || [] - } -} - -const globalRenderer = new DeclarativeComponentRenderer() - -export function getDeclarativeRenderer(): DeclarativeComponentRenderer { - return globalRenderer -} - -export function loadPackageComponents(packageContent: any) { - const renderer = getDeclarativeRenderer() - - if (packageContent.componentConfigs) { - Object.entries(packageContent.componentConfigs).forEach(([type, config]) => { - renderer.registerComponentConfig(type, config as DeclarativeComponentConfig) - }) - } - - if (packageContent.luaScripts) { - packageContent.luaScripts.forEach((script: any) => { - renderer.registerLuaScript(script.id, { - code: script.code, - parameters: script.parameters || [], - returnType: script.returnType || 'any', - }) - }) - } -} +export { executeLuaScript } from './execute-lua-script' +export { getComponentConfig } from './get-component-config' +export { hasComponentConfig } from './has-component-config' +export { interpolateValue } from './interpolate-value' +export { evaluateConditional } from './evaluate-conditional' +export { registerComponentConfig } from './register-component-config' +export { registerLuaScript } from './register-lua-script' +export { resolveDataSource } from './resolve-data-source'