forge-sql-orm
Version:
Drizzle ORM integration for Forge-SQL in Atlassian Forge applications.
128 lines • 6.31 kB
TypeScript
import { ForgeSqlOrmOptions } from "..";
import { CRUDForgeSQL, ForgeSqlOperation } from "./ForgeSQLQueryBuilder";
import { AnyMySqlTable } from "drizzle-orm/mysql-core/index";
import { InferInsertModel } from "drizzle-orm";
import { SQL } from "drizzle-orm";
/**
* Class implementing CRUD operations for ForgeSQL ORM.
* Provides methods for inserting, updating, and deleting records with support for optimistic locking.
*/
export declare class ForgeSQLCrudOperations implements CRUDForgeSQL {
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.
*
* @template T - The type of the table schema
* @param {T} schema - The entity schema
* @param {Partial<InferInsertModel<T>>[]} models - Array of entities to insert
* @param {boolean} [updateIfExists=false] - Whether to update existing records
* @returns {Promise<number>} The number of inserted rows
* @throws {Error} If the insert operation fails
*/
insert<T extends AnyMySqlTable>(schema: T, models: InferInsertModel<T>[], updateIfExists?: boolean): Promise<number>;
/**
* 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.
*
* @template T - The type of the table schema
* @param {unknown} id - The ID of the record to delete
* @param {T} schema - The entity schema
* @returns {Promise<number>} Number of affected rows
* @throws {Error} If the delete operation fails
* @throws {Error} If multiple primary keys are found
*/
deleteById<T extends AnyMySqlTable>(id: unknown, schema: T): Promise<number>;
/**
* 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
*
* @template T - The type of the table schema
* @param {Partial<InferInsertModel<T>>} entity - The entity with updated values
* @param {T} schema - The entity schema
* @returns {Promise<number>} 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<T extends AnyMySqlTable>(entity: Partial<InferInsertModel<T>>, schema: T): Promise<number>;
/**
* 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<InferInsertModel<T>>} updateData - The data to update
* @param {T} schema - The entity schema
* @param {SQL<unknown>} where - The WHERE conditions
* @returns {Promise<number>} Number of affected rows
* @throws {Error} If WHERE conditions are not provided
* @throws {Error} If the update operation fails
*/
updateFields<T extends AnyMySqlTable>(updateData: Partial<InferInsertModel<T>>, schema: T, where?: SQL<unknown>): Promise<number>;
/**
* 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;
/**
* Validates and retrieves version field metadata.
* @param {string} tableName - The name of the table
* @param {Record<string, AnyColumn>} 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<InferInsertModel<T>>} entity - The entity
* @param {string} primaryKeyName - The name of the primary key
* @param {Object | undefined} versionMetadata - Version field metadata
* @param {Record<string, AnyColumn>} columns - The table columns
* @param {T} schema - The table schema
* @returns {Promise<unknown>} The current version value
*/
private getCurrentVersion;
/**
* Prepares a model for insertion with version field.
* @template T - The type of the table schema
* @param {Partial<InferInsertModel<T>>} model - The model to prepare
* @param {Object | undefined} versionMetadata - Version field metadata
* @param {Record<string, AnyColumn>} columns - The table columns
* @returns {InferInsertModel<T>} The prepared model
*/
private prepareModelWithVersion;
/**
* Prepares update data with version field.
* @template T - The type of the table schema
* @param {Partial<InferInsertModel<T>>} entity - The entity to update
* @param {Object | undefined} versionMetadata - Version field metadata
* @param {Record<string, AnyColumn>} columns - The table columns
* @param {unknown} currentVersion - The current version value
* @returns {Partial<InferInsertModel<T>>} The prepared update data
*/
private prepareUpdateData;
/**
* Retrieves an existing model by primary key.
* @template T - The type of the table schema
* @param {Record<string, unknown>} primaryKeyValues - The primary key values
* @param {T} schema - The table schema
* @param {[string, AnyColumn]} versionField - The version field name and column
* @returns {Promise<Awaited<T> extends Array<any> ? Awaited<T>[number] | undefined : Awaited<T> | undefined>} The existing model
* @throws {Error} If the record is not found
*/
private getOldModel;
}
//# sourceMappingURL=ForgeSQLCrudOperations.d.ts.map