/** * TONLDocument - Main document class for TONL Query & Navigation API * * Provides a high-level interface for working with TONL documents including * querying, navigation, and export capabilities. */ import type { TONLValue, EncodeOptions, DecodeOptions } from './types.js'; import { type WalkCallback, type WalkOptions } from './navigation/index.js'; import { type DiffResult } from './modification/index.js'; import { type IndexOptions, type IIndex } from './indexing/index.js'; import { type ValidationResult } from './schema/index.js'; import { AggregationResult, type AggregationOptions } from './query/aggregators.js'; /** * Document statistics */ export interface DocumentStats { /** * Size in bytes (TONL format) */ sizeBytes: number; /** * Total number of nodes in the tree */ nodeCount: number; /** * Maximum depth of the tree */ maxDepth: number; /** * Number of arrays in the document */ arrayCount: number; /** * Number of objects in the document */ objectCount: number; /** * Number of primitive values */ primitiveCount: number; } /** * TONLDocument class - main interface for TONL documents * * @example * ```typescript * // Parse from TONL string * const doc = TONLDocument.parse(tonlText); * * // Query document * const name = doc.get('user.name'); * const admins = doc.query('users[?(@.role == "admin")]'); * * // Navigate document * for (const [key, value] of doc.entries()) { * console.log(`${key}: ${value}`); * } * * // Export * const json = doc.toJSON(); * const tonl = doc.toTONL(); * ``` */ export declare class TONLDocument { private data; private evaluator; private indexManager; /** * Private constructor - use static factory methods */ private constructor(); /** * Parse a TONL string into a document * * @param tonlText - TONL format string * @param options - Decode options * @returns TONLDocument instance */ static parse(tonlText: string, options?: DecodeOptions): TONLDocument; /** * Create a document from JSON data * * @param data - JavaScript object or array * @returns TONLDocument instance */ static fromJSON(data: any): TONLDocument; /** * Load a TONL document from a file synchronously * * @param path - File path * @returns TONLDocument instance */ static fromFileSync(path: string): TONLDocument; /** * Load a TONL document from a file asynchronously * * @param path - File path * @returns Promise */ static fromFile(path: string): Promise; /** * Load a TONL document from a file (alias for fromFileSync) * * @param path - File path * @returns TONLDocument instance * * @example * ```typescript * const doc = TONLDocument.load('data.tonl'); * ``` */ static load(path: string): TONLDocument; /** * Get a value at a specific path * * @param pathExpression - Path expression (e.g., "user.name", "users[0].id") * @returns Value at path or undefined if not found * * @example * ```typescript * const name = doc.get('user.name'); * const firstUser = doc.get('users[0]'); * ``` */ get(pathExpression: string): any; /** * Query the document with a path expression * * This is an alias for get() but semantically clearer for complex queries * * @param pathExpression - Path expression with wildcards, filters, etc. * @returns Query result (may be array for wildcards/filters) */ query(pathExpression: string): any; /** * Check if a path exists in the document * * @param pathExpression - Path expression * @returns True if path exists, false otherwise */ exists(pathExpression: string): boolean; /** * Get the type of value at a path * * @param pathExpression - Path expression * @returns Type string or undefined if path doesn't exist */ typeOf(pathExpression: string): string | undefined; /** * Iterate over [key, value] pairs at the root level * * @returns Iterator of [key, value] pairs */ entries(): Generator<[string, any], void, undefined>; /** * Iterate over keys at the root level * * @returns Iterator of keys */ keys(): Generator; /** * Iterate over values at the root level * * @returns Iterator of values */ values(): Generator; /** * Recursively iterate over all [path, value] pairs * * @param maxDepth - Maximum recursion depth * @returns Iterator of [path, value] pairs */ deepEntries(maxDepth?: number): Generator<[string, any], void, undefined>; /** * Recursively iterate over all paths * * @param maxDepth - Maximum recursion depth * @returns Iterator of paths */ deepKeys(maxDepth?: number): Generator; /** * Recursively iterate over all values * * @param maxDepth - Maximum recursion depth * @returns Iterator of values */ deepValues(maxDepth?: number): Generator; /** * Walk the document tree with a callback * * @param callback - Function called for each node * @param options - Walk options */ walk(callback: WalkCallback, options?: WalkOptions): void; /** * Count total number of nodes in the document * * @returns Node count */ countNodes(): number; /** * Find a value matching a predicate * * @param predicate - Test function * @returns First matching value or undefined */ find(predicate: (value: any, path: string) => boolean): any; /** * Find all values matching a predicate * * @param predicate - Test function * @returns Array of matching values */ findAll(predicate: (value: any, path: string) => boolean): any[]; /** * Check if any value matches a predicate * * @param predicate - Test function * @returns True if any match */ some(predicate: (value: any, path: string) => boolean): boolean; /** * Check if all values match a predicate * * @param predicate - Test function * @returns True if all match */ every(predicate: (value: any, path: string) => boolean): boolean; /** * Convert to JavaScript object/array * * @returns The underlying data */ toJSON(): any; /** * Convert to TONL format string * * @param options - Encoding options * @returns TONL format string */ toTONL(options?: EncodeOptions): string; /** * Save document to a file synchronously * * @param path - File path * @param options - Encoding options */ saveSync(path: string, options?: EncodeOptions): void; /** * Save document to a file asynchronously * * @param path - File path * @param options - Encoding options */ save(path: string, options?: EncodeOptions): Promise; /** * Get document size in bytes (TONL format) * * @returns Size in bytes */ size(): number; /** * Get detailed document statistics * * @returns Document statistics */ stats(): DocumentStats; /** * Get the raw data (for advanced use cases) * * @returns The underlying data */ getData(): TONLValue; /** * Set a value at a specific path * * @param pathExpression - Path expression * @param value - Value to set * @returns this for chaining * * @example * ```typescript * doc.set('user.name', 'Alice Smith'); * doc.set('users[0].active', true); * doc.set('user.profile.age', 31); * ``` */ set(pathExpression: string, value: any): this; /** * Delete a value at a specific path */ delete(pathExpression: string): this; /** * Push to array */ push(arrayPath: string, ...values: any[]): number; /** * Pop from array */ pop(arrayPath: string): any; /** * Merge object at a specific path or at root level * * @overload * @param updates - Object to merge at root level * @returns this for chaining * * @overload * @param pathExpression - Path to merge at * @param updates - Object to merge * @returns this for chaining */ merge(pathExpressionOrUpdates: string | Record, updates?: Record): this; /** * Compare this document with another and get a diff * * @param other - The other document to compare with * @returns Diff result with all changes * * @example * ```typescript * const doc1 = TONLDocument.fromJSON({ a: 1, b: 2 }); * const doc2 = TONLDocument.fromJSON({ a: 1, b: 3, c: 4 }); * const diff = doc1.diff(doc2); * console.log(diff.summary); // { added: 1, modified: 1, deleted: 0, total: 2 } * ``` */ diff(other: TONLDocument): DiffResult; /** * Get a formatted string representation of changes between documents * * @param other - The other document to compare with * @returns Human-readable diff string */ diffString(other: TONLDocument): string; /** * Create a snapshot of the current document state * * @returns A new document with a deep copy of current data */ snapshot(): TONLDocument; /** * Create an index on specified fields * * @overload * @param name - Index name * @param path - Path expression to index * @param type - Index type ('hash' or 'btree') * @returns The created index * * @overload * @param options - Index options * @returns The created index * * @example * ```typescript * // Simple 3-argument signature * doc.createIndex('userById', 'users[*].id', 'hash'); * doc.createIndex('userByAge', 'users[*].age', 'btree'); * * // Full options signature * doc.createIndex({ name: 'userIdIndex', fields: ['id'], unique: true }); * * // Create btree index for range queries * doc.createIndex({ name: 'ageIndex', fields: ['age'], type: 'btree' }); * * // Create compound index on multiple fields * doc.createIndex({ name: 'nameAgeIndex', fields: ['name', 'age'] }); * ``` */ createIndex(nameOrOptions: string | IndexOptions, path?: string, type?: 'hash' | 'btree'): IIndex; /** * Get an index by name */ getIndex(name: string): IIndex | undefined; /** * Drop an index */ dropIndex(name: string): boolean; /** * List all index names */ listIndices(): string[]; /** * Get statistics for all indices */ indexStats(): Record; /** * Alias for indexStats() - for consistency with test expectations */ getIndexStats(): Record; /** * Get query cache statistics * * @returns Cache statistics with hits, misses, and hit rate * * @example * ```typescript * const stats = doc.getCacheStats(); * console.log(`Cache hit rate: ${stats.hitRate}%`); * ``` * * BUG-007 FIX: Now returns accurate misses from cache stats */ getCacheStats(): { hits: number; misses: number; hitRate: number; size: number; }; /** * Restore document state from a snapshot * * @param snapshot - Document snapshot to restore from * @returns this for chaining * * @example * ```typescript * const snapshot = doc.snapshot(); * doc.set('user.name', 'New Name'); * doc.restore(snapshot); // Reverts to original state * ``` */ restore(snapshot: TONLDocument): this; /** * Create a simple hash or btree index (private helper) * * @param name - Index name * @param path - Path expression to index * @param type - Index type ('hash' or 'btree') * @returns The created index */ private createSimpleIndex; /** * Query an index by exact value * * @param indexName - Name of the index * @param value - Value to look up * @returns Matching document or undefined * * @example * ```typescript * const user = doc.queryIndex('userById', 123); * ``` */ queryIndex(indexName: string, value: any): any; /** * Query an index with range (for btree indices) * * @param indexName - Name of the btree index * @param min - Minimum value (inclusive) * @param max - Maximum value (inclusive) * @returns Array of matching documents * * @example * ```typescript * const users = doc.queryIndexRange('userByAge', 25, 35); * ``` */ queryIndexRange(indexName: string, min: any, max: any): any[]; /** * Create a compound index on multiple fields * * @param name - Index name * @param paths - Array of path expressions * @param type - Index type ('hash' or 'btree', default 'hash') * @returns The created compound index * * @example * ```typescript * doc.createCompoundIndex('userRoleAge', ['users[*].role', 'users[*].age']); * doc.createCompoundIndex('sortedUsers', ['users[*].age', 'users[*].name'], 'btree'); * ``` */ createCompoundIndex(name: string, paths: string[], type?: 'hash' | 'btree'): IIndex; /** * Validate document against a schema file * * @param schemaPath - Path to schema file (.schema.tonl) * @returns Validation result with detailed error information * * @example * ```typescript * const result = doc.validate('schemas/users.schema.tonl'); * if (!result.valid) { * console.error('Validation failed:', result.errors); * } * ``` */ validate(schemaPath: string): ValidationResult; /** * Stream query results (for large result sets) * * @param pathExpression - Path expression to query * @returns Generator yielding results one at a time * * @example * ```typescript * for (const item of doc.stream('items[*]')) { * console.log(item); * } * ``` */ stream(pathExpression: string): Generator; /** * Aggregate query results with count, sum, avg, min, max, groupBy * * @param pathExpression - Path expression to query * @param options - Aggregation options * @returns AggregationResult wrapper for fluent operations * * @example * ```typescript * // Count users * doc.aggregate("users[*]").count() // 42 * * // Sum order totals * doc.aggregate("orders[*]").sum("total") // 15420.50 * * // Average age * doc.aggregate("users[*]").avg("age") // 29.5 * * // Group by country * doc.aggregate("users[*]").groupBy("country") * // { "TR": [...], "US": [...] } * * // Chained operations * doc.aggregate("users[*]") * .filter(u => u.active) * .orderBy("age", "desc") * .take(10) * .toArray() * * // Statistics * doc.aggregate("products[*]").stats("price") * // { count, sum, avg, min, max, variance, stdDev } * ``` */ aggregate(pathExpression: string, options?: AggregationOptions): AggregationResult; /** * Count items matching a query * * @param pathExpression - Path expression to query * @returns Number of matching items * * @example * ```typescript * doc.count("users[*]") // 42 * doc.count("users[?(@.active)]") // 38 * doc.count("orders[?(@.status == 'pending')]") * ``` */ count(pathExpression: string): number; /** * Sum numeric values from query results * * @param pathExpression - Path expression to query * @param field - Optional field name for object arrays * @returns Sum of values * * @example * ```typescript * doc.sum("orders[*]", "total") // 15420.50 * doc.sum("items[*].quantity") // 150 * ``` */ sum(pathExpression: string, field?: string): number; /** * Calculate average from query results * * @param pathExpression - Path expression to query * @param field - Optional field name for object arrays * @returns Average value * * @example * ```typescript * doc.avg("users[*]", "age") // 29.5 * doc.avg("products[*]", "price") // 49.99 * ``` */ avg(pathExpression: string, field?: string): number; /** * Find minimum value from query results * * @param pathExpression - Path expression to query * @param field - Optional field name for object arrays * @returns Minimum value * * @example * ```typescript * doc.min("products[*]", "price") // 9.99 * doc.min("users[*]", "age") // 18 * ``` */ min(pathExpression: string, field?: string): any; /** * Find maximum value from query results * * @param pathExpression - Path expression to query * @param field - Optional field name for object arrays * @returns Maximum value * * @example * ```typescript * doc.max("products[*]", "price") // 999.99 * doc.max("users[*]", "age") // 65 * ``` */ max(pathExpression: string, field?: string): any; /** * Group query results by a field * * @param pathExpression - Path expression to query * @param field - Field name to group by * @returns Object with group keys and arrays of items * * @example * ```typescript * doc.groupBy("users[*]", "country") * // { "TR": [user1, user2], "US": [user3] } * * doc.groupBy("orders[*]", "status") * // { "pending": [...], "completed": [...] } * ``` */ groupBy(pathExpression: string, field: K): Record; /** * Get distinct values from query results * * @param pathExpression - Path expression to query * @param field - Optional field name for object arrays * @returns Array of unique values * * @example * ```typescript * doc.distinct("users[*]", "country") // ["TR", "US", "DE"] * doc.distinct("tags[*]") // ["js", "ts", "node"] * ``` */ distinct(pathExpression: string, field?: string): any[]; } //# sourceMappingURL=document.d.ts.map