protoobject
Version:
A universal class for creating any JSON objects and simple manipulations with them.
107 lines (106 loc) • 3.25 kB
TypeScript
import { EventEmitter } from "node:events";
import { ProtoObject } from "./proto-object.js";
import type { ProtoObjectDynamicMethods } from "../types/dynamic-methods.js";
/**
* File operation types
*/
export declare enum FileOperationType {
SAVE = "SAVE",
LOAD = "LOAD",
DELETE = "DELETE",
WATCH = "WATCH"
}
/**
* File formats supported
*/
export declare enum FileFormat {
JSON = ".json",
CSV = ".csv",
TEXT = ".txt"
}
/**
* File operation result
*/
export interface FileOperationResult {
success: boolean;
operation: FileOperationType;
filePath: string;
error?: Error;
}
/**
* Static methods interface for FS ProtoObject classes
*/
export interface ProtoObjectFSStaticMethods<T extends ProtoObjectDynamicMethods<T>> {
new (data?: Partial<T>): T;
fromJSON<U extends ProtoObjectDynamicMethods<U>>(data: {
[key: string]: unknown;
}): U;
loadFromFile<U extends ProtoObjectDynamicMethods<U>>(filePath: string): Promise<U>;
loadManyFromDirectory<U extends ProtoObjectDynamicMethods<U>>(directoryPath: string): Promise<U[]>;
}
/**
* CSV field mapping for exports
*/
export interface CSVFieldMapping {
[]: string;
}
/**
* File system watcher for ProtoObject files
*/
export declare class ProtoObjectFileWatcher extends EventEmitter {
private watchers;
/**
* Watch a file for changes
*/
watchFile(filePath: string): void;
/**
* Stop watching a file
*/
unwatchFile(filePath: string): void;
/**
* Stop watching all files
*/
unwatchAll(): void;
}
/**
* Base class for File System enabled ProtoObjects
*/
export declare class ProtoObjectFS<T extends ProtoObjectDynamicMethods<T>> extends ProtoObject<T> {
constructor(data?: Partial<T>);
/**
* Save object to file
*/
saveToFile(filePath: string, format?: FileFormat): Promise<FileOperationResult>;
/**
* Load object from file
*/
static loadFromFile<U extends ProtoObjectDynamicMethods<U>>(filePath: string): Promise<U>;
/**
* Load multiple objects from directory
*/
static loadManyFromDirectory<U extends ProtoObjectDynamicMethods<U>>(directoryPath: string, pattern?: string): Promise<U[]>;
/**
* Delete file
*/
deleteFile(filePath: string): Promise<FileOperationResult>;
/**
* Save multiple objects to directory
*/
static saveManyToDirectory<T extends ProtoObjectFS<T>>(objects: T[], directoryPath: string, fileNameGenerator?: (obj: T, index: number) => string): Promise<FileOperationResult[]>;
/**
* Convert to CSV format
*/
toCSV(fieldMapping?: CSVFieldMapping): string;
/**
* Batch save with streaming for large datasets
*/
static saveManyToFileStream<T extends ProtoObjectFS<T>>(objects: T[], filePath: string, format?: FileFormat): Promise<FileOperationResult>;
/**
* Load large datasets with streaming
*/
static loadManyFromFileStream<U extends ProtoObjectDynamicMethods<U>>(filePath: string, callback: (object: U) => void | Promise<void>): Promise<void>;
/**
* Create backup of object with timestamp
*/
createBackup(backupDirectory: string): Promise<FileOperationResult>;
}