Class

Sequence

Sequence(seq)

Constructor

# new Sequence(seq)

A model class that represents a sequence in the RAM adapter. It stores the current value of a sequence that can be used for generating sequential identifiers for entities. The sequence is identified by its ID and maintains the current value.

RAM sequence model for auto-incrementing values

Parameters:
Name Type Description
seq ModelArg.<Sequence>

Initial sequence data

View Source model/SequenceModel.ts, line 12

Example
```typescript
// Create a new sequence
const orderSequence = new Sequence({ id: 'order_seq', current: 1 });

// Use the sequence to get the next value
const nextOrderId = parseInt(orderSequence.current.toString()) + 1;
orderSequence.current = nextOrderId;
```

Classes

Sequence

Protected constructor that initializes the sequence with the provided options

Sequence

Protected constructor that initializes the sequence with the provided options

Methods

# async current(args)

Gets the current value of the sequence from storage. If the sequence doesn't exist yet, it returns the configured starting value.

Retrieves the current value of the sequence

Parameters:
Name Type Description
args MaybeContextualArg.<any>

View Source persistence/Sequence.ts, line 271

A promise that resolves to the current sequence value

# async protected increment(current, countopt)

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.

Increments the sequence value

Parameters:
Name Type Attributes Description
current string | number | bigint

The current value of the sequence

count number <optional>

Optional amount to increment by, defaults to the sequence's incrementBy value

View Source persistence/Sequence.ts, line 283

A promise that resolves to the new sequence value after incrementing

# async next(argz)

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.

Gets the next value in the sequence

Parameters:
Name Type Description
argz MaybeContextualArg.<any>

View Source persistence/Sequence.ts, line 293

A promise that resolves to the next value in the sequence

# async range(count)

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.

Generates a range of sequential values

Parameters:
Name Type Description
count number

The number of sequential values to generate

View Source persistence/Sequence.ts, line 304

A promise that resolves to an array of sequential values

# static parseValue(type, value) → {string|number|bigint}

Converts a sequence value to the specified type (Number or BigInt)

Parses a sequence value to the appropriate type

Parameters:
Name Type Description
type "Number" | "BigInt" | undefined

The target type to convert to

value string | number | bigint

The value to convert

View Source persistence/Sequence.ts, line 322

The converted value

string | number | bigint

# static pk(model) → {string}

Utility method that returns the standardized sequence name for a model's primary key

Gets the primary key sequence name for a model

Parameters:
Name Type Description
model M | Constructor.<M>

The model instance or constructor

View Source persistence/Sequence.ts, line 313

The sequence name for the model's primary key

string

Sequence(options)

Constructor

# new Sequence(options)

Provides a framework for generating sequential values (like primary keys) in the persistence layer. Implementations of this class handle the specifics of how sequences are stored and incremented in different database systems.

Abstract base class for sequence generation

Parameters:
Name Type Description
options SequenceOptions

Configuration options for the sequence generator

View Source persistence/Sequence.ts, line 10

Example
```typescript
// Example implementation for a specific database
class PostgresSequence extends Sequence {
  constructor(options: SequenceOptions) {
    super(options);
  }

  async next(): Promise<number> {
    // Implementation to get next value from PostgreSQL sequence
    const result = await this.options.executor.raw(`SELECT nextval('${this.options.name}')`);
    return parseInt(result.rows[0].nextval);
  }

  async current(): Promise<number> {
    // Implementation to get current value from PostgreSQL sequence
    const result = await this.options.executor.raw(`SELECT currval('${this.options.name}')`);
    return parseInt(result.rows[0].currval);
  }

  async range(count: number): Promise<number[]> {
    // Implementation to get a range of values
    const values: number[] = [];
    for (let i = 0; i < count; i++) {
      values.push(await this.next());
    }
    return values;
  }
}

// Usage
const sequence = new PostgresSequence({
  name: 'user_id_seq',
  executor: dbExecutor
});

const nextId = await sequence.next();
```

Classes

Sequence

Protected constructor that initializes the sequence with the provided options

Sequence

Protected constructor that initializes the sequence with the provided options

Methods

# async current(args)

Gets the current value of the sequence from storage. If the sequence doesn't exist yet, it returns the configured starting value.

Retrieves the current value of the sequence

Parameters:
Name Type Description
args MaybeContextualArg.<any>

View Source persistence/Sequence.ts, line 271

A promise that resolves to the current sequence value

# async protected increment(current, countopt)

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.

Increments the sequence value

Parameters:
Name Type Attributes Description
current string | number | bigint

The current value of the sequence

count number <optional>

Optional amount to increment by, defaults to the sequence's incrementBy value

View Source persistence/Sequence.ts, line 283

A promise that resolves to the new sequence value after incrementing

# async next(argz)

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.

Gets the next value in the sequence

Parameters:
Name Type Description
argz MaybeContextualArg.<any>

View Source persistence/Sequence.ts, line 293

A promise that resolves to the next value in the sequence

# async range(count)

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.

Generates a range of sequential values

Parameters:
Name Type Description
count number

The number of sequential values to generate

View Source persistence/Sequence.ts, line 304

A promise that resolves to an array of sequential values

# static parseValue(type, value) → {string|number|bigint}

Converts a sequence value to the specified type (Number or BigInt)

Parses a sequence value to the appropriate type

Parameters:
Name Type Description
type "Number" | "BigInt" | undefined

The target type to convert to

value string | number | bigint

The value to convert

View Source persistence/Sequence.ts, line 322

The converted value

string | number | bigint

# static pk(model) → {string}

Utility method that returns the standardized sequence name for a model's primary key

Gets the primary key sequence name for a model

Parameters:
Name Type Description
model M | Constructor.<M>

The model instance or constructor

View Source persistence/Sequence.ts, line 313

The sequence name for the model's primary key

string