import { ForgeSqlOrmOptions } from ".."; import { VerioningModificationForgeSQL, ForgeSqlOperation } from "./ForgeSQLQueryBuilder"; import { AnyMySqlTable } from "drizzle-orm/mysql-core"; import { InferInsertModel, SQL } from "drizzle-orm"; /** * Class implementing Modification operations for ForgeSQL ORM. * Provides methods for inserting, updating, and deleting records with support for optimistic locking. */ export declare class ForgeSQLCrudOperations implements VerioningModificationForgeSQL { private readonly forgeOperations; private readonly options; /** * Creates a new instance of ForgeSQLCrudOperations. * @param forgeSqlOperations - The ForgeSQL operations instance * @param options - Configuration options for the ORM */ constructor(forgeSqlOperations: ForgeSqlOperation, options: ForgeSqlOrmOptions); /** * Inserts records into the database with optional versioning support. * If a version field exists in the schema, versioning is applied. * * This method automatically handles: * - Version field initialization for optimistic locking * - Batch insertion for multiple records * - Duplicate key handling with optional updates * * @template T - The type of the table schema * @param schema - The entity schema * @param models - Array of entities to insert * @param updateIfExists - Whether to update existing records (default: false) * @returns Promise that resolves to the number of inserted rows * @throws Error if the insert operation fails */ insert(schema: T, models: InferInsertModel[], updateIfExists?: boolean): Promise; /** * Deletes a record by its primary key with optional version check. * If versioning is enabled, ensures the record hasn't been modified since last read. * * This method automatically handles: * - Single primary key validation * - Optimistic locking checks if versioning is enabled * - Version field validation before deletion * * @template T - The type of the table schema * @param id - The ID of the record to delete * @param schema - The entity schema * @returns Promise that resolves to the number of affected rows * @throws Error if the delete operation fails * @throws Error if multiple primary keys are found * @throws Error if optimistic locking check fails */ deleteById(id: unknown, schema: T): Promise; /** * Updates a record by its primary key with optimistic locking support. * If versioning is enabled: * - Retrieves the current version * - Checks for concurrent modifications * - Increments the version on successful update * * This method automatically handles: * - Primary key validation * - Version field retrieval and validation * - Optimistic locking conflict detection * - Version field incrementation * * @template T - The type of the table schema * @param entity - The entity with updated values (must include primary key) * @param schema - The entity schema * @returns Promise that resolves to the number of affected rows * @throws Error if the primary key is not provided * @throws Error if optimistic locking check fails * @throws Error if multiple primary keys are found */ updateById(entity: Partial>, schema: T): Promise; /** * Updates specified fields of records based on provided conditions. * This method does not support versioning and should be used with caution. * * @template T - The type of the table schema * @param {Partial>} updateData - The data to update * @param {T} schema - The entity schema * @param {SQL} where - The WHERE conditions * @returns {Promise} Number of affected rows * @throws {Error} If WHERE conditions are not provided * @throws {Error} If the update operation fails */ updateFields(updateData: Partial>, schema: T, where?: SQL): Promise; /** * Gets primary keys from the schema. * @template T - The type of the table schema * @param {T} schema - The table schema * @returns {[string, AnyColumn][]} Array of primary key name and column pairs * @throws {Error} If no primary keys are found */ private getPrimaryKeys; /** * Finds version field in columns by name. */ private findVersionField; /** * Validates that version field is not nullable. */ private validateVersionFieldNotNull; /** * Validates that version field type is supported. */ private validateVersionFieldType; /** * Validates and retrieves version field metadata. * @param {string} tableName - The name of the table * @param {Record} columns - The table columns * @returns {Object | undefined} Version field metadata if valid, undefined otherwise */ private validateVersionField; /** * Gets the current version of an entity. * @template T - The type of the table schema * @param {Partial>} entity - The entity * @param {string} primaryKeyName - The name of the primary key * @param {Object | undefined} versionMetadata - Version field metadata * @param {Record} columns - The table columns * @param {T} schema - The table schema * @returns {Promise} The current version value */ private getCurrentVersion; /** * Prepares a model for insertion with version field. * @template T - The type of the table schema * @param {Partial>} model - The model to prepare * @param {Object | undefined} versionMetadata - Version field metadata * @param {Record} columns - The table columns * @returns {InferInsertModel} The prepared model */ private prepareModelWithVersion; /** * Calculates new version value based on field type. * Coerces `currentVersion` via `Number()` so DECIMAL columns returned as * strings increment instead of concatenating; non-numeric inputs propagate * as `NaN` (matching prior behavior for missing/invalid versions). */ private calculateNewVersionValue; /** * Prepares update data with version field. * @template T - The type of the table schema * @param {Partial>} entity - The entity to update * @param {Object | undefined} versionMetadata - Version field metadata * @param {Record} columns - The table columns * @param {unknown} currentVersion - The current version value * @returns {Partial>} The prepared update data */ private prepareUpdateData; /** * Retrieves an existing model by primary key. * @template T - The type of the table schema * @param {Record} primaryKeyValues - The primary key values * @param {T} schema - The table schema * @param {[string, AnyColumn]} versionField - The version field name and column * @returns {Promise extends unknown[] ? Awaited[number] | undefined : Awaited | undefined>} The existing model * @throws {Error} If the record is not found */ private getOldModel; } //# sourceMappingURL=ForgeSQLCrudOperations.d.ts.map