mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-05-06 11:39:36 +00:00
code: dbal,lua,script (6 files)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file create-lua-script.ts
|
||||
* @description Create Lua script operation
|
||||
*/
|
||||
import type { LuaScript, CreateLuaScriptInput, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import { validateLuaSyntax } from '../validation/lua-script-validation';
|
||||
|
||||
/**
|
||||
* Create a new Lua script in the store
|
||||
*/
|
||||
export async function createLuaScript(
|
||||
store: InMemoryStore,
|
||||
input: CreateLuaScriptInput
|
||||
): Promise<Result<LuaScript>> {
|
||||
if (!input.name || input.name.length > 100) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Name required (max 100)' } };
|
||||
}
|
||||
if (!input.code) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Code required' } };
|
||||
}
|
||||
if (!validateLuaSyntax(input.code)) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Invalid Lua syntax' } };
|
||||
}
|
||||
|
||||
const script: LuaScript = {
|
||||
id: store.generateId('lua_script'),
|
||||
name: input.name,
|
||||
description: input.description ?? '',
|
||||
code: input.code,
|
||||
category: input.category ?? 'general',
|
||||
isActive: input.isActive ?? true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
store.luaScripts.set(script.id, script);
|
||||
return { success: true, data: script };
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @file delete-lua-script.ts
|
||||
* @description Delete Lua script operation
|
||||
*/
|
||||
import type { Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
|
||||
/**
|
||||
* Delete a Lua script by ID
|
||||
*/
|
||||
export async function deleteLuaScript(store: InMemoryStore, id: string): Promise<Result<boolean>> {
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
|
||||
}
|
||||
|
||||
const script = store.luaScripts.get(id);
|
||||
if (!script) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Lua script not found: ${id}` } };
|
||||
}
|
||||
|
||||
store.luaScripts.delete(id);
|
||||
return { success: true, data: true };
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @file get-lua-script.ts
|
||||
* @description Get Lua script operation
|
||||
*/
|
||||
import type { LuaScript, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
|
||||
/**
|
||||
* Get a Lua script by ID
|
||||
*/
|
||||
export async function getLuaScript(store: InMemoryStore, id: string): Promise<Result<LuaScript>> {
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
|
||||
}
|
||||
|
||||
const script = store.luaScripts.get(id);
|
||||
if (!script) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Lua script not found: ${id}` } };
|
||||
}
|
||||
|
||||
return { success: true, data: script };
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @file index.ts
|
||||
* @description Barrel export for Lua script operations
|
||||
*/
|
||||
export { createLuaScript } from './create-lua-script';
|
||||
export { getLuaScript } from './get-lua-script';
|
||||
export { updateLuaScript } from './update-lua-script';
|
||||
export { deleteLuaScript } from './delete-lua-script';
|
||||
export { listLuaScripts } from './list-lua-scripts';
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @file list-lua-scripts.ts
|
||||
* @description List Lua scripts with filtering and pagination
|
||||
*/
|
||||
import type { LuaScript, ListOptions, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
|
||||
/**
|
||||
* List Lua scripts with filtering and pagination
|
||||
*/
|
||||
export async function listLuaScripts(
|
||||
store: InMemoryStore,
|
||||
options: ListOptions = {}
|
||||
): Promise<Result<LuaScript[]>> {
|
||||
const { filter = {}, sort = {}, page = 1, limit = 20 } = options;
|
||||
|
||||
let scripts = Array.from(store.luaScripts.values());
|
||||
|
||||
// Apply filters
|
||||
if (filter.isActive !== undefined) {
|
||||
scripts = scripts.filter((s) => s.isActive === filter.isActive);
|
||||
}
|
||||
if (filter.category !== undefined) {
|
||||
scripts = scripts.filter((s) => s.category === filter.category);
|
||||
}
|
||||
|
||||
// Apply sorting
|
||||
if (sort.name) {
|
||||
scripts.sort((a, b) => (sort.name === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)));
|
||||
} else if (sort.createdAt) {
|
||||
scripts.sort((a, b) =>
|
||||
sort.createdAt === 'asc'
|
||||
? a.createdAt.getTime() - b.createdAt.getTime()
|
||||
: b.createdAt.getTime() - a.createdAt.getTime()
|
||||
);
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
const start = (page - 1) * limit;
|
||||
const paginated = scripts.slice(start, start + limit);
|
||||
|
||||
return { success: true, data: paginated };
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @file update-lua-script.ts
|
||||
* @description Update Lua script operation
|
||||
*/
|
||||
import type { LuaScript, UpdateLuaScriptInput, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import { validateLuaSyntax } from '../validation/lua-script-validation';
|
||||
|
||||
/**
|
||||
* Update an existing Lua script
|
||||
*/
|
||||
export async function updateLuaScript(
|
||||
store: InMemoryStore,
|
||||
id: string,
|
||||
input: UpdateLuaScriptInput
|
||||
): Promise<Result<LuaScript>> {
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
|
||||
}
|
||||
|
||||
const script = store.luaScripts.get(id);
|
||||
if (!script) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Lua script not found: ${id}` } };
|
||||
}
|
||||
|
||||
if (input.name !== undefined) {
|
||||
if (!input.name || input.name.length > 100) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Invalid name' } };
|
||||
}
|
||||
script.name = input.name;
|
||||
}
|
||||
|
||||
if (input.code !== undefined) {
|
||||
if (!input.code || !validateLuaSyntax(input.code)) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Invalid Lua code' } };
|
||||
}
|
||||
script.code = input.code;
|
||||
}
|
||||
|
||||
if (input.description !== undefined) script.description = input.description;
|
||||
if (input.category !== undefined) script.category = input.category;
|
||||
if (input.isActive !== undefined) script.isActive = input.isActive;
|
||||
|
||||
script.updatedAt = new Date();
|
||||
return { success: true, data: script };
|
||||
}
|
||||
Reference in New Issue
Block a user