UNPKG

@decaf-ts/core

Version:

Core persistence module for the decaf framework

76 lines (75 loc) 3.54 kB
import { RamSequenceModel } from "./model/RamSequenceModel"; import { Sequence } from "../persistence"; import { SequenceOptions } from "../interfaces"; import { RamAdapter } from "./RamAdapter"; import { Repo } from "../repository"; /** * @description RAM-specific sequence implementation * @summary Extends the base Sequence class to provide auto-incrementing sequence functionality * for the RAM adapter. This class manages sequences stored in memory, allowing for the generation * of sequential identifiers for entities. * @param {SequenceOptions} options - Configuration options for the sequence * @param {RamAdapter} adapter - The RAM adapter instance to use for storage * @class RamSequence * @category Ram * @example * ```typescript * // Create a new numeric sequence starting at 1 * const sequence = new RamSequence({ * name: 'order_sequence', * type: 'Number', * startWith: 1, * incrementBy: 1 * }, ramAdapter); * * // Get the next value in the sequence * const nextId = await sequence.next(); * * // Get a range of values * const idRange = await sequence.range(5); // Returns 5 sequential values * ``` */ export declare class RamSequence extends Sequence { protected repo: Repo<RamSequenceModel>; constructor(options: SequenceOptions, adapter: RamAdapter); /** * @description Retrieves the current value of the sequence * @summary Gets the current value of the sequence from storage. If the sequence * doesn't exist yet, it returns the configured starting value. * @return A promise that resolves to the current sequence value */ current(): Promise<string | number | bigint>; /** * @description Parses a value according to the sequence type * @summary Converts a value to the appropriate type for the sequence (string, number, or bigint) * using the base Sequence class's parseValue method. * @param {string | number | bigint} value - The value to parse * @return {string | number | bigint} The parsed value in the correct type */ private parse; /** * @description Increments the sequence value * @summary Increases the current sequence value by the specified amount and persists * the new value to storage. This method handles both numeric and BigInt sequence types. * @param {string | number | bigint} current - The current value of the sequence * @param {number} [count] - Optional amount to increment by, defaults to the sequence's incrementBy value * @return A promise that resolves to the new sequence value after incrementing */ private increment; /** * @description Gets the next value in the sequence * @summary Retrieves the current value of the sequence and increments it by the * configured increment amount. This is the main method used to get a new sequential value. * @return A promise that resolves to the next value in the sequence */ next(): Promise<number | string | bigint>; /** * @description Generates a range of sequential values * @summary Retrieves a specified number of sequential values from the sequence. * This is useful when you need to allocate multiple IDs at once. * The method increments the sequence by the total amount needed and returns all values in the range. * @param {number} count - The number of sequential values to generate * @return A promise that resolves to an array of sequential values */ range(count: number): Promise<(number | string | bigint)[]>; }