/** * db4ai Collection Implementation * * Provides an in-memory collection implementation with CRUD operations. * This implementation is used for testing and can be extended with * remote or binding-based transports. * * Features: * - Memory-efficient document storage * - Optional query caching for repeated reads * - Batch operation optimization * - Builder pattern for advanced configuration * - Lifecycle hooks support * * @packageDocumentation */ import type { Collection, WithId, WithoutId } from './types.js'; import { QueryCache } from './cache.js'; /** * Create an in-memory collection instance. * Provides a full implementation of the Collection interface. * * @typeParam T - Document type * @returns Collection instance with CRUD operations * * @example * ```typescript * interface User { * name: string; * email: string; * age: number; * } * * const users = createCollection(); * * // Create * const user = await users.create({ name: 'Alice', email: 'alice@example.com', age: 30 }); * * // Read * const found = await users.get(user.id); * const adults = await users.find({ age: { $gte: 18 } }); * * // Update * await users.update(user.id, { age: 31 }); * * // Delete * await users.delete(user.id); * ``` */ export declare function createCollection(): Collection; /** * Create an optimized collection with cache support. * * @typeParam T - Document type * @param name - Collection name (used for cache keys) * @param cache - Optional query cache instance * @returns Collection instance with CRUD operations and caching * * @example * ```typescript * const cache = createQueryCache({ maxEntries: 1000 }); * const users = createOptimizedCollection('users', cache); * * // First call hits the database * const admins = await users.find({ role: 'admin' }); * * // Second call returns cached results * const adminsAgain = await users.find({ role: 'admin' }); * ``` */ export declare function createOptimizedCollection(name: string, cache?: QueryCache): Collection; /** * Lifecycle hooks for collection operations. */ export interface CollectionHooks { /** Called before a document is created */ beforeCreate?: (doc: WithoutId) => WithoutId | Promise>; /** Called after a document is created */ afterCreate?: (doc: WithId) => void | Promise; /** Called before a document is updated */ beforeUpdate?: (id: string, changes: Partial) => Partial | Promise>; /** Called after a document is updated */ afterUpdate?: (doc: WithId) => void | Promise; /** Called before a document is deleted */ beforeDelete?: (id: string) => void | Promise; /** Called after a document is deleted */ afterDelete?: (id: string) => void | Promise; } /** * Builder for creating collections with custom configuration. * Supports custom ID generators, validators, and lifecycle hooks. * * @typeParam T - Document type * * @example * ```typescript * import { v4 as uuidv4 } from 'uuid'; * * const users = CollectionBuilder.create() * .withIdGenerator(() => `user_${uuidv4()}`) * .withValidator((doc) => { * if (!doc.email.includes('@')) { * throw new Error('Invalid email'); * } * }) * .beforeCreate(async (doc) => ({ * ...doc, * createdAt: Date.now() * })) * .afterCreate(async (doc) => { * console.log(`User created: ${doc.id}`); * }) * .build(); * ``` */ export declare class CollectionBuilder { private _idGenerator; private _validator?; private _hooks; /** * Create a new collection builder. * * @returns A new CollectionBuilder instance */ static create(): CollectionBuilder; /** * Set a custom ID generator function. * * @param generator - Function that generates unique IDs * @returns The builder for chaining * * @example * ```typescript * builder.withIdGenerator(() => `doc_${Date.now()}_${Math.random().toString(36)}`) * ``` */ withIdGenerator(generator: () => string): this; /** * Set a validation function for documents. * The function should throw an error if validation fails. * * @param validator - Validation function * @returns The builder for chaining * * @example * ```typescript * builder.withValidator((doc) => { * if (!doc.email) throw new Error('Email is required'); * if (doc.age < 0) throw new Error('Age must be positive'); * }) * ``` */ withValidator(validator: (doc: WithoutId) => void): this; /** * Add a beforeCreate hook. * Use this to transform documents before they are stored. * * @param hook - Function to call before creating a document * @returns The builder for chaining * * @example * ```typescript * builder.beforeCreate((doc) => ({ * ...doc, * createdAt: new Date().toISOString() * })) * ``` */ beforeCreate(hook: (doc: WithoutId) => WithoutId | Promise>): this; /** * Add an afterCreate hook. * Use this for side effects like logging or notifications. * * @param hook - Function to call after creating a document * @returns The builder for chaining */ afterCreate(hook: (doc: WithId) => void | Promise): this; /** * Add a beforeUpdate hook. * Use this to transform changes before they are applied. * * @param hook - Function to call before updating a document * @returns The builder for chaining * * @example * ```typescript * builder.beforeUpdate((id, changes) => ({ * ...changes, * updatedAt: new Date().toISOString() * })) * ``` */ beforeUpdate(hook: (id: string, changes: Partial) => Partial | Promise>): this; /** * Add an afterUpdate hook. * * @param hook - Function to call after updating a document * @returns The builder for chaining */ afterUpdate(hook: (doc: WithId) => void | Promise): this; /** * Add a beforeDelete hook. * Use this for cleanup or to prevent deletion. * * @param hook - Function to call before deleting a document * @returns The builder for chaining */ beforeDelete(hook: (id: string) => void | Promise): this; /** * Add an afterDelete hook. * * @param hook - Function to call after deleting a document * @returns The builder for chaining */ afterDelete(hook: (id: string) => void | Promise): this; /** * Build and return the configured collection. * * @returns The configured collection */ build(): Collection; } //# sourceMappingURL=collection.d.ts.map